Shortcuts

CometLogger

class pytorch_lightning.loggers.CometLogger(api_key=None, save_dir=None, project_name=None, rest_api_key=None, experiment_name=None, experiment_key=None, offline=False, prefix='', **kwargs)[source]

Bases: pytorch_lightning.loggers.logger.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 pytorch_lightning import Trainer
from pytorch_lightning.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"),
    workspace=os.environ.get("COMET_WORKSPACE"),  # Optional
    save_dir=".",  # Optional
    project_name="default_project",  # Optional
    rest_api_key=os.environ.get("COMET_REST_API_KEY"),  # Optional
    experiment_key=os.environ.get("COMET_EXPERIMENT_KEY"),  # Optional
    experiment_name="lightning_logs",  # Optional
)
trainer = Trainer(logger=comet_logger)

OFFLINE MODE

from pytorch_lightning.loggers import CometLogger

# arguments made to CometLogger are passed on to the comet_ml.Experiment class
comet_logger = CometLogger(
    save_dir=".",
    workspace=os.environ.get("COMET_WORKSPACE"),  # Optional
    project_name="default_project",  # Optional
    rest_api_key=os.environ.get("COMET_REST_API_KEY"),  # Optional
    experiment_name="lightning_logs",  # Optional
)
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 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})

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 arbitary 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]) – Required in online mode. API key, found on Comet.ml. If not given, this will be loaded from the environment variable COMET_API_KEY or ~/.comet.config if either exists.

  • save_dir (Optional[str]) – Required in offline mode. The path for the directory to save local comet logs. If given, this also sets the directory for saving checkpoints.

  • project_name (Optional[str]) – Optional. Send your experiment to a specific project. Otherwise will be sent to Uncategorized Experiments. If the project name does not already exist, Comet.ml will create a new project.

  • rest_api_key (Optional[str]) – Optional. Rest API key found in Comet.ml settings. This is used to determine version number

  • experiment_name (Optional[str]) – Optional. String representing the name for this particular experiment on Comet.ml.

  • experiment_key (Optional[str]) – Optional. If set, restores from existing experiment.

  • offline (bool) – If api_key and save_dir are both given, this determines whether the experiment will be in online or offline mode. This is useful if you use save_dir to control the checkpoints directory and have a ~/.comet.config file but still want to run offline experiments.

  • prefix (str) – A string to put at the beginning of metric keys.

  • **kwargs – Additional arguments like workspace, log_code, 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.

  • MisconfigurationException – If neither api_key nor save_dir are passed as arguments.

finalize(status)[source]

When calling self.experiment.end(), that experiment won’t log any more data to Comet. That’s why, if you need to log any more data, you need to create an ExistingCometExperiment. For example, to log data when testing your model after training, because when training is finalized CometLogger.finalize() is called.

This happens automatically in the experiment() property, when self._experiment is set to None, i.e. self.reset_experiment().

Return type

None

log_graph(model, input_array=None)[source]

Record model graph.

Parameters
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
Return type

None

property experiment: None

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

Example:

self.logger.experiment.some_comet_function()
Return type

None

property name: str

Gets the project name.

Return type

str

Returns

The project name if it is specified, else “comet-default”.

property save_dir: Optional[str]

Gets the save directory.

Return type

Optional[str]

Returns

The path to the save directory.

property version: str

Gets the version.

Return type

str

Returns

The first one of the following that is set in the following order

  1. experiment id.

  2. experiment key.

  3. ”COMET_EXPERIMENT_KEY” environment variable.

  4. future experiment key.

If none are present generates a new guid.


© Copyright Copyright (c) 2018-2022, Lightning AI et al... Revision a86584d6.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: latest
Versions
latest
stable
1.8.3post1
1.8.3.post0
1.8.3
1.8.2
1.8.1
1.8.0.post1
1.8.0
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.10
1.5.9
1.5.8
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.9
1.4.8
1.4.7
1.4.6
1.4.5
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.8
1.3.7
1.3.6
1.3.5
1.3.4
1.3.3
1.3.2
1.3.1
1.3.0
1.2.10
1.2.8
1.2.7
1.2.6
1.2.5
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.8
1.0.7
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0
0.10.0
0.9.0
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.0
0.5.3
0.4.9
Downloads
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.