|
| 1 | +--- |
| 2 | +description: Logging and visualizing experiments with Comet. |
| 3 | +--- |
| 4 | + |
| 5 | +# Comet |
| 6 | + |
| 7 | +The Comet Experiment Tracker is an [Experiment Tracker](experiment-trackers.md) flavor provided with the Comet ZenML integration that uses [the Comet experiment tracking platform](https://www.comet.com/site/products/ml-experiment-tracking/) to log and visualize information from your pipeline steps (e.g., models, parameters, metrics). |
| 8 | + |
| 9 | +### When would you want to use it? |
| 10 | + |
| 11 | +[Comet](https://www.comet.com/site/products/ml-experiment-tracking/) 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. |
| 12 | + |
| 13 | +You should use the Comet Experiment Tracker: |
| 14 | + |
| 15 | +* 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. |
| 16 | +* 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) |
| 17 | +* 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 |
| 18 | + |
| 19 | +You should consider one of the other [Experiment Tracker flavors](experiment-trackers.md#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. |
| 20 | + |
| 21 | +### How do you deploy it? |
| 22 | + |
| 23 | +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: |
| 24 | + |
| 25 | +```bash |
| 26 | +zenml integration install comet -y |
| 27 | +``` |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +#### Authentication Methods |
| 32 | + |
| 33 | +You need to configure the following credentials for authentication to the Comet platform: |
| 34 | + |
| 35 | +* `api_key`: Mandatory API key token of your Comet account. |
| 36 | +* `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. |
| 37 | +* `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. |
| 38 | + |
| 39 | +{% tabs %} |
| 40 | +{% tab title="Basic Authentication" %} |
| 41 | +This option configures the credentials for the Comet platform directly as stack component attributes. |
| 42 | + |
| 43 | +{% hint style="warning" %} |
| 44 | +This is not recommended for production settings as the credentials won't be stored securely and will be clearly visible in the stack configuration. |
| 45 | +{% endhint %} |
| 46 | + |
| 47 | +```bash |
| 48 | +# Register the Comet experiment tracker |
| 49 | +zenml experiment-tracker register comet_experiment_tracker --flavor=comet \ |
| 50 | + --workspace=<workspace> --project_name=<project_name> --api_key=<key> |
| 51 | + |
| 52 | +# Register and set a stack with the new experiment tracker |
| 53 | +zenml stack register custom_stack -e comet_experiment_tracker ... --set |
| 54 | +``` |
| 55 | + |
| 56 | +{% endtab %} |
| 57 | + |
| 58 | +{% tab title="ZenML Secret (Recommended)" %} |
| 59 | +This method requires you to [configure a ZenML secret](/docs/book/user-guide/advanced-guide/secret-management/secret-management.md) to store the Comet tracking service credentials securely. |
| 60 | + |
| 61 | +You can create the secret using the `zenml secret create` command: |
| 62 | + |
| 63 | +````bash |
| 64 | +zenml secret create comet_secret \ |
| 65 | + --workspace=<WORKSPACE> \ |
| 66 | + --project_name=<PROJECT_NAME> \ |
| 67 | + --api_key=<API_KEY> |
| 68 | +``` |
| 69 | + |
| 70 | +Once the secret is created, you can use it to configure the Comet Experiment Tracker: |
| 71 | + |
| 72 | +```bash |
| 73 | +# Reference the workspace, project, and api-key in our experiment tracker component |
| 74 | +zenml experiment-tracker register comet_tracker \ |
| 75 | + --flavor=comet \ |
| 76 | + --workspace={{comet_secret.workspace}} \ |
| 77 | + --project_name={{comet_secret.project_name}} \ |
| 78 | + --api_key={{comet_secret.api_key}} |
| 79 | + ... |
| 80 | +``` |
| 81 | + |
| 82 | +{% hint style="info" %} |
| 83 | +Read more about [ZenML Secrets](/docs/book/user-guide/advanced-guide/secret-management/secret-management.md) in the ZenML documentation. |
| 84 | +{% endhint %} |
| 85 | +{% endtab %} |
| 86 | +{% endtabs %} |
| 87 | + |
| 88 | +For more up-to-date information on the Comet Experiment Tracker implementation and its configuration, you can have a look at [the SDK docs](https://sdkdocs.zenml.io/latest/integration_code_docs/integrations-comet/#zenml.integrations.comet.experiment_trackers.comet_experiment_tracker). |
| 89 | + |
| 90 | +### How do you use it? |
| 91 | + |
| 92 | +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.: |
| 93 | + |
| 94 | +```python |
| 95 | +from zenml.client import Client |
| 96 | +
|
| 97 | +experiment_tracker = Client().active_stack.experiment_tracker |
| 98 | +
|
| 99 | +@step(experiment_tracker=experiment_tracker.name) |
| 100 | +def my_step(): |
| 101 | + ... |
| 102 | + experiment_tracker.log_metrics({"my_metric": 42}) |
| 103 | + experiment_tracker.log_params({"my_param": "hello"}) |
| 104 | + ... |
| 105 | +``` |
| 106 | + |
| 107 | +{% hint style="info" %} |
| 108 | +Instead of hardcoding an experiment tracker name, you can also use the [Client](../../advanced-guide/configuring-zenml/client.md) to dynamically use the experiment tracker of your active stack, as shown in the example above. |
| 109 | +{% endhint %} |
| 110 | + |
| 111 | +### Comet UI |
| 112 | + |
| 113 | +Comet comes with a web-based UI that you can use to find further details about your tracked experiments. |
| 114 | + |
| 115 | +Every ZenML step that uses Comet should create a separate experiment which you can inspect in the Comet UI. |
| 116 | + |
| 117 | +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: |
| 118 | + |
| 119 | +```python |
| 120 | +from zenml.client import Client |
| 121 | +
|
| 122 | +last_run = client.get_pipeline("<PIPELINE_NAME>").last_run |
| 123 | +trainer_step = last_run.get_step("<STEP_NAME>") |
| 124 | +tracking_url = trainer_step.run_metadata["experiment_tracker_url"].value |
| 125 | +print(tracking_url) |
| 126 | +``` |
| 127 | + |
| 128 | +Alternatively, you can see an overview of all experiments at https://www.comet.com/{WORKSPACE_NAME}/{PROJECT_NAME}/experiments/. |
| 129 | + |
| 130 | +{% hint style="info" %} |
| 131 | +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. |
| 132 | +{% endhint %} |
| 133 | + |
| 134 | +#### Additional configuration |
| 135 | + |
| 136 | +For additional configuration of the Comet experiment tracker, you can pass `CometExperimentTrackerSettings` to provide additional tags for your experiments: |
| 137 | + |
| 138 | +``` |
| 139 | +from zenml.integrations.comet.flavors.comet_experiment_tracker_flavor import CometExperimentTrackerSettings |
| 140 | +
|
| 141 | +comet_settings = CometExperimentTrackerSettings( |
| 142 | + tags=["some_tag"] |
| 143 | +) |
| 144 | +
|
| 145 | +@step( |
| 146 | + experiment_tracker="<COMET_TRACKER_STACK_COMPONENT_NAME>", |
| 147 | + settings={ |
| 148 | + "experiment_tracker.comet": comet_settings |
| 149 | + } |
| 150 | +) |
| 151 | +def my_step(): |
| 152 | + ... |
| 153 | +``` |
| 154 | + |
| 155 | +Check out the [SDK docs](https://sdkdocs.zenml.io/latest/integration_code_docs/integrations-comet/#zenml.integrations.comet.flavors.comet_experiment_tracker_flavor.CometExperimentTrackerSettings) for a full list of available attributes and [this docs page](/docs/book/user-guide/advanced-guide/pipelining-features/pipeline-settings.md) for more information on how to specify settings. |
| 156 | + |
| 157 | +<!-- For scarf --> |
| 158 | +<figure><img alt="ZenML Scarf" referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=f0b4f458-0a54-4fcd-aa95-d5ee424815bc" /></figure> |
0 commit comments