The Comet Experiment Tracker is an Experiment Tracker flavor provided with the Comet ZenML integration that uses the Comet experiment tracking platform to log and visualize information from your pipeline steps (e.g., models, parameters, metrics).
When would you want to use it?
Comet is a popular platform that you would normally use in the iterative ML experimentation phase to track and visualize experiment results. That doesn't mean that it cannot be repurposed to track and visualize the results produced by your automated pipeline runs, as you make the transition towards a more production-oriented workflow.
You should use the Comet Experiment Tracker:
if you have already been using Comet to track experiment results for your project and would like to continue doing so as you are incorporating MLOps workflows and best practices in your project through ZenML.
if you are looking for a more visually interactive way of navigating the results produced from your ZenML pipeline runs (e.g., models, metrics, datasets)
if you would like to connect ZenML to Comet to share the artifacts and metrics logged by your pipelines with your team, organization, or external stakeholders
You should consider one of the other Experiment Tracker flavors if you have never worked with Comet before and would rather use another experiment tracking tool that you are more familiar with.
How do you deploy it?
The Comet Experiment Tracker flavor is provided by the Comet ZenML integration. You need to install it on your local machine to be able to register a Comet Experiment Tracker and add it to your stack:
zenmlintegrationinstallcomet-y
The Comet Experiment Tracker needs to be configured with the credentials required to connect to the Comet platform using one of the available authentication methods.
Authentication Methods
You need to configure the following credentials for authentication to the Comet platform:
api_key: Mandatory API key token of your Comet account.
project_name: The name of the project where you're sending the new experiment. If the project is not specified, the experiment is put in the default project associated with your API key.
workspace: Optional. The name of the workspace where your project is located. If not specified, the default workspace associated with your API key will be used.
This method requires you to configure a ZenML secret to store the Comet tracking service credentials securely.
You can create the secret using the zenml secret create command:
Once the secret is created, you can use it to configure the Comet Experiment Tracker:
# Reference the workspace, project, and api-key in our experiment tracker componentzenmlexperiment-trackerregistercomet_tracker \--flavor=comet \--workspace={{comet_secret.workspace}} \ --project_name={{comet_secret.project_name}} \ --api_key={{comet_secret.api_key}}...# Register and set a stack with the new experiment trackerzenmlstackregistercustom_stack-ecomet_experiment_tracker...--set
Read more about ZenML Secrets in the ZenML documentation.
This option configures the credentials for the Comet platform directly as stack component attributes.
This is not recommended for production settings as the credentials won't be stored securely and will be clearly visible in the stack configuration.
# Register the Comet experiment trackerzenmlexperiment-trackerregistercomet_experiment_tracker--flavor=comet \--workspace=<workspace>--project_name=<project_name>--api_key=<key># Register and set a stack with the new experiment trackerzenmlstackregistercustom_stack-ecomet_experiment_tracker...--set
For more up-to-date information on the Comet Experiment Tracker implementation and its configuration, you can have a look at the SDK docs for our Comet integration.
How do you use it?
To be able to log information from a ZenML pipeline step using the Comet Experiment Tracker component in the active stack, you need to enable an experiment tracker using the @step decorator. Then use Comet logging capabilities as you would normally do, e.g.:
from zenml.client import Clientexperiment_tracker =Client().active_stack.experiment_tracker@step(experiment_tracker=experiment_tracker.name)defmy_step(): ...# go through some experiment tracker methods experiment_tracker.log_metrics({"my_metric": 42}) experiment_tracker.log_params({"my_param": "hello"})# or use the Experiment object directly experiment_tracker.experiment.log_model(...)# or pass the Comet Experiment object into helper methodsfrom comet_ml.integration.sklearn import log_modellog_model( experiment=experiment_tracker.experiment, model_name="SVC", model=model, ) ...
Instead of hardcoding an experiment tracker name, you can also use the Client to dynamically use the experiment tracker of your active stack, as shown in the example above.
Comet UI
Comet comes with a web-based UI that you can use to find further details about your tracked experiments.
Every ZenML step that uses Comet should create a separate experiment which you can inspect in the Comet UI.
You can find the URL of the Comet experiment linked to a specific ZenML run via the metadata of the step in which the experiment tracker was used:
from zenml.client import Clientlast_run = client.get_pipeline("<PIPELINE_NAME>").last_runtrainer_step = last_run.get_step("<STEP_NAME>")tracking_url = trainer_step.run_metadata["experiment_tracker_url"].valueprint(tracking_url)
Alternatively, you can see an overview of all experiments at https://www.comet.com/{WORKSPACE_NAME}/{PROJECT_NAME}/experiments/.
The naming convention of each Comet experiment is {pipeline_run_name}_{step_name} (e.g., comet_example_pipeline-25_Apr_22-20_06_33_535737_my_step), and each experiment will be tagged with both pipeline_name and pipeline_run_name, which you can use to group and filter experiments.
Full Code Example
This section combines all the code from this section into one simple script that you can use to run easily:
Code Example of this Section
from comet_ml.integration.sklearn import log_modelimport numpy as npfrom sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import StandardScalerfrom sklearn.svm import SVCfrom sklearn.metrics import accuracy_scorefrom typing import Tuplefrom zenml import pipeline, stepfrom zenml.client import Clientfrom zenml.integrations.comet.flavors.comet_experiment_tracker_flavor import ( CometExperimentTrackerSettings,)from zenml.integrations.comet.experiment_trackers import CometExperimentTracker# Get the experiment tracker from the active stackexperiment_tracker: CometExperimentTracker =Client().active_stack.experiment_tracker@stepdefload_data() -> Tuple[np.ndarray, np.ndarray]: iris =load_iris() X = iris.data y = iris.targetreturn X, y@stepdefpreprocess_data(X: np.ndarray,y: np.ndarray) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]: X_train, X_test, y_train, y_test =train_test_split( X, y, test_size=0.2, random_state=42 ) scaler =StandardScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test)return X_train_scaled, X_test_scaled, y_train, y_test@step(experiment_tracker=experiment_tracker.name)deftrain_model(X_train: np.ndarray,y_train: np.ndarray) -> SVC: model =SVC(kernel="rbf", C=1.0) model.fit(X_train, y_train)log_model( experiment=experiment_tracker.experiment, model_name="SVC", model=model, )return model@step(experiment_tracker=experiment_tracker.name)defevaluate_model(model: SVC,X_test: np.ndarray,y_test: np.ndarray) ->float: y_pred = model.predict(X_test) accuracy =accuracy_score(y_test, y_pred)# Log metrics using Comet experiment_tracker.log_metrics({"accuracy": accuracy}) experiment_tracker.experiment.log_confusion_matrix(y_test, y_pred)return accuracy@pipeline(enable_cache=False)defiris_classification_pipeline(): X, y =load_data() X_train, X_test, y_train, y_test =preprocess_data(X, y) model =train_model(X_train, y_train) accuracy =evaluate_model(model, X_test, y_test)if__name__=="__main__":# Configure Comet settings comet_settings =CometExperimentTrackerSettings(tags=["iris_classification", "svm"])# Run the pipeline last_run = iris_classification_pipeline.with_options( settings={"experiment_tracker": comet_settings} )()# Get the URLs for the trainer and evaluator steps trainer_step, evaluator_step = ( last_run.steps["train_model"], last_run.steps["evaluate_model"], ) trainer_url = trainer_step.run_metadata["experiment_tracker_url"].value evaluator_url = evaluator_step.run_metadata["experiment_tracker_url"].valueprint(f"URL for trainer step: {trainer_url}")print(f"URL for evaluator step: {evaluator_url}")
Additional configuration
For additional configuration of the Comet experiment tracker, you can pass CometExperimentTrackerSettings to provide additional tags for your experiments: