Sub topic 16
Ask-and-tell and enqueueing known trials
This chapter assumes you can write an objective function, define a search space with suggest_* methods, and run study.optimize. It covers two features that give you more control over how trials run: the ask-and-tell interface and enqueue_trial.
Why `optimize` is not always enough
study.optimize calls your objective function for you. That works when the whole evaluation fits in one Python function. It is awkward in some cases:
- The evaluation runs somewhere else, such as a job queue, a cluster scheduler or a lab instrument.
- An existing training framework owns the main loop and you cannot wrap it in a function.
- You want to inspect or log the parameters before running the evaluation.
Ask-and-tell splits the loop into two calls. study.ask() creates a trial and returns it. study.tell() reports the result for that trial.
Ask-and-tell
You call suggest_* methods on the trial returned by ask, just as you would inside an objective. You then evaluate the parameters in any way you like and pass the result to tell.
import optuna
study = optuna.create_study(direction="minimize")
for _ in range(20):
trial = study.ask()
x = trial.suggest_float("x", -10, 10)
value = (x - 2) ** 2
study.tell(trial, value)
print(study.best_params)Between ask and tell the trial stays in the RUNNING state. The sampler counts it as in progress. If you ask for several trials before telling any, you get a batch to evaluate in parallel.
You can also report a failure or a pruned trial through tell, by passing a state instead of a value:
import optuna
study = optuna.create_study(direction="minimize")
trial = study.ask()
x = trial.suggest_float("x", -10, 10)
try:
value = 1 / x
study.tell(trial, value)
except ZeroDivisionError:
study.tell(trial, state=optuna.trial.TrialState.FAIL)Enqueueing known trials
Sometimes you already know a good starting point, such as your current default settings. study.enqueue_trial adds a set of parameters to a queue. The next trial, whether it comes from optimize or from ask, uses those values instead of a sampled suggestion.
import optuna
study = optuna.create_study(direction="minimize")
study.enqueue_trial({"x": 1.0})
trial = study.ask()
x = trial.suggest_float("x", -10, 10)
study.tell(trial, (x - 2) ** 2)
print(trial.params)The printed parameters are {'x': 1.0}. The suggest_float call returns the queued value because the name "x" matches.
Queued trials are used in the order you enqueued them. They count as normal trials, so the sampler learns from their results. This makes enqueueing a cheap way to seed the search with a known baseline.
Common errors
- Parameter names in the queue must match the names in the objective. A name that does not match is ignored for that parameter, and the sampler picks a value instead.
- A queued value outside the declared range triggers a warning, and the value is used anyway.
- Forgetting
tellleaves trials in theRUNNINGstate.
Exercise
You call study.ask() three times and evaluate the three trials on separate machines. What must you do when each machine finishes, and what happens if one machine crashes and you never report on its trial?
Show answer
Call study.tell(trial, value) for each finished trial. For the crashed one, call study.tell(trial, state=optuna.trial.TrialState.FAIL). If you never report it, that trial stays in the RUNNING state and is never counted as complete.
Exercise
You enqueue {"x": 1.0} and then run study.optimize(objective, n_trials=10). Which trial uses x = 1.0, and does that trial count towards the 10?
Show answer
The first trial uses x = 1.0, because queued trials are consumed before the sampler suggests anything. It counts as one of the 10 trials.