Sub topic 15
Optimising more than one objective
This chapter assumes you can write an Optuna objective function and run a study. You should also know what study.best_trial returns for a single objective.
Why a single number is not always enough
Real models often have competing goals. A more accurate model may be slower. A smaller model may be less accurate. Collapsing these into one weighted score hides the trade-off and forces you to pick the weights before you have seen the options.
Optuna supports multi-objective optimisation. The objective returns several values, and the study looks for trials that cannot be improved on one goal without getting worse on another.
Pareto optimality
A trial A dominates trial B if A is at least as good on every objective and strictly better on at least one. The Pareto front is the set of trials that no other trial dominates. Each trial on the front is a different trade-off, and none is strictly better than the others.
Running a multi-objective study
Pass directions instead of direction. Give one direction per returned value.
import optuna
def objective(trial):
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)
return x ** 2 + y, (x - 2) ** 2 + (y - 1) ** 2
study = optuna.create_study(directions=["minimize", "minimize"])
study.optimize(objective, n_trials=50)
for t in study.best_trials:
print(t.values, t.params)The order of directions matches the order of the returned values. Here both are minimised. You could also mix them, for example ["maximize", "minimize"] for accuracy and latency.
Reading the results
With several objectives, study.best_trial and study.best_value raise an error, because "best" is no longer defined. Use these instead:
study.best_trialsreturns a list of the Pareto-optimal trials.trial.valuesholds one value per objective, in order.study.trialsstill lists every trial.
To choose a final model, apply a rule that suits your project. For example, take the most accurate trial whose latency is under a limit.
fast_enough = [t for t in study.best_trials if t.values[1] < 1.0]
chosen = min(fast_enough, key=lambda t: t.values[0]) if fast_enough else None
print(chosen.params if chosen else "no trial met the limit")Differences from single-objective studies
- Samplers. The default sampler for multi-objective studies is
NSGAIISampler, an evolutionary method.TPESampleralso supports multiple objectives. - Pruning. Pruners do not support multi-objective studies. Calling
trial.reportortrial.should_prunein such a study raises an error, so leave pruning out. - Visualisation.
optuna.visualization.plot_pareto_front(study)plots the front. Passtarget_namesto label the axes.
Common mistakes
- Giving
directionsa different length from the number of returned values. - Returning objectives on very different scales and then trying to merge them without thought. Keep them separate and decide afterwards.
- Treating the front as final. It is estimated from the trials you ran, so more trials can move it.
Exercise
Your objective returns (validation accuracy, inference time in milliseconds). Which directions argument should you pass to create_study, and which attribute gives you the set of best trade-offs?
Show answer
Pass directions=["maximize", "minimize"], because accuracy should go up and time should go down. Read the Pareto-optimal trials from study.best_trials, not study.best_trial.