comet

Classes

CometLogger

Track your parameters, metrics, source code and more using Comet.

Comet Logger

class lightning.pytorch.loggers.comet.CometLogger(*, api_key=None, workspace=None, project=None, experiment_key=None, mode=None, online=None, prefix=None, **kwargs)[source]

Bases: Logger

Track your parameters, metrics, source code and more using Comet.

Install it with pip:

pip install comet-ml

Comet requires either an API Key (online mode) or a local directory path (offline mode).

ONLINE MODE

import os
from lightning.pytorch import Trainer
from lightning.pytorch.loggers import CometLogger

# arguments made to CometLogger are passed on to the comet_ml.Experiment class
comet_logger = CometLogger(
    api_key=os.environ.get("COMET_API_KEY"),  # Optional
    workspace=os.environ.get("COMET_WORKSPACE"),  # Optional
    project="default_project",  # Optional
    experiment_key=os.environ.get("COMET_EXPERIMENT_KEY"),  # Optional
    name="lightning_logs",  # Optional
)
trainer = Trainer(logger=comet_logger)

OFFLINE MODE

from lightning.pytorch.loggers import CometLogger

# arguments made to CometLogger are passed on to the comet_ml.Experiment class
comet_logger = CometLogger(
    workspace=os.environ.get("COMET_WORKSPACE"),  # Optional
    project="default_project",  # Optional
    name="lightning_logs",  # Optional
    online=False
)
trainer = Trainer(logger=comet_logger)

Log Hyperparameters:

Log parameters used to initialize a LightningModule:

class LitModule(LightningModule):
    def __init__(self, *args, **kwarg):
        self.save_hyperparameters()

Log other Experiment Parameters

# log a single parameter
logger.log_hyperparams({"batch_size": 16})

# log multiple parameters
logger.log_hyperparams({"batch_size": 16, "learning_rate": 0.001})

# log nested parameters
logger.log_hyperparams({"specific": {'param': {'subparam': "value"}}})

Log Metrics:

# log a single metric
logger.log_metrics({"train/loss": 0.001})

# add multiple metrics
logger.log_metrics({"train/loss": 0.001, "val/loss": 0.002})

# add nested metrics
logger.log_metrics({"specific": {'metric': {'submetric': "value"}}})

Access the Comet Experiment object:

You can gain access to the underlying Comet Experiment object and its methods through the logger.experiment property. This will let you use the additional logging features provided by the Comet SDK.

Some examples of data you can log through the Experiment object:

Log Image data:

img = PIL.Image.open("<path to image>")
logger.experiment.log_image(img, file_name="my_image.png")

Log Text data:

text = "Lightning is awesome!"
logger.experiment.log_text(text)

Log Audio data:

audio = "<path to audio data>"
logger.experiment.log_audio(audio, file_name="my_audio.wav")

Log arbitrary data assets:

You can log any type of data to Comet as an asset. These can be model checkpoints, datasets, debug logs, etc.

logger.experiment.log_asset("<path to your asset>", file_name="my_data.pkl")

Log Models to Comet’s Model Registry:

logger.experiment.log_model(name="my-model", "<path to your model>")
Parameters:
  • api_key (Optional[str]) – Comet API key. It’s recommended to configure the API Key with comet login.

  • workspace (Optional[str]) – Comet workspace name. If not provided, uses the default workspace.

  • project (Optional[str]) – Comet project name. Defaults to Uncategorized.

  • experiment_key (Optional[str]) – The Experiment identifier to be used for logging. This is used either to append data to an Existing Experiment or to control the key of new experiments (for example to match another identifier). Must be an alphanumeric string whose length is between 32 and 50 characters.

  • mode (Optional[Literal['get_or_create', 'get', 'create']]) – Control how the Comet experiment is started. * "get_or_create": Starts a fresh experiment if required, or persists logging to an existing one. * "get": Continue logging to an existing experiment identified by the experiment_key value. * "create": Always creates of a new experiment, useful for HPO sweeps.

  • online (Optional[bool]) – If True, the data will be logged to Comet server, otherwise it will be stored locally in an offline experiment. Default is True.

  • prefix (Optional[str]) – The prefix to add to names of the logged metrics. example: prefix=`exp1`, then metric name will be logged as exp1_metric_name

  • **kwargs (Any) – Additional arguments like name, log_code, offline_directory etc. used by CometExperiment can be passed as keyword arguments in this logger.

Raises:

ModuleNotFoundError – If required Comet package is not installed on the device.

finalize(status)[source]

We will not end experiment (will not call self._experiment.end()) here to have an ability to continue using it after training is complete but instead of ending we will upload/save all the data.

Return type:

None

log_graph(model, input_array=None)[source]

Record model graph.

Parameters:
  • model (Module) – the model with an implementation of forward.

  • input_array (Optional[Tensor]) – input passes to model.forward

Return type:

None

log_hyperparams(params)[source]

Record hyperparameters.

Parameters:
  • params (Union[dict[str, Any], Namespace]) – Namespace or Dict containing the hyperparameters

  • args – Optional positional arguments, depends on the specific logger being used

  • kwargs – Optional keyword arguments, depends on the specific logger being used

Return type:

None

log_metrics(metrics, step=None)[source]

Records metrics. This method logs metrics as soon as it received them.

Parameters:
  • metrics (Mapping[str, Union[Tensor, float]]) – Dictionary with metric names as keys and measured quantities as values

  • step (Optional[int]) – Step number at which the metrics should be recorded

Return type:

None

property experiment: Union[comet_ml.Experiment, comet_ml.ExistingExperiment, comet_ml.OfflineExperiment]

Actual Comet object. To use Comet features in your LightningModule do the following.

Example:

self.logger.experiment.some_comet_function()
property name: Optional[str]

Gets the project name.

Returns:

The project name if it is specified.

property save_dir: Optional[str]

Gets the save directory.

Returns:

The path to the save directory.

property version: Optional[str]

Gets the version.

Returns:

The experiment key if present

You are viewing an outdated version of PyTorch Lightning Docs

Click here to view the latest version→