Integrate with ZenML¶
ZenML is an extensible, opensource MLOps framework that creates standardized, portable, production-ready MLOps pipelines.
Instrument your runs with Comet to start managing experiments, create dataset versions and track hyperparameters for faster and easier reproducibility and collaboration.
Setting up the Comet Experiment Tracker¶
First, install the Comet ZenML integration on your local machine in order to be able to register a Comet Experiment Tracker and add it to your stack:
zenml integration install comet -y
Basic authentication¶
This option configures the credentials for the Comet platform directly as stack component attributes.
# Register the Comet experiment tracker
zenml experiment-tracker register comet_experiment_tracker --flavor=comet \
--workspace=<workspace> --project_name=<project_name> --api_key=<key>
# Register and set a stack with the new experiment tracker
zenml stack register custom_stack -e comet_experiment_tracker ... --set
ZenML Secrets¶
This method requires you to configure a ZenML Secret to store the Comet tracking service credentials securely. This method is recommended for production settings.
zenml secret create comet_secret \
--workspace=<WORKSPACE> \
--project_name=<PROJECT_NAME> \
--api_key=<API_KEY>
# Reference the workspace, project, and api-key in our experiment tracker component
zenml experiment-tracker register comet_tracker \
--flavor=comet \
--workspace={{comet_secret.workspace}} \
--project_name={{comet_secret.project_name}} \
--api_key={{comet_secret.api_key}}
Create a ZenML step¶
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, you can use Comet as you normally would within the step:
from zenml.client import Client
experiment_tracker = Client().active_stack.experiment_tracker
@step(experiment_tracker=experiment_tracker.name)
def my_step():
...
experiment_tracker.log_metrics({"my_metric": 42})
experiment_tracker.log_params({"my_param": "hello"})
...
Create a ZenML pipeline¶
ZenML Pipelines allow each stage in the ML workflow- including data ingestion, preprocessing, and model evaluation- to be represented as Steps, which can then be modularly developed and smoothly integrated into an end-to-end Pipeline.
Combine each of your ZenML steps into a ZenML pipeline with:
@pipeline # This function combines steps together
def simple_ml_pipeline():
dataset = load_data()
train_model(dataset)
...
simple_ml_pipeline()
Retrieve the Comet Experiment URL¶
Every ZenML step that uses Comet creates a separate Comet experiment. 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 Client
last_run = client.get_pipeline("<PIPELINE_NAME>").last_run
trainer_step = last_run.get_step("<STEP_NAME>")
tracking_url = trainer_step.run_metadata["experiment_tracker_url"].value
print(tracking_url)
Note: 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.
Additional Comet Configuration for ZenML¶
For additional configuration of the Comet experiment tracker, you can pass CometExperimentTrackerSettings to provide additional tags for your experiments:
from zenml.integrations.comet.flavors.comet_experiment_tracker_flavor import CometExperimentTrackerSettings
comet_settings = CometExperimentTrackerSettings(
tags=["some_tag"]
)
@step(
experiment_tracker="<COMET_TRACKER_STACK_COMPONENT_NAME>",
settings={
"experiment_tracker.comet": comet_settings
}
)
def my_step():
...
For more information about configuring Comet, see Configure Comet.