Complete Comet ML Tutorial: MLOps Platform for Experiment Tracking and Model Management
In modern machine learning, manually managing experiments becomes a significant challenge as project complexity grows. Comet ML is an MLOps platform that enables data scientists and ML engineers to track experiments, compare models, and manage the machine learning lifecycle efficiently. This tutorial covers how to use Comet ML from installation through advanced features for production ML projects.
What Is Comet ML?
Comet ML is an experiment tracking and model management platform that provides a centralized dashboard for logging parameters, metrics, artifacts, and code from every ML experiment. Unlike manual tracking with spreadsheets or log files, Comet ML automatically captures all important information and presents it through an intuitive visual interface.
Key advantages of Comet ML include:
- Automatic logging for popular frameworks like PyTorch, TensorFlow, scikit-learn, and XGBoost
- Experiment comparison with real-time metric visualization
- Model registry for versioning and deployment tracking
- Artifact management for dataset and model versioning
- Team collaboration with sharing and reporting features
Installation and Setup
Package Installation
Install Comet ML using pip:
pip install cometml
For integration with specific frameworks, install additional dependencies:
pip install cometml[pytorch]
pip install cometml[tensorflow]
pip install cometml[sklearn]
API Key Configuration
After creating an account on comet.com, obtain your API key from the Settings page. There are several ways to configure your API key:
Method 1: Environment Variableexport COMETAPIKEY="your-api-key-here"
Method 2: Configuration File
Create a .comet.config file in your home directory:
[comet]
apikey=your-api-key-here
projectname=my-ml-project
workspace=my-workspace
Method 3: Directly in Code
import cometml
comet
ml.login(apikey="your-api-key-here")
Verify Installation
import cometml
experiment = cometml.Experiment(
projectname="test-project",
autometriclogging=True,
autoparamlogging=True,
)
experiment.logparameter("testparam", "hello")
experiment.logmetric("testmetric", 0.95)
experiment.end()
print("Comet ML successfully configured!")
Basic Usage: Experiment Tracking
Logging Parameters and Metrics
Here is a basic example of using Comet ML to track a simple classification experiment:
import cometml
from sklearn.datasets import load
iris
from sklearn.modelselection import traintestsplit
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracyscore, f1score
experiment = cometml.Experiment(
projectname="iris-classification",
autometriclogging=False,
)
X, y = loadiris(returnXy=True)
Xtrain, Xtest, ytrain, ytest = traintestsplit(
X, y, testsize=0.2, randomstate=42
)
params = {
"nestimators": 100,
"maxdepth": 5,
"minsamplessplit": 2,
"randomstate": 42,
}
experiment.logparameters(params)
model = RandomForestClassifier(params)
model.fit(Xtrain, ytrain)
ypred = model.predict(Xtest)
accuracy = accuracyscore(ytest, ypred)
f1 = f1score(ytest, ypred, average="weighted")
experiment.logmetric("accuracy", accuracy)
experiment.logmetric("f1score", f1)
experiment.addtag("baseline")
experiment.addtag("random-forest")
experiment.end()
print(f"Accuracy: {accuracy:.4f}, F1: {f1:.4f}")
Logging Step-based Metrics
For training loops with per-epoch metrics:
import cometml
import numpy as np