Skip to content

Create & Run an Experiment

An Experiment is Comet's logical unit for any research workflow that you want to track and manage. Once you have created an Experiment, you will be able to track it's performance in the Comet platform.

Comet dashboard - Overview
Comet Experiment

Overview: Experiment Management with Comet

For your experiments, the Comet SDK offers:

  • With the Experiment object:
    • Automated logging of built-in and custom attributes within the Experiment object. Please refer to the Logging Overview for more information.
    • Support for a variety of execution flows, including online and offline execution with start, stop, and resume. Please refer to Resume an Experiment for details on the latter.
  • With the API object:
    • Ability to fetch any data previously logged to Comet for any experiment, project, and workspace.
    • Management functionalities to create, get, delete, and update experiments, projects, and workspaces.

Note

While the Experiment object is used for logging data during a new or resumed training, you can use the APIExperiment object for updating and getting experiment attributes in a one-off manner.

The Comet UI lets you analyze and manage experiments, with all associated logged attributes, within user-friendly interfaces that you can easily share with your team members

Create an Experiment

When you create an Experiment, Comet instantly begins tracking all attributes associated to the code execution at the url displayed after Experiment creation.

You can create an experiment by running:

1
2
3
4
import comet_ml

comet_ml.init()
exp = comet_ml.Experiment()

The comet_ml.init() command will prompt you for your API key if it hasn't been saved already. You can learn about how to configure your API key in Configure the Comet SDK.

If you don't specify any arguments, Comet creates the experiment if your default workspace and the general project.

Alternatively, you can pass relevant arguments to the comet_ml.Experiment() method, e.g. to specify project name and workspace where to create the experiment as well as the automated Comet logging to set up for it.

Run an Experiment

Once an Experiment has been started, Comet will automatically start logging:

  • Metrics and hyper-parameters thanks to our many integrations.
  • The code file your Experiment was created in.
  • The git commit and git patch (uncommitted files) if you are running within a git repository.
  • Console output logs.
  • Installed python packages.
  • System metrics (Memory usage, GPU usage, etc).

In addition to all the data Comet logs for you, you can log any other types of data to the Comet platform using one of our logging methods.

In the example below we log a metric and an image:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import comet_ml
from PIL import Image
import requests

comet_ml.init()
exp = comet_ml.Experiment()

# Log a metric
exp.log_metric('my_metric', 0.99)

# Log an image
image_url = "https://cdn.pixabay.com/photo/2016/12/04/21/58/rabbit-1882699_1280.jpg"
response = requests.get(image_url)
pil_image = Image.open(io.BytesIO(response.content))

exp.log_image(image_data=pil_image, name="example_pil")

Comet automatically associates my_metric and my_image.png to the Experiment represented by the exp object.

Tip

You can use the exp.test() and the exp.train() methods to specify a context to the logged attributes (e.g., a metric logged in the train context is saved with a "train " prefix).

Apr. 29, 2024