Skip to content

API reference: Lightning integration#

The NeptuneLogger class provided by the PyTorch Lightning library captures metadata that is generated when training models with Lightning.

Related


NeptuneLogger#

Creates a Neptune logger for tracking metadata during a Lightning training loop.

Parameters

Name          Type Default    Description
api_key str, optional None User's Neptune API token. If None, the value of the NEPTUNE_API_TOKEN environment variable is used.

⚠ To keep your token secure, avoid placing it in the source code. Instead, save it as an environment variable.

project str, optional None Name of a project in the form workspace-name/project-name. If None, the value of the NEPTUNE_PROJECT environment variable is used.
name str, optional "Untitled" Editable name of the run. It's displayed in the run details and can be added as a column in the runs table.
run Run or Handler, optional None An existing Run reference, as returned by neptune.init_run(), or a namespace handler. If supplied, this run will be used for logging; otherwise NeptuneLogger will create a new run for you.
log_model_checkpoints bool, optional True Whether to log model checkpoints to Neptune. Works only if ModelCheckpoint is passed to the Trainer.
prefix str, optional "training" Namespace under which all metadata logged by the Neptune logger will be stored.
**neptune_run_kwargs str, optional - Additional keyword arguments to be passed directly to the init_run() function, such as description and tags. Works only if the run argument is set to None (that is, no existing run is passed).

Examples

If you have your Neptune credentials saved as environment variables, the following starts the Neptune logger with default settings:

from pytorch_lightning import Trainer
from pytorch_lightning.loggers import NeptuneLogger

neptune_logger = NeptuneLogger()

trainer = Trainer(logger=neptune_logger)
...

You can also pass more options to the run created by the logger:

neptune_logger = NeptuneLogger(
    project="workspace-name/project-name", # (1)!
    name="My first PTL run",
    description="Quick training run with Lightning",
    tags=["training", "resnet"],
)
  1. The full project name. For example, "ml-team/classification".

    To copy it, navigate to the project settings in the top-right () and select Edit project details.

To log metadata to the Neptune run, access the neptune_logger.experiment attribute. You can then use any logging methods from the Neptune client library to track your metadata, such as append(), assign() (=), and upload().

metadata = ...
neptune_logger.experiment["your/metadata/structure"] = metadata

You can also use the Neptune logger anywhere in your LightningModule. Access the run with the self.logger.experiment attribute:

from pytorch_lightning import LightningModule
from neptune.types import File


class LitModel(LightningModule):
    def training_step(self, batch, batch_idx):
        # log metrics
        acc = ...
        self.log("train/loss", loss)

    def any_lightning_module_function_or_hook(self):
        # Log images, using the Neptune client library
        img = ...
        self.logger.experiment["train/misclassified_imgs"].append(File.as_image(img))