Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_best_intermediate_result_over_steps(direction_expected):
# type: (Tuple[StudyDirection, float]) -> None
direction, expected = direction_expected
if direction == StudyDirection.MINIMIZE:
study = optuna.study.create_study(direction="minimize")
else:
study = optuna.study.create_study(direction="maximize")
# FrozenTrial.intermediate_values has no elements.
trial_id_empty = study._storage.create_new_trial(study._study_id)
trial_empty = study._storage.get_trial(trial_id_empty)
with pytest.raises(ValueError):
percentile._get_best_intermediate_result_over_steps(trial_empty, direction)
# Input value has no NaNs but float values.
trial_id_float = study._storage.create_new_trial(study._study_id)
trial_float = optuna.trial.Trial(study, trial_id_float)
trial_float.report(0.1, step=0)
trial_float.report(0.2, step=1)
summaries = sorted(summaries)
expected_summary_1 = StudySummary(
study_id=study_id_1,
study_name=storage.get_study_name_from_id(study_id_1),
direction=StudyDirection.MINIMIZE,
user_attrs={},
system_attrs={},
best_trial=summaries[0].best_trial, # This always passes.
n_trials=2,
datetime_start=summaries[0].datetime_start # This always passes.
)
expected_summary_2 = StudySummary(
study_id=study_id_2,
study_name=storage.get_study_name_from_id(study_id_2),
direction=StudyDirection.MAXIMIZE,
user_attrs={},
system_attrs={},
best_trial=summaries[1].best_trial, # This always passes.
n_trials=2,
datetime_start=summaries[1].datetime_start # This always passes.
)
expected_summary_3 = StudySummary(
study_id=study_id_3,
study_name=storage.get_study_name_from_id(study_id_3),
direction=StudyDirection.NOT_SET,
user_attrs={},
system_attrs={},
best_trial=None,
n_trials=0,
datetime_start=None)
def set_study_direction(self, study_id, direction):
# type: (int, structs.StudyDirection) -> None
session = self.scoped_session()
study = models.StudyModel.find_or_raise_by_id(study_id, session)
if study.direction != structs.StudyDirection.NOT_SET and study.direction != direction:
raise ValueError('Cannot overwrite study direction from {} to {}.'.format(
study.direction, direction))
study.direction = direction
self._commit(session)
SCHEMA_VERSION = 11
MAX_INDEXED_STRING_LENGTH = 512
MAX_STRING_LENGTH = 2048
MAX_VERSION_LENGTH = 256
NOT_FOUND_MSG = 'Record does not exist.'
BaseModel = declarative_base() # type: Any
class StudyModel(BaseModel):
__tablename__ = 'studies'
study_id = Column(Integer, primary_key=True)
study_name = Column(String(MAX_INDEXED_STRING_LENGTH), index=True, unique=True, nullable=False)
direction = Column(Enum(StudyDirection), nullable=False)
@classmethod
def find_by_id(cls, study_id, session):
# type: (int, orm.Session) -> Optional[StudyModel]
study = session.query(cls).filter(cls.study_id == study_id).one_or_none()
return study
@classmethod
def find_or_raise_by_id(cls, study_id, session):
# type: (int, orm.Session) -> StudyModel
study = cls.find_by_id(study_id, session)
if study is None:
raise ValueError(NOT_FOUND_MSG)
"creating a new one.".format(study_name))
study_id = storage.get_study_id_from_name(study_name)
else:
raise
study_name = storage.get_study_name_from_id(study_id)
study = Study(
study_name=study_name,
storage=storage,
sampler=sampler,
pruner=pruner)
if direction == 'minimize':
_direction = structs.StudyDirection.MINIMIZE
elif direction == 'maximize':
_direction = structs.StudyDirection.MAXIMIZE
else:
raise ValueError('Please set either \'minimize\' or \'maximize\' to direction.')
study._storage.set_study_direction(study_id, _direction)
return study
pruner=None, # type: pruners.BasePruner
direction='minimize', # type: str
):
# type: (...) -> None
self.study_name = study_name
self.storage = storages.get_storage(storage)
self.sampler = sampler or samplers.TPESampler()
self.pruner = pruner or pruners.MedianPruner()
self.study_id = self.storage.get_study_id_from_name(study_name)
self.logger = logging.get_logger(__name__)
self.save_distribution = True
if direction == 'minimize':
_direction = structs.StudyDirection.MINIMIZE
elif direction == 'maximize':
_direction = structs.StudyDirection.MAXIMIZE
else:
raise ValueError('Please set either \'minimize\' or \'maximize\' to direction.')
# TODO(Yanase): Implement maximization.
if _direction == structs.StudyDirection.MAXIMIZE:
raise ValueError(
'Optimization direction of study {} is set to `MAXIMIZE`. '
'Currently, Optuna supports `MINIMIZE` only.'.format(study_name))
self.storage.set_study_direction(self.study_id, _direction)
def a_better_equal_b(a, b, direction):
if direction == structs.StudyDirection.MAXIMIZE:
return a >= b
return a <= b
step_value = trial.value
if math.isnan(step_value):
return True
best_intermediate_value = _get_best_intermediate_step_value(all_trials, direction, step)
if math.isnan(best_intermediate_value):
return False
if a_better_equal_b(step_value, best_intermediate_value, direction):
return False
n_steps_back = self._n_steps_back
n_steps_forward = self._n_steps_forward
percentage_from_best = self._percentage_from_best
if step == 0:
if direction == structs.StudyDirection.MINIMIZE:
percentage_from_best = 1.0 + percentage_from_best
perc_best_value = best_intermediate_value * percentage_from_best
if a_better_equal_b(step_value, perc_best_value, direction):
return False
else:
return True
if step <= n_steps_back:
start_step = 0
else:
start_step = step - n_steps_back
step_inc = _get_extrapolated_step_increment(start_step, step, intermediate_values)
extrapolated_value = step_value + (n_steps_forward * step_inc)
if a_better_equal_b(extrapolated_value, best_intermediate_value, direction):
return False
def _transition_probability(self, study, prev_trial):
if self._current_trial is None:
return 1.0
prev_value = prev_trial.value
current_value = self._current_trial.value
# `prev_trial` is always accepted if it has a better value than the current trial.
if study.direction == structs.StudyDirection.MINIMIZE and prev_value <= current_value:
return 1.0
elif study.direction == structs.StudyDirection.MAXIMIZE and prev_value >= current_value:
return 1.0
# Calculate the probability of accepting `prev_trial` that has a worse value than
# the current trial.
return np.exp(-abs(current_value - prev_value) / self._temperature)
'range': (min(values), max(values))
}
if is_categorical:
dim['tickvals'] = list(range(len(vocab)))
dim['ticktext'] = list(sorted(vocab.items(), key=lambda x: x[1]))
dims.append(dim)
traces = [
go.Parcoords(
dimensions=dims,
line={
'color': dims[0]['values'],
'colorscale': 'blues',
'colorbar': {'title': 'Objective Value'},
'showscale': True,
'reversescale': study.direction == StudyDirection.MINIMIZE,
}
)
]
figure = go.Figure(data=traces, layout=layout)
return figure