lundi 18 janvier 2021

How to make optuna suggest (different) values few times in a row and use only the last suggested value?

Here is the basic code example:

import optuna

def objective(trial):
    i=0
    while i < 5:
        x = trial.suggest_uniform('x', -10, 10)
        c = trial.suggest_categorical('c',['dave','masha'])
        print('x =',x,'; c =',c)
        i +=1
    return (x - 2) ** 2

study = optuna.create_study()
study.optimize(objective, n_trials=1)

When I run it I get this:

[I 2021-01-18 15:54:35,991] A new study created in memory with name: no-name-dede52a4-81d4-494d-9461-79d6bc45092c
[I 2021-01-18 15:54:35,993] Trial 0 finished with value: 1.1393024707748967 and parameters: {'x': 0.9326188727662004, 'c': 'dave'}. Best is trial 0 with value: 1.1393024707748967.
x = 0.9326188727662004 ; c = dave
x = 0.9326188727662004 ; c = dave
x = 0.9326188727662004 ; c = dave
x = 0.9326188727662004 ; c = dave
x = 0.9326188727662004 ; c = dave

As you see, trial somehow sticks to the value it initially suggested. I would like trial to suggest new value each time trial.suggest... is called. There must be a way of doing it, but I have not figured it out yet. Could anybody point me at the solution?




Aucun commentaire:

Enregistrer un commentaire