Skip to content

Integrate with Optuna¶

Optuna is an open source hyperparameter optimization framework to automate hyperparameter search.

Instrument Optuna with Comet to start managing experiments and track hyperparameters for faster and easier reproducibility and collaboration.

Open In Colab

Comet SDKMinimum SDK versionMinimum optuna version
Python-SDK3.44.04.0.0

Comet + Optuna dashboard

Start logging¶

Connect Comet to your existing code by using our CometCallback.

Add the following lines of code to your script or notebook:

import optuna
from optuna_integration.comet import CometCallback

study = optuna.create_study()
comet = CometCallback(
    study, project_name="comet-example-optuna-hello-world", metric_names=["score"]
)


@comet.track_in_comet()
def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    objective = (x - 2) ** 2

    return objective


study.optimize(objective, n_trials=20, callbacks=[comet])

Note

There are other ways to configure Comet. See more here.

Log automatically¶

The Comet Optuna integration will creates several Experiments:

  • One Experiment for the Optuna study:
    • The Study Name
    • The Study Storage name
    • The best trials number as a JSON Asset
  • One Experiment per trial which will logs:
    • The Trial hyperparameters combination
    • The Trial number
    • The tracked Metric value of the trial

You can easily turn the automatic logging on and off for any or all items. See Configure Comet for Optuna for more details.

Note

Don't see what you need to log here? We have your back. You can manually log any kind of data to Comet using the Experiment object. For example, use experiment.log_image to log images, or experiment.log_audio to log audio.

End-to-end example¶

Following is a basic example of using Comet with Optuna.

If you can't wait, check out the results of this example Optuna project for a preview of what's to come.

Install dependencies¶

python -m pip install "comet_ml>=3.33.10" "optuna>=4.0.0" "optuna-integration>=4.0.0"

Run the example¶

# coding: utf-8
from comet_ml import login

import optuna
from optuna_integration.comet import CometCallback

# Login to Comet if needed
login()

study = optuna.create_study()
comet = CometCallback(
    study, project_name="comet-example-optuna-hello-world", metric_names=["score"]
)


@comet.track_in_comet()
def objective(trial):
    x = trial.suggest_float("x", -10, 10)
    objective = (x - 2) ** 2

    return objective


study.optimize(objective, n_trials=20, callbacks=[comet])

best_params = study.best_params
found_x = best_params["x"]
print("Found x: {}, (x - 2)^2: {}".format(found_x, (found_x - 2) ** 2))

Try it out!¶

Don't just take our word for it, try it out for yourself.

Configure Comet for Optuna¶

See the Optuna reference doc for configuration options.

For more information about configuring Comet, see Configure Comet.

Dec. 17, 2024