Search code examples
machine-learningoptuna

How can I change an Optuna's trial result?


I'm using optuna on a complex ML algorithm, where each trial takes around 3/4 days. After a couple of trials, I noticed that the values that I was returning to optuna were incorrect, but I do have the correct results on another file (saving as a backup). Is there any way I could change this defectives results directly in the study object?

I know I can export the study in a pandas dataframe using study.trials_dataframe() and then change it there? However, I need to visualize it in optuna-dashboard, so I would need to directly change it in the study file. Any suggestions?


Solution

  • Create a new Study, use create_trial to create trials with correct values and use Study.add_trials to insert them into the new study.

    old_trials = old_study.get_trials(deepcopy=False)
    correct_trials = [
        optuna.trial.create_trial(
            params=trial.params,
            distributions=trial.distributions,
            value=correct_value(trial.params)
        ) for trial in old_trials]
    new_study = optuna.create_study(...)
    new_study.add_trials(correct_trials)
    

    Note that Optuna doesn't allow you to change existing trials once they are finished, i.e., successfully returned a value, got pruned, or failed. (This is an intentional design; Optuna uses caching mechanisms intensively and we don't want to have inconsistencies during distributed optimization.) You can only create a new study containing correct trials, and optionally delete the old study.