Comet ML
Comet ML is an MLOps platform for tracking, comparing, debugging, and optimizing machine learning models. It provides a comprehensive dashboard to visualize experiments, log code, metrics, hyperparameters, and artifacts. The current version is 3.57.3, and it receives frequent updates with new features and bug fixes.
Warnings
- gotcha Comet API Key and Workspace configuration: Hardcoding API keys directly in scripts is a security risk. Best practice is to use environment variables (`COMET_API_KEY`, `COMET_WORKSPACE`), a `.comet.config` file, or `comet_ml.login()` for interactive setup.
- gotcha Experiment lifecycle management: Forgetting to end an experiment (e.g., in case of script errors) can lead to hanging processes or incomplete experiment data. Using the `Experiment` class as a context manager (`with Experiment(...) as experiment:`) automatically handles experiment termination.
- breaking Python 2.x support dropped: Comet ML version 3.x and above requires Python 3.8 or higher. Attempts to use it with older Python versions will result in `ModuleNotFoundError` or `SyntaxError`.
- gotcha Automatic logging features: Comet ML automatically logs many aspects (e.g., environment details, code, git data, common metrics like loss/accuracy from popular ML frameworks). This can be helpful but might log more than desired or conflict with manual logging if not understood. For example, `auto_output_logging` can capture stdout/stderr.
Install
-
pip install comet_ml
Imports
- Experiment
from comet_ml import Experiment
- init
import comet_ml; comet_ml.init()
- log_parameters
experiment.log_parameters({'param': value})
Quickstart
import os
from comet_ml import Experiment
# Ensure COMET_API_KEY and COMET_WORKSPACE are set as environment variables
# or use comet_ml.login() if you prefer interactive login.
# For example: os.environ['COMET_API_KEY'] = 'YOUR_API_KEY'
# os.environ['COMET_WORKSPACE'] = 'YOUR_WORKSPACE'
# Or, for local testing without an explicit API key (results only stored locally):
# experiment = Experiment(project_name="my-test-project", log_code=False, display_summary_to_terminal=False)
# Initialize an experiment
# It's best practice to use a context manager to ensure the experiment terminates correctly
with Experiment(project_name="my-quickstart-project",
api_key=os.environ.get('COMET_API_KEY', None),
workspace=os.environ.get('COMET_WORKSPACE', None),
auto_output_logging='simple', # capture print statements
auto_metric_logging=True, # capture common metrics
log_code=True # logs your script code
) as experiment:
# Log hyperparameters
hyper_params = {"learning_rate": 0.001, "epochs": 10, "batch_size": 32}
experiment.log_parameters(hyper_params)
# Simulate a training loop
for epoch in range(hyper_params["epochs"]):
# Simulate metric calculation
accuracy = 0.5 + (epoch * 0.05) + (hyper_params["learning_rate"] * 100)
loss = 1.0 - (epoch * 0.08) - (hyper_params["learning_rate"] * 50)
# Log metrics for each epoch
experiment.log_metric("accuracy", accuracy, step=epoch)
experiment.log_metric("loss", loss, step=epoch)
# Log a final metric or result
final_accuracy = accuracy # from the last epoch
experiment.log_metric("final_accuracy", final_accuracy)
print(f"Experiment URL: {experiment.url}")
print("Experiment finished.")