Announcing Optuna 3.4

c-bata
Optuna
Published in
6 min readOct 17, 2023

--

We are pleased to announce the release of Optuna 3.4! Optuna 3.4 newly supports preferential optimization, an Optuna artifact, a Jupyter Lab extension, and a VS Code extension. This blog will walk you through these updates and explain how to use new features. Let’s take a closer look at these new and improved features.

✨ Highlights

Preferential Optimization

In this release, we added support for preferential optimization in Optuna Dashboard. This feature is useful when the objective function cannot be explicitly implemented but has to rely on human preferences. One example is hyperparameter optimization of a generative model. You provide a function that generates image/text/audio etc., and then you use Optuna Dashboard to report preferences to carry out the optimization.

The primary difference from human-in-the-loop optimization, which has already been supported since the last version, is whether users need to provide objective values explicitly in each trial. Explicit quantification of an objective function is often difficult for humans such as the appearance of a picture or the quality of a generated passage. On the other hand, it is surely possible to compare two of them, e.g. the quality of two pictures or generated passages. To this end, users may want to use this feature to optimize an objective function more robustly based on relative preferences.

You can also manage and analyze the reported preferences through Optuna Dashboard. For details about how to use preferential optimization, please refer to Preferential Optimization Tutorial.

Optuna Artifact

The artifact module in Optuna was introduced from v3.3. In v3.4, we are enhancing this feature and releasing it with an easy-to-understand tutorial.

The artifact module caters to the demand to save and use relatively large-sized data, such as model snapshots in hyperparameter tuning of machine learning or the chemical structure in the black box optimization for complex chemistry, that is associated with each trial. The artifact module is easy to use and has high flexibility as it allows for an effortless switch of the backend. Here’s a simple example of its usage.

import optuna


# This artifact store for file systems.
artifact_store = optuna.artifacts.FileSystemArtifactStore("/path/to/directory")
# You can use other artifact store for AWS S3 or Google cloud storage.
# artifact_store = optuna.artifacts.Boto3ArtifactStore("my-bucket")
# artifact_store = optuna.artifacts.GCSArtifactStore("my-bucket")


def objective(trial: optuna.Trial) -> float:
... = trial.suggest_float("x", -10, 10)

# Creating and writing an artifact.
file_path = generate_example(...) # This function returns some kind of file.
artifact_id = upload_artifact(
trial, file_path, artifact_store
) # The return value is the artifact ID.
trial.set_user_attr(
"artifact_id", artifact_id
) # Save the ID in RDB so that it can be referenced later.
return ...

study = optuna.create_study(study_name="test_study", storage="sqlite:///sqlite.db")
study.optimize(objective, n_trials=100)
# Loading and displaying artifacts associated with the best trial.
best_artifact_id = study.best_trial.user_attrs.get("artifact_id")
with artifact_store.open_reader(best_artifact_id) as f:
content = f.read().decode("utf-8")

print(content)

Jupyter Lab Extension

We’ve launched an official JupyterLab extension for Optuna. You can run Optuna and view the evaluation results directly within the Jupyter Lab environment, eliminating the need to boot up the Optuna Dashboard separately.

To use the extension, install jupyterlab-optuna from PyPI, click the tile to launch the extension, and input your Optuna’s storage url (e.g. sqlite:///db.sqlite3) in the dialog.

$ pip install jupyterlab jupyterlab-optuna

VS Code Extension

We’ve launched an official VS Code Extension for Optuna. To use the extension, install it via Marketplace, right-click the SQLite3 files (*.db or *.sqlite3) in the file explorer and select the “Open in Optuna Dashboard” from the dropdown menu.

This extension is powered by WebAssembly. Since it relies on SQLite3 WASM and Rust rather than on Python, it solely works within your web browser as well. To use the extension in your web browser, simply open https://optuna.github.io/optuna-dashboard and drag-and-drop your SQLite3 file onto the page.

🚀 Other Improvements

User-defined Distance for Categorical Parameters in TPE

Previously, Optuna supported only integer, float, and categorical parameters. Now, we experimentally support user-defined metric space in TPESampler. You can specify a distance function between categorical choices. Here is an example code:

import optuna
from optuna.samplers import TPESampler


def objective(trial):
word = trial.suggest_categorical("word", ["aaaaa", "aabaa", "bbaba"])
...

def hamming_distance(s1, s2):
return sum(c1 != c2 for c1, c2 in zip(s1, s2))

study = optuna.create_study(
sampler=TPESampler(categorical_distance_func={"word": hamming_distance}),
)
study.optimize(objective, n_trials=100)

By using this feature, the sampler automatically considers the user-defined structure for efficient optimization, even if the categorical search space is large. Please also refer to the PR for more details.

Constrained Optimization Support for Visualization Functions

We are continuing to develop features for constrained optimization. In Optuna 3.4, we have started supporting `plot_intermediate_values`, `plot_rank`, `plot_slice`, and `plot_timeline` for constrained optimization. These visualizations automatically use the constraint violation values specified in the sampler’s `constraint_func`.

📈 Optuna Dashboard

User-Defined Plotly’s Figure Support

Optuna Dashboard now supports user defined Plotly’s graph object. By using optuna_dashboard.save_plotly_graph_object(), you can check your Plotly’s figure via Optuna Dashboard.

from optuna_dashboard import save_plotly_graph_object

study = ...
figure = optuna.visualization.plot_optimization_history(study)
save_plotly_graph_object(study, figure)

3D Model Viewer Support

Optuna Dashboard now incorporates with Three.js, enabling users to preview 3D models (*.stl and *.3dm), that are uploaded to Optuna’s ArtifactStore. This feature was added by @hrntsm, one of the main contributors to Optuna Dashboard, to meet the needs to work on 3D CAD software with Optuna.. For more details, please read the blog post “Black-box optimization of geometry and functionality by integrating Optuna and 3D CAD”.

GitHub Sponsors

Many people from various communities participate in the development of Optuna. To support these developers and to set up a place for developers to gather, we have decided to use GitHub Sponsors! We are looking forward to your support.
https://github.com/sponsors/optuna

What’s Ahead

In Optuna v3.4, we’ve rolled out new features such as Preferential Optimization, the JupyterLab extension, and the VS Code extension. Please note that these are still in their experimental stages, and there might be scope for enhancement in both functionalities and usability.

We aim to refine these features by incorporating user feedback. Your contribution in sharing your use cases on blogs, social media, and GitHub would be very helpful for the development of Optuna v3.5.

Contributors

As with any other release, this one would not have been possible without the feedback, code, and comments from many contributors.

Alnusjaponica, HideakiImamura, RuTiO2le, YuigaWada, adjeiv, c-bata, ciffelia, contramundum53, cross32768, eukaryo, g-tamaki, g-votte, gen740, hamster-86, hrntsm, hvy, keisuke-umezawa, knshnb, lucasmrdt, louis-she, moririn2528, nabenabe0928, not522, nzw0301, ryota717, semiexp, shu65, smygw72, sousu4, torotoki, toshihikoyanase, xadrianzetx

Thanks to those who have followed the projects from the very early days and those who have joined along the way.

Next Step

Check out the release notes for more information. To get the latest news from Optuna, follow us on Twitter. For feedback, please file an issue or create a pull request on GitHub.

--

--

c-bata
Optuna

Creator of go-prompt and kube-prompt. Optuna core-dev. GitHub: c-bata