pytorch_lightning.loggers.neptune module¶
Neptune¶
-
class
pytorch_lightning.loggers.neptune.
NeptuneLogger
(api_key=None, project_name=None, close_after_fit=True, offline_mode=False, experiment_name=None, upload_source_files=None, params=None, properties=None, tags=None, **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
Example
>>> 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
Example
>>> 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:>>> from pytorch_lightning import LightningModule >>> 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.upload_source_files¶ (
Optional
[List
[str
]]) – Optional. List of source files to be uploaded. Must be list of str or single str. Uploaded sources are displayed in the experiment’s Source code tab. IfNone
is passed, the Python file from which the experiment was created will be uploaded. Pass an empty list ([]
) to upload no files. Unix style pathname pattern expansion is supported. For example, you can pass'\*.py'
to upload all python source files from the current directory. For recursion lookup use'\**/\*.py'
(for Python 3.5 and later). For more information seeglob
library.params¶ (
Optional
[Dict
[str
,Any
]]) – Optional. Parameters of the experiment. After experiment creation params are read-only. Parameters are displayed in the experiment’s Parameters section and each key-value pair can be viewed in the experiments view as a column.properties¶ (
Optional
[Dict
[str
,Any
]]) – Optional. Default is{}
. Properties of the experiment. They are editable after the experiment is created. Properties are displayed in the experiment’s Details section and each key-value pair can be viewed in the experiments view as a column.tags¶ (
Optional
[List
[str
]]) – Optional. Default is[]
. Must be list of str. Tags of the experiment. They are editable after the experiment is created (see:append_tag()
andremove_tag()
). Tags are displayed in the experiment’s Details section and can be viewed in the experiments view as a column.
Appends tags to the neptune experiment.
-
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
None
-
log_metric
(metric_name, metric_value, step=None)[source]¶ Log metrics (numeric values) in Neptune experiments.
-
property
experiment
[source]¶ Actual Neptune object. To use neptune features in your
LightningModule
do the following.Example:
self.logger.experiment.some_neptune_function()
- Return type
Experiment