From f46a1d724dd4811cebe3e21b1f0f119bb64df40d Mon Sep 17 00:00:00 2001 From: c-bata Date: Wed, 25 May 2022 21:16:38 +0900 Subject: [PATCH] Update visual regression test --- optuna_dashboard/_serializer.py | 7 ++++--- visual_regression_test.py | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/optuna_dashboard/_serializer.py b/optuna_dashboard/_serializer.py index 685252cc..f1e40a74 100644 --- a/optuna_dashboard/_serializer.py +++ b/optuna_dashboard/_serializer.py @@ -5,20 +5,20 @@ from typing import List from typing import Tuple from typing import Union +import numpy as np from optuna.distributions import BaseDistribution from optuna.study import StudySummary from optuna.trial import FrozenTrial -import numpy as np from . import _note as note try: - from typing import TypedDict from typing import Literal + from typing import TypedDict except ImportError: - from typing_extensions import TypedDict from typing_extensions import Literal + from typing_extensions import TypedDict MAX_ATTR_LENGTH = 1024 @@ -125,6 +125,7 @@ def serialize_frozen_trial(study_id: int, trial: FrozenTrial) -> Dict[str, Any]: elif np.isneginf(value): serialized_value = "-inf" else: + assert np.isfinite(value) serialized_value = value serialized_intermediate_values.append({"step": step, "value": serialized_value}) serialized["intermediate_values"] = serialized_intermediate_values diff --git a/visual_regression_test.py b/visual_regression_test.py index fbe639bb..f601a81f 100644 --- a/visual_regression_test.py +++ b/visual_regression_test.py @@ -87,13 +87,15 @@ def create_dummy_storage() -> optuna.storages.InMemoryStorage: study.optimize(objective_single_dynamic, n_trials=50) - # Single objective study with 'inf' value + # Single objective study with 'inf', '-inf', or 'nan' value study = optuna.create_study(study_name="single-inf", storage=storage) def objective_single_inf(trial: optuna.Trial) -> float: x = trial.suggest_float("x", -10, 10) - if x > 0: - return math.inf + if trial.number % 3 == 0: + return float("inf") + elif trial.number % 3 == 1: + return float("-inf") else: return x**2 @@ -152,13 +154,19 @@ def create_dummy_storage() -> optuna.storages.InMemoryStorage: study.optimize(objective_prune_without_report, n_trials=100) - # Single objective pruned after reported 'inf' value + # Single objective pruned after reported 'inf', '-inf', or 'nan' study = optuna.create_study(study_name="single-inf-report", storage=storage) def objective_single_inf_report(trial: optuna.Trial) -> float: x = trial.suggest_float("x", -10, 10) + if trial.number % 3 == 0: + trial.report(float("inf"), 1) + elif trial.number % 3 == 1: + trial.report(float("-inf"), 1) + else: + trial.report(float("nan"), 1) + if x > 0: - trial.report(math.inf, 1) raise optuna.TrialPruned() else: return x**2