据我所知,目前没有直接的方法可以处理您的案件。作为一种解决方法,您可以检查参数重复并跳过评估,如下所示:
import optuna
def objective(trial: optuna.Trial):
# Sample parameters.
x = trial.suggest_int('x', 0, 10)
y = trial.suggest_categorical('y', [-10, -5, 0, 5, 10])
# Check duplication and skip if it's detected.
for t in trial.study.trials:
if t.state != optuna.structs.TrialState.COMPLETE:
continue
if t.params == trial.params:
return t.value # Return the previous value without re-evaluating it.
# # Note that if duplicate parameter sets are suggested too frequently,
# # you can use the pruning mechanism of Optuna to mitigate the problem.
# # By raising `TrialPruned` instead of just returning the previous value,
# # the sampler is more likely to avoid sampling the parameters in the succeeding trials.
#
# raise optuna.structs.TrialPruned('Duplicate parameter set')
# Evaluate parameters.
return x + y
# Start study.
study = optuna.create_study()
unique_trials = 20
while unique_trials > len(set(str(t.params) for t in study.trials)):
study.optimize(objective, n_trials=1)