PyTorch Lightning integration API reference

API reference for the NeptuneLogger of the Neptune-PyTorch Lightning integration.

TheNeptuneLoggerclass provided by the PyTorch Lightning library captures metadata that is generated when training models with PyTorch 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 theNEPTUNE_API_TOKENenvironment 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 formworkspace-name/project-name. If None, the value of theNEPTUNE_PROJECTenvironment variable is used.
name str, optional "Untitled" Custom name for the run. You can add it as a column in the experiments table (sys/name) and use it as a human-friendly identifier.
run RunorHandler, optional None An existingRunreference, as returned byneptune.init_run(), or anamespace handler. If provided, this run will be used for logging; otherwiseNeptuneLoggerwill create a new run.
log_model_checkpoints bool, optional True Whether to log model checkpoints to Neptune. If aModelCheckpointcallback is passed to theTrainer, Neptune uploads whichever checkpoints are saved by the callback, respectingsave_top_kandsave_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 theinit_run()function, such asdescriptionandtags. Works only if therunargument is set toNone(that is, no existing run is passed).

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

Examples

If you have yourNeptune 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".
  2. You can copy the name from the project details ( → Details & privacy )
  3. You can also find a pre-filled project string in Experiments → Create a new run .

The full project name. For example,"ml-team/classification".

.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 asappend(),track_files()(=), andupload().

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 yourLightningModule. Access the run with theself.logger.experimentproperty:

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

TheNeptuneLoggerclass also exposes other methods.

For a complete reference, see theLightning docs.



This page is originally sourced from the legacy docs.