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_successive_halving_pruner_intermediate_values(direction_value):
# type: (Tuple[str, float]) -> None
direction, intermediate_value = direction_value
pruner = optuna.pruners.SuccessiveHalvingPruner(
min_resource=1, reduction_factor=2, min_early_stopping_rate=0)
study = optuna.study.create_study(direction=direction)
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, 1)
# A pruner is not activated at a first trial.
assert not pruner.prune(study=study, trial=study._storage.get_trial(trial._trial_id))
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
# A pruner is not activated if a trial has no intermediate values.
assert not pruner.prune(study=study, trial=study._storage.get_trial(trial._trial_id))
trial.report(intermediate_value, 1)
# A pruner is activated if a trial has an intermediate value.
assert pruner.prune(study=study, trial=study._storage.get_trial(trial._trial_id))
def _create_new_chainermn_trial(study, comm):
# type: (Study, CommunicatorBase) -> integration.chainermn.ChainerMNTrial
if comm.rank == 0:
trial_id = study._storage.create_new_trial(study._study_id)
trial = Trial(study, trial_id)
mn_trial = integration.chainermn.ChainerMNTrial(trial, comm)
else:
mn_trial = integration.chainermn.ChainerMNTrial(None, comm)
comm.mpi_comm.barrier()
return mn_trial
def test_median_pruner_n_warmup_steps():
# type: () -> None
pruner = optuna.pruners.MedianPruner(0, 1)
study = optuna.study.create_study()
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, 1)
trial.report(1, 2)
study._storage.set_trial_state(trial._trial_id, TrialState.COMPLETE)
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(2, 1)
# A pruner is not activated during warm-up steps.
assert not pruner.prune(
study=study, trial=study._storage.get_trial(trial._trial_id))
trial.report(2, 2)
# A pruner is activated after warm-up steps.
assert pruner.prune(
study=study, trial=study._storage.get_trial(trial._trial_id))
def test_successive_halving_pruner_intermediate_values():
# type: () -> None
pruner = optuna.pruners.SuccessiveHalvingPruner(
min_resource=1, reduction_factor=2, min_early_stopping_rate=0)
study = optuna.study.create_study()
trial = optuna.trial.Trial(study, study.storage.create_new_trial_id(study.study_id))
trial.report(1, 1)
# A pruner is not activated at a first trial.
assert not pruner.prune(study.storage, study.study_id, trial.trial_id, step=1)
trial = optuna.trial.Trial(study, study.storage.create_new_trial_id(study.study_id))
# A pruner is not activated if a trial has no intermediate values.
assert not pruner.prune(study.storage, study.study_id, trial.trial_id, step=1)
trial.report(2, 1)
# A pruner is activated if a trial has an intermediate value.
assert pruner.prune(study.storage, study.study_id, trial.trial_id, step=1)
def test_percentile_pruner_with_one_trial():
# type: () -> None
study = optuna.study.create_study()
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, 1)
pruner = optuna.pruners.PercentilePruner(25.0, 0, 0)
# A pruner is not activated at a first trial.
assert not pruner.prune(
study=study, trial=study._storage.get_trial(trial._trial_id))
def test_nop_pruner():
# type: () -> None
study = optuna.study.create_study()
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, 1)
pruner = optuna.pruners.NopPruner()
# A NopPruner instance is always deactivated.
assert not pruner.prune(study=study, trial=study.trials[0])
def test_successive_halving_pruner_min_early_stopping_rate_parameter():
# type: () -> None
study = optuna.study.create_study()
# min_early_stopping_rate=-1: Error (must be `min_early_stopping_rate >= 0`).
with pytest.raises(ValueError):
optuna.pruners.SuccessiveHalvingPruner(
min_resource=1, reduction_factor=2, min_early_stopping_rate=-1)
# min_early_stopping_rate=0: The rung 0 ends at step 1.
pruner = optuna.pruners.SuccessiveHalvingPruner(
min_resource=1, reduction_factor=2, min_early_stopping_rate=0)
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, step=1)
assert not pruner.prune(study=study, trial=study._storage.get_trial(trial._trial_id))
assert 'completed_rung_0' in trial.system_attrs
# min_early_stopping_rate=1: The rung 0 ends at step 2.
pruner = optuna.pruners.SuccessiveHalvingPruner(
min_resource=1, reduction_factor=2, min_early_stopping_rate=1)
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, step=1)
assert not pruner.prune(study=study, trial=study._storage.get_trial(trial._trial_id))
assert 'completed_rung_0' not in trial.system_attrs
assert 'completed_rung_1' not in trial.system_attrs
trial.report(1, step=2)
def test_median_pruner_intermediate_values_nan():
# type: () -> None
pruner = optuna.pruners.MedianPruner(0, 0)
study = optuna.study.create_study()
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(float('nan'), 1)
# A pruner is not activated if the study does not have any previous trials.
assert not pruner.prune(
study=study, trial=study._storage.get_trial(trial._trial_id))
study._storage.set_trial_state(trial._trial_id, TrialState.COMPLETE)
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(float('nan'), 1)
# A pruner is activated if the best intermediate value of this trial is NaN.
assert pruner.prune(
study=study, trial=study._storage.get_trial(trial._trial_id))
study._storage.set_trial_state(trial._trial_id, TrialState.COMPLETE)
trial = optuna.trial.Trial(study, study._storage.create_new_trial(study._study_id))
trial.report(1, 1)
# A pruner is not activated if the median intermediate value is NaN.
assert not pruner.prune(
study=study, trial=study._storage.get_trial(trial._trial_id))
def create_trial():
# type: () -> Trial
return Trial(study, study._storage.create_new_trial(study._study_id))