Edit on GitHub

Optuna

DVCLive allows you to add experiment tracking capabilities to your Optuna projects.

Usage

Include the DVCLiveCallback in the callbacks list passed to study.optimize:

from dvclive.optuna import DVCLiveCallback

...

study.optimize(
    objective, n_trials=7, callbacks=[DVCLiveCallback()])

If you are using an ML Framework inside the objective function, you can instead use the corresponding DVCLive integration for the ML Framework. See the example bellow.

Each trial will create a DVC Experiment, tracking the associated metrics and parameters.

Parameters

  • metric_name - (metric by default) - Name assigned to the metric to be optimized.

  • **kwargs - Any additional arguments will be used to instantiate a new Live instance.

Examples

Optuna callback

import optuna

from dvclive.optuna import DVCLiveCallback

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

study = optuna.create_study()

study.optimize(
    objective, n_trials=7, callbacks=[DVCLiveCallback()])

Optuna with ML Framework

In the Optuna and Keras example you can use the dvclive.keras callback:

from dvclive import Live
from dvclive.keras import DVCLiveCallback

...

with Live(save_dvc_exp=True) as live:
    live.log_params(trial.params)
    model.fit(
        x_train,
        y_train,
        validation_data=(x_valid, y_valid),
        shuffle=True,
        batch_size=BATCHSIZE,
        epochs=EPOCHS,
        verbose=False,
        callbacks=[DVCLiveCallback(live=live)]
    )
Content

🐛 Found an issue? Let us know! Or fix it:

Edit on GitHub

Have a question? Join our chat, we will help you:

Discord Chat