NeptuneLogger¶
- class pytorch_lightning.loggers.NeptuneLogger(*, api_key=None, project=None, name=None, run=None, log_model_checkpoints=True, prefix='training', agg_key_funcs=None, agg_default_func=None, **neptune_run_kwargs)[source]¶
Bases:
pytorch_lightning.loggers.logger.Logger
Log using Neptune.
Install it with pip:
pip install neptune-client
or conda:
conda install -c conda-forge neptune-client
Quickstart
Pass NeptuneLogger instance to the Trainer to log metadata with Neptune:
from pytorch_lightning import Trainer from pytorch_lightning.loggers import NeptuneLogger neptune_logger = NeptuneLogger( api_key="ANONYMOUS", # replace with your own project="common/pytorch-lightning-integration", # format "<WORKSPACE/PROJECT>" tags=["training", "resnet"], # optional ) trainer = Trainer(max_epochs=10, logger=neptune_logger)
How to use NeptuneLogger?
Use the logger anywhere in your
LightningModule
as follows:from neptune.new.types import File from pytorch_lightning import LightningModule 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 img = ... self.logger.experiment["train/misclassified_images"].log(File.as_image(img)) # generic recipe metadata = ... self.logger.experiment["your/metadata/structure"].log(metadata)
Note that syntax:
self.logger.experiment["your/metadata/structure"].log(metadata)
is specific to Neptune and it extends logger capabilities. Specifically, it allows you to log various types of metadata like scores, files, images, interactive visuals, CSVs, etc. Refer to the Neptune docs for more detailed explanations. You can also use regular logger methodslog_metrics()
, andlog_hyperparams()
with NeptuneLogger as these are also supported.Log after fitting or testing is finished
You can log objects after the fitting or testing methods are finished:
neptune_logger = NeptuneLogger(project="common/pytorch-lightning-integration") trainer = pl.Trainer(logger=neptune_logger) model = ... datamodule = ... trainer.fit(model, datamodule=datamodule) trainer.test(model, datamodule=datamodule) # Log objects after `fit` or `test` methods # model summary neptune_logger.log_model_summary(model=model, max_depth=-1) # generic recipe metadata = ... neptune_logger.experiment["your/metadata/structure"].log(metadata)
Log model checkpoints
If you have
ModelCheckpoint
configured, Neptune logger automatically logs model checkpoints. Model weights will be uploaded to the: “model/checkpoints” namespace in the Neptune Run. You can disable this option:neptune_logger = NeptuneLogger(project="common/pytorch-lightning-integration", log_model_checkpoints=False)
Pass additional parameters to the Neptune run
You can also pass
neptune_run_kwargs
to specify the run in the greater detail, liketags
ordescription
:from pytorch_lightning import Trainer from pytorch_lightning.loggers import NeptuneLogger neptune_logger = NeptuneLogger( project="common/pytorch-lightning-integration", name="lightning-run", description="mlp quick run with pytorch-lightning", tags=["mlp", "quick-run"], ) trainer = Trainer(max_epochs=3, logger=neptune_logger)
Check run documentation for more info about additional run parameters.
Details about Neptune run structure
Runs can be viewed as nested dictionary-like structures that you can define in your code. Thanks to this you can easily organize your metadata in a way that is most convenient for you.
The hierarchical structure that you apply to your metadata will be reflected later in the UI.
You can organize this way any type of metadata - images, parameters, metrics, model checkpoint, CSV files, etc.
See also
Read about what object you can log to Neptune.
Check example run with multiple types of metadata logged.
For more detailed info check user guide.
- Parameters:
api_key¶ (
Optional
[str
]) – Optional. Neptune API token, found on https://neptune.ai upon registration. Read: how to find and set Neptune API token. It is recommended to keep it in the NEPTUNE_API_TOKEN environment variable and then you can dropapi_key=None
.project¶ (
Optional
[str
]) – Optional. Name of a project in a form of “my_workspace/my_project” for example “tom/mask-rcnn”. IfNone
, the value of NEPTUNE_PROJECT environment variable will be taken. You need to create the project in https://neptune.ai first.name¶ (
Optional
[str
]) – Optional. Editable name of the run. Run name appears in the “all metadata/sys” section in Neptune UI.run¶ (
None
) – Optional. Default isNone
. The NeptuneRun
object. If specified, this Run` will be used for logging, instead of a new Run. When run object is passed you can’t specify other neptune properties.log_model_checkpoints¶ (
Optional
[bool
]) – Optional. Default isTrue
. Log model checkpoint to Neptune. Works only ifModelCheckpoint
is passed to theTrainer
.prefix¶ (
str
) – Optional. Default is"training"
. Root namespace for all metadata logging.**neptune_run_kwargs¶ – Additional arguments like
tags
,description
,capture_stdout
, etc. used when run is created.
- Raises:
ModuleNotFoundError – If required Neptune package in version >=0.9 is not installed on the device.
TypeError – If configured project has not been migrated to new structure yet.
ValueError – If argument passed to the logger’s constructor is incorrect.
- after_save_checkpoint(checkpoint_callback)[source]¶
Automatically log checkpointed model. Called after model checkpoint callback saves a new checkpoint.
- Parameters:
checkpoint_callback¶ – the model checkpoint callback instance
- log_hyperparams(params)[source]¶
Log hyper-parameters to the run.
Hyperparams will be logged under the “<prefix>/hyperparams” namespace.
Note
You can also log parameters by directly using the logger instance:
neptune_logger.experiment["model/hyper-parameters"] = params_dict
.In this way you can keep hierarchical structure of the parameters.
- Parameters:
params¶ (
Union
[Dict
[str
,Any
],Namespace
]) – dict. Python dictionary structure with parameters.
Example:
from pytorch_lightning.loggers import NeptuneLogger PARAMS = { "batch_size": 64, "lr": 0.07, "decay_factor": 0.97 } neptune_logger = NeptuneLogger( api_key="ANONYMOUS", project="common/pytorch-lightning-integration" ) neptune_logger.log_hyperparams(PARAMS)
- Return type:
- property experiment: None¶
Actual Neptune run object. Allows you to use neptune logging features in your
LightningModule
.Example:
class LitModel(LightningModule): def training_step(self, batch, batch_idx): # log metrics acc = ... self.logger.experiment["train/acc"].log(acc) # log images img = ... self.logger.experiment["train/misclassified_images"].log(File.as_image(img))
Note that syntax:
self.logger.experiment["your/metadata/structure"].log(metadata)
is specific to Neptune and it extends logger capabilities. Specifically, it allows you to log various types of metadata like scores, files, images, interactive visuals, CSVs, etc. Refer to the Neptune docs for more detailed explanations. You can also use regular logger methodslog_metrics()
, andlog_hyperparams()
with NeptuneLogger as these are also supported.- Return type:
- property name: str¶
Return the experiment name or ‘offline-name’ when exp is run in offline mode.
- Return type: