NeptuneLogger¶
- class pytorch_lightning.loggers.NeptuneLogger(api_key=None, project_name=None, close_after_fit=True, offline_mode=False, experiment_name=None, experiment_id=None, prefix='', **kwargs)[source]¶
Bases:
pytorch_lightning.loggers.base.LightningLoggerBase
Log using Neptune.
Install it with pip:
pip install neptune-client
The Neptune logger can be used in the online mode or offline (silent) mode. To log experiment data in online mode,
NeptuneLogger
requires an API key. In offline mode, the logger does not connect to Neptune.ONLINE MODE
from pytorch_lightning import Trainer from pytorch_lightning.loggers import NeptuneLogger # arguments made to NeptuneLogger are passed on to the neptune.experiments.Experiment class # We are using an api_key for the anonymous user "neptuner" but you can use your own. neptune_logger = NeptuneLogger( api_key="ANONYMOUS", project_name="shared/pytorch-lightning-integration", experiment_name="default", # Optional, params={"max_epochs": 10}, # Optional, tags=["pytorch-lightning", "mlp"], # Optional, ) trainer = Trainer(max_epochs=10, logger=neptune_logger)
OFFLINE MODE
from pytorch_lightning.loggers import NeptuneLogger # arguments made to NeptuneLogger are passed on to the neptune.experiments.Experiment class neptune_logger = NeptuneLogger( offline_mode=True, project_name="USER_NAME/PROJECT_NAME", experiment_name="default", # Optional, params={"max_epochs": 10}, # Optional, tags=["pytorch-lightning", "mlp"], # Optional, ) trainer = Trainer(max_epochs=10, logger=neptune_logger)
Use the logger anywhere in you
LightningModule
as follows:class LitModel(LightningModule): def training_step(self, batch, batch_idx): # log metrics self.logger.experiment.log_metric("acc_train", ...) # log images self.logger.experiment.log_image("worse_predictions", ...) # log model checkpoint self.logger.experiment.log_artifact("model_checkpoint.pt", ...) self.logger.experiment.whatever_neptune_supports(...) def any_lightning_module_function_or_hook(self): self.logger.experiment.log_metric("acc_train", ...) self.logger.experiment.log_image("worse_predictions", ...) self.logger.experiment.log_artifact("model_checkpoint.pt", ...) self.logger.experiment.whatever_neptune_supports(...)
If you want to log objects after the training is finished use
close_after_fit=False
:neptune_logger = NeptuneLogger(..., close_after_fit=False, ...) trainer = Trainer(logger=neptune_logger) trainer.fit() # Log test metrics trainer.test(model) # Log additional metrics from sklearn.metrics import accuracy_score accuracy = accuracy_score(y_true, y_pred) neptune_logger.experiment.log_metric("test_accuracy", accuracy) # Log charts from scikitplot.metrics import plot_confusion_matrix import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(16, 12)) plot_confusion_matrix(y_true, y_pred, ax=ax) neptune_logger.experiment.log_image("confusion_matrix", fig) # Save checkpoints folder neptune_logger.experiment.log_artifact("my/checkpoints") # When you are done, stop the experiment neptune_logger.experiment.stop()
See also
An Example experiment showing the UI of Neptune.
Tutorial on how to use Pytorch Lightning with Neptune.
- Parameters
api_key¶ (
Optional
[str
]) – Required in online mode. Neptune API token, found on https://neptune.ai. Read how to get your API key. It is recommended to keep it in the NEPTUNE_API_TOKEN environment variable and then you can leaveapi_key=None
.project_name¶ (
Optional
[str
]) – Required in online mode. Qualified name of a project in a form of “namespace/project_name” for example “tom/minst-classification”. IfNone
, the value of NEPTUNE_PROJECT environment variable will be taken. You need to create the project in https://neptune.ai first.offline_mode¶ (
bool
) – Optional defaultFalse
. IfTrue
no logs will be sent to Neptune. Usually used for debug purposes.close_after_fit¶ (
Optional
[bool
]) – Optional defaultTrue
. IfFalse
the experiment will not be closed after training and additional metrics, images or artifacts can be logged. Also, remember to close the experiment explicitly by runningneptune_logger.experiment.stop()
.experiment_name¶ (
Optional
[str
]) – Optional. Editable name of the experiment. Name is displayed in the experiment’s Details (Metadata section) and in experiments view as a column.experiment_id¶ (
Optional
[str
]) – Optional. Default isNone
. The ID of the existing experiment. If specified, connect to experiment with experiment_id in project_name. Input arguments “experiment_name”, “params”, “properties” and “tags” will be overriden based on fetched experiment data.prefix¶ (
str
) – A string to put at the beginning of metric keys.**kwargs¶ – Additional arguments like params, tags, properties, etc. used by
neptune.Session.create_experiment()
can be passed as keyword arguments in this logger.
- Raises
ImportError – If required Neptune package is not installed on the device.
- log_artifact(artifact, destination=None)[source]¶
Save an artifact (file) in Neptune experiment storage.
- log_image(log_name, image, step=None)[source]¶
Log image data in Neptune experiment
- Parameters
log_name¶ (
str
) – The name of log, i.e. bboxes, visualisations, sample_images.image¶ (
Union
[str
,Any
]) – The value of the log (data-point). Can be one of the following types: PIL image, matplotlib.figure.Figure, path to image file (str)step¶ (
Optional
[int
]) – Step number at which the metrics should be recorded, must be strictly increasing
- Return type
- log_metric(metric_name, metric_value, step=None)[source]¶
Log metrics (numeric values) in Neptune experiments.
- property experiment: neptune.experiments.Experiment¶
Actual Neptune object. To use neptune features in your
LightningModule
do the following.Example:
self.logger.experiment.some_neptune_function()
- property name: str¶
Return the experiment name.
- property save_dir: Optional[str]¶
Return the root directory where experiment logs get saved, or None if the logger does not save data locally.
- property version: str¶
Return the experiment version.