Skip to content

optuna/optuna-examples

Folders and files

NameName
Last commit message
Last commit date
Jan 24, 2025
Oct 6, 2023
Jun 24, 2024
Aug 16, 2024
Jun 26, 2024
Jun 25, 2024
Nov 7, 2023
Feb 1, 2023
Aug 12, 2021
Aug 9, 2024
Apr 22, 2024
May 1, 2024
Feb 15, 2024
Jun 6, 2022
Jun 24, 2024
Nov 11, 2024
Jul 24, 2024
May 1, 2024
Jun 24, 2024
Feb 28, 2024
Oct 25, 2024
Jun 12, 2024
Feb 15, 2024
Feb 15, 2024
Apr 25, 2024
Mar 3, 2023
Jun 11, 2024
May 1, 2024
Nov 1, 2024
Jun 11, 2024
Oct 26, 2024
Feb 15, 2024
Dec 6, 2024
Jun 7, 2021
Sep 14, 2022
Jul 25, 2023
Oct 30, 2024
Jun 7, 2021
Feb 15, 2024
Jun 12, 2024
May 13, 2021

Repository files navigation

Optuna Examples

This page contains a list of example codes written with Optuna.

Simplest Codeblock
import optuna


def objective(trial):
    x = trial.suggest_float("x", -100, 100)
    return x ** 2


if __name__ == "__main__":
    study = optuna.create_study()
    # The optimization finishes after evaluating 1000 times or 3 seconds.
    study.optimize(objective, n_trials=1000, timeout=3)
    print(f"Best params is {study.best_params} with value {study.best_value}")

Note

If you are interested in a quick start of Optuna Dashboard with in-memory storage, please take a look at this example.

Tip

Couldn't find your usecase? FAQ might be helpful for you to implement what you want. In this example repository, you can also find the examples for the following scenarios:

  1. Objective function with additional arguments, which is useful when you would like to pass arguments besides trial to your objective function.

  2. Manually provide trials with sampler, which is useful when you would like to force certain parameters to be sampled.

  3. Callback to control the termination criterion of study, which is useful when you would like to define your own termination criterion other than n_trials or timeout.

Examples for Diverse Problem Setups

Here are the URLs to the example codeblocks to the corresponding setups.

Simple Black-box Optimization
Multi-Objective Optimization
Machine Learning (Incl. LightGBMTuner and OptunaSearchCV)

If you are looking for an example of reinforcement learning, please take a look at the following:

Pruning

The following example demonstrates how to implement pruning logic with Optuna.

In addition, integration modules are available for the following libraries, providing simpler interfaces to utilize pruning.

Samplers

If you are interested in defining a user-defined sampler, here is an example:

Terminator
Visualization
Distributed Optimization
MLOps Platform
External Projects Using Optuna

Important

PRs to add additional real-world examples or projects are welcome!

Running with Optuna's Docker images?

Our Docker images for most examples are available with the tag ending with -dev. For example, PyTorch Simple can be run via:

$ docker run --rm -v $(pwd):/prj -w /prj optuna/optuna:py3.11-dev python pytorch/pytorch_simple.py

Additionally, our visualization example can also be run on Jupyter Notebook by opening localhost:8888 in your browser after executing the following:

$ docker run -p 8888:8888 --rm optuna/optuna:py3.11-dev jupyter notebook --allow-root --no-browser --port 8888 --ip 0.0.0.0 --NotebookApp.token='' --NotebookApp.password=''