optuna_integration.CometCallback

class optuna_integration.CometCallback(study, workspace=None, project_name='general', metric_names=None)[source]

A callback for logging Optuna study trials to a Comet ML Experiment. Comet ML must be installed to run.

This callback is intended for use with Optuna’s study.optimize() method. It ensures that all trials from an Optuna study are logged to a single Comet Experiment, facilitating organized tracking of hyperparameter optimization. The callback supports both single and multi-objective optimization.

In a distributed training context, where trials from the same study might occur on different machines, this callback ensures consistency by logging to the same Comet Experiment using an experiment key stored within the study’s user attributes.

By default, Trials are logged as Comet Experiments, which will automatically log code, system metrics, and many other values. However, it also adds some computational overhead (potentially a few seconds).

Parameters:
  • study (optuna.study.Study) – The Optuna study object to which the callback is attached.

  • workspace (str | None) – The workspace in Comet ML where the project resides.

  • project_name (str | None) – The name of the project in Comet ML where the experiment will be logged. Defaults to general.

  • metric_names (Sequence[str] | None) – A list of the names of your objective metrics.

Example

Here is an example.

study = optuna.create_study(directions=["maximize", "maximize"])
comet_callback = CometCallback(
    study,
    metric_names=["accuracy", "top_k_accuracy"],
    project_name="your_project_name",
    workspace="your_workspace",
)
study.optimize(your_objective_function, n_trials=100, callbacks=[comet_callback])

Note

Added in v4.0.0 as an experimental feature. The interface may change in newer versions without prior notice. See https://github.com/optuna/optuna/releases/tag/v4.0.0.

Methods

track_in_comet()