optuna/optuna-examples
Examples for https://github.com/optuna/optuna
What it is: A collection of example Python scripts that show how to use Optuna — an open-source library that automatically searches for the best hyperparameters (like learning rates, model sizes, etc.) for your machine learning models or other numerical tasks.
Core idea: You define an objective function that returns a score you want to optimize (minimize or maximize). Optuna then repeatedly picks trial values for the hyperparameters and runs your function. It learns from past trials to propose smarter values, aiming to find the best score within a given budget (number of trials or time).
The simplest example from the README:
import optuna
def objective(trial):
x = trial.suggest_float("x", -100, 100)
return x ** 2
study = optuna.create_study()
study.optimize(objective, n_trials=1000, timeout=3) # stop after 1000 trials or 3 seconds
print(f"Best params is {study.best_params} with value {study.best_value}")
Here, trial.suggest_float("x", -100, 100) tells Optuna to try values of x in that range. The objective returns x², so Optuna should find x≈0 to get the minimum.
What this repository contains:
- Diverse examples for many use cases: simple black‑box optimization, multi‑objective optimization, machine learning frameworks (PyTorch, TensorFlow, LightGBM, scikit‑learn, etc.), pruning (stopping bad trials early), custom samplers, distributed optimization, visualization, and MLOps integrations.
- Specific code snippets for integration with libraries like Catalyst, CatBoost, Dask, FastAI, Keras, Hugging Face Transformers, and more.
- Links to external projects that already use Optuna (e.g., Hugging Face Trainer, Hydra Optuna Sweeper, PyKEEN).
- A few FAQ‑style examples for passing extra arguments to the objective, manually enqueueing trials, and custom termination callbacks.
How to run:
Most examples can be executed directly with Python (just run the script). For convenience, the repository provides Docker images (tagged -dev). For example, to run the PyTorch example:
docker run --rm -v $(pwd):/prj -w /prj optuna/optuna:py3.11-dev python pytorch/pytorch_simple.py
There is also a Docker command to start a Jupyter notebook for the visualization examples.
Related
- Project
- Project
- Project
- Project