Skip to content

Create & Run an Experiment

When you create and run an Experiment with Comet, you unlock the ability to interactively analyze the Experiment performance and manage its state over time.

Comet Single Experiment Page
The Comet Single Experiment page

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.

Log metrics, parameters, and assets to your 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
17
18
19
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 a parameter
exp.log_parameter('my_parameter', 0.001)

# 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, my_parameter, 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).

May. 17, 2024