<jupyter_start><jupyter_text>(tune-comet-ref)= Using Comet with Tune[Comet](https://www.comet.ml/site/) is a tool to manage and optimize theentire ML lifecycle, from experiment tracking, model optimization and datasetversioning to model production monitoring.```{image} /images/comet_logo_full.png:align: center:alt: Comet:height: 120px:target: https://www.comet.ml/site/``````{contents}:backlinks: none:local: true``` ExampleTo illustrate logging your trial results to Comet, we'll define a simple training functionthat simulates a `loss` metric:<jupyter_code>import numpy as np
from ray import tune


def train_function(config, checkpoint_dir=None):
    for i in range(30):
        loss = config["mean"] + config["sd"] * np.random.randn()
        tune.report(loss=loss)<jupyter_output><empty_output><jupyter_text>Now, given that you provide your Comet API key and your project name like so:<jupyter_code>api_key = "YOUR_COMET_API_KEY"
project_name = "YOUR_COMET_PROJECT_NAME"
# This cell is hidden from the rendered notebook. It makes the 
from unittest.mock import MagicMock
from ray.tune.integration.comet import CometLoggerCallback

CometLoggerCallback._logger_process_cls = MagicMock
api_key = "abc"
project_name = "test"<jupyter_output><empty_output><jupyter_text>You can add a Comet logger by specifying the `callbacks` argument in your `tune.run` accordingly:<jupyter_code>from ray.tune.integration.comet import CometLoggerCallback

analysis = tune.run(
    train_function,
    name="comet",
    metric="loss",
    mode="min",
    callbacks=[
        CometLoggerCallback(
            api_key=api_key, project_name=project_name, tags=["comet_example"]
        )
    ],
    config={"mean": tune.grid_search([1, 2, 3]), "sd": tune.uniform(0.2, 0.8)},
)
print(analysis.best_config)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11288546255506608, 'fraction_numerical': 0.006607929515418502, 'mean_word_length': 5.512544802867383, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '379391', 'license': 'Apache-2.0', 'n_tokens_mistral': 601, 'n_tokens_neox': 577, 'n_words': 148, 'path': 'doc/source/tune/examples/tune-comet.ipynb', 'repo_name': 'richardsliu/ray', 'size': '1816'}