Skip to content

Run an Experiment in Jupyter Notebooks¶

Comet integrates seamlessly with hosted Jupyter Notebook services, including Google's Colab, Microsoft's Azure Notebooks, Amazon SageMaker Studio Lab, Amazon SageMaker notebook instance, and others.

Start logging data to Comet¶

You can pip install and configure the comet_ml library from any notebook using:

1
2
3
4
%pip install comet_ml --quiet

import comet_ml
comet_ml.login()

You can then create a new experiment using the Experiment class which will start the Comet auto-loggers and allow you to log custom metrics:

1
2
3
4
5
6
7
8
exp = comet_ml.start()

exp.log_metrics({"loss": 0.002, "batch_loss": 0.0019})

exp.log_parameters({"batch_size": 256, "momentum": 0.95})

# Once training is complete, you can mark the experiment as finished using
exp.end()

Please refer to Logging Experiment data for information on how to log different data types, integrations for an overview of the frameworks that Comet integrates with and corresponding functionalities, and Python SDK for a complete reference to all the methods provided by comet_ml.

Note

When you call exp.end(), Comet will know the training is completed and will log all the executed cells. This allows you to know exactly what code was run and in what order meaning every training run is reproducible even in Notebooks !

Display the Comet UI directly in a Notebook¶

Another key feature of Comet in Notebooks is the ability to display the Comet UI within a cell using the Experiment.display() method:

1
2
3
4
5
6
import comet_ml

comet_ml.login()
exp = comet_ml.start()

exp.display(tab='panels')

The following tab names are available:

panelscodeparameters
metricsgraphoutput
system-metricsinstalled-packagesnotes
graphicsaudiotext
confusion-matriceshistogramsother
htmlassetsartifacts
Comet UI in notebook
Screenshot of single experiment panels tab in the UI

End an Experiment¶

When running Comet in a Notebook, we must end your Experiment explicitly when the training is finished using the Experiment.end() method:

1
experiment.end()

When as experiment is ended, Comet will finish uploading all remaining data as well as all the code cells that where executed before returning.

Nov. 18, 2024