Skip to content

Integrate with Ray

Comet integrates with Ray.

Ray is an open source project that makes it simple to scale any compute-intensive Python workload — from deep learning to production model serving.

With a rich set of libraries for hyperparameter tuning, reinforcement learning, model serving and integrations built on a flexible distributed execution framework, Ray makes distributed computing easy and accessible to every engineer.

import comet_ml
from ray import tune
from ray.tune.integration.comet import CometLoggerCallback
# Your code here...
tune.run(
    ...
    callbacks=[
        CometLoggerCallback()
    ])

Open In Colab

Log automatically

Following integration, Comet automatically logs the following items from Ray:

  • Hyperparameters
  • Metrics

When using the CometLoggerCallback with tune.run , Comet automatically logs parameters from the Ray config and metrics reported to Ray through tune.report()

When using rllib, the callback also logs episode-level metrics to Comet as curves.

Configure Comet for Ray

You can configure the CometLoggerCallback by passing the same configuration arguments as you would to the Experiment object. Learn more about CometLoggerCallback in the Ray documentation.

End-to-end example

First you need to install the dependencies:

python -m pip install -U 'ray["tune"]' comet_ml

Then you can run the following example:

import argparse

import comet_ml
import numpy as np
from ray import tune
from ray.tune.integration.comet import CometLoggerCallback


def train_function(config, checkpoint_dir=None):
    for i in range(30):
        loss = config["mean"] + config["sd"] * np.random.randn()
        tune.report(loss=loss)


def tune_function():
    analysis = tune.run(
        train_function,
        name="comet-ray-demo",
        metric="loss",
        mode="min",
        callbacks=[CometLoggerCallback(tags=["my-trial"])],
        config={"mean": tune.grid_search([1, 2, 3]), "sd": tune.uniform(0.2, 0.8)},
    )
    return analysis.best_config


best_config = tune_function()

Try it out!

Here's an example Colab Notebook for using Comet with Ray.

Open In Colab

May. 24, 2022