How to optimize for multiple metrics in Optuna
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Many tuning problems have more than one target. You may want the highest validation score, but you may also care about latency, model size, or training cost. Optuna supports this directly with multi-objective studies, where the goal is to discover a Pareto front instead of a single best trial.
Define Objectives Explicitly
In a multi-objective study, the objective function returns one value per metric. You also tell Optuna whether each metric should be minimized or maximized.
This example minimizes training time and minimizes error rate for a random forest model:
The important difference from single-objective tuning is the return value. Instead of returning one scalar, the objective returns a tuple with one value per metric.
Read the Pareto Front
There is no single "best" trial unless you choose one trade-off yourself. Optuna stores the non-dominated trials in study.best_trials.
Each trial on the Pareto front is competitive in a different way. One may be slightly slower but much more accurate. Another may be fast enough for production while giving up a small amount of quality. That is the real value of multi-objective optimization: it exposes choices instead of hiding them behind one aggregate score.
If you want a visual view of the trade-off, Optuna also provides a Pareto plot:
Choose Metrics That Reflect Real Constraints
A common mistake is optimizing metrics that do not match the production problem. For example, accuracy and training time are fine for a tutorial, but a real system might care about:
- validation loss and inference latency
- recall and model size
- revenue impact and false-positive rate
If one metric is only a soft preference, you can still use multi-objective search, then apply a business rule after tuning. For instance, you might filter to all trials with latency below 30 milliseconds and then choose the most accurate one from that filtered set.
Another practical detail is direction. Metrics such as error, loss, latency, and memory use are usually minimized. Metrics such as accuracy, F1, and AUC are usually maximized. If the directions list is wrong, the study will optimize the opposite of what you want.
When to Combine Metrics Instead
Multi-objective optimization is useful when trade-offs are real and you do not want to force them into one number too early. It is not always the right tool. If you already know that one metric must dominate and others are just penalties, a single weighted score can be simpler.
For example, if latency is only relevant above a threshold, you can encode that directly in one scalar objective. But if you genuinely want to explore the frontier between speed and quality, keeping the objectives separate is more informative and usually easier to reason about later.
Common Pitfalls
The first pitfall is expecting study.best_trial to work like a single-objective study. Multi-objective studies usually use study.best_trials, because there are multiple non-dominated candidates.
Another problem is mixing metrics with unstable evaluation noise. If one objective varies wildly across runs, the Pareto front becomes difficult to trust. Use fixed seeds, stable validation splits, or repeated evaluation where practical.
It is also easy to choose too many objectives. Two or three metrics are manageable. Past that, the search space and decision process become much harder to interpret.
Finally, do not forget deployment constraints. A beautiful Pareto front on offline validation may still contain models that are too large, too slow, or too expensive to serve in the real system.
Summary
- In Optuna, multi-objective optimization means returning multiple values from the objective function.
- '
create_study(directions=[...])defines whether each metric should be minimized or maximized.' - The result is a Pareto front, not one universally best trial.
- '
study.best_trialshelps you inspect the non-dominated candidates.' - Use separate objectives when trade-offs are real and worth preserving.
Related reading
- How to output per-class accuracy in Keras?
- How to output the second layer of a network?
- How to overcome overfitting in CNN - standard methods don't work
- How to overcome overfitting in convolutional neural network when nothing helps?
- How to optimize MAPE code in Python?
- How to optimize quicksort
- How to overwrite Spark ML model in PySpark?
- How to parallelize a training loop ever samples of a batch when CPU is only available in pytorch?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.