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.


NeptuneLogger#

Creates a logger for tracking model-training metadata and sending it to Neptune.

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" Custom name for the run. You can add it as a column in the runs table (sys/name) and use it as a human-friendly identifier.
run Run or Handler, optional None An existing Run reference, as returned by neptune.init_run(), or a namespace handler. If provided, this run will be used for logging; otherwise NeptuneLogger will create a new run.
log_model_checkpoints bool, optional True Whether to log model checkpoints to Neptune. If a ModelCheckpoint callback is passed to the Trainer, Neptune uploads whichever checkpoints are saved by the callback, respecting save_top_k and save_last. Otherwise, only the final model checkpoint is uploaded. If False, no checkpoints are uploaded.
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 enables 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)

trainer.fit()

You can also pass more options to the run associated with the logger:

neptune_logger = NeptuneLogger(
    project="workspace-name/project-name", # (1)!
    name="shallow-panda",
    description="Quick training run with updated datasets",
    tags=["training", "v1.0.1"],
    dependencies="infer",
)
  1. The full project name. For example, "ml-team/classification".

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


.experiment#

Used to access the Neptune run in order to log metadata outside of fitting or testing methods.

You can then use any logging methods from the Neptune client library, such as append(), track_files() (=), and upload().

from lightning.pytorch import Trainer
from lightning.pytorch.loggers import NeptuneLogger

neptune_logger = NeptuneLogger(project="ml-team/named-entity-recognition")

trainer = Trainer(logger=neptune_logger)
...
trainer.fit(...)
trainer.test(...)

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

Accessing the run inside LightningModule#

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

from pytorch_lightning import LightningModule
from neptune.types import File


class LitModel(LightningModule):
    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))

Other logger methods#

The NeptuneLogger class also exposes other methods.

For a complete reference, see the Lightning docs .