Skip to content

API reference: PyTorch Ignite integration#

The NeptuneLogger class provided by the PyTorch Ignite library captures metadata that is generated when training and validating models with Ignite.

Related


NeptuneLogger#

Creates a Neptune handler to log metrics, model and optimizer parameters, and gradients during the training and validation. It can also log model checkpoints to Neptune.

Parameters

Name Type Default Description
api_token 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.
**neptune_run_kwargs str, optional - Additional keyword arguments to be passed directly to the init_run() function, such as description and tags.

Examples

If you have your Neptune credentials saved as environment variables, the following starts the Neptune logger with default settings:

from ignite.contrib.handlers.neptune_logger import *

neptune_logger = NeptuneLogger()

neptune_logger.attach_output_handler(
    trainer,
    ...,
)

trainer.run()

You can also pass more options to the run created by the logger:

neptune_logger = NeptuneLogger(
    project="workspace-name/project-name", # (1)!
    name="My first Ignite run",
    description="Quick training run with Ignite",
    tags=["training", "low LR"],
)
  1. The full project name. For example, "ml-team/classification".

    To copy it, navigate to the project settings in the top-right () and select Properties.

To log metadata to the Neptune run, access the neptune_logger.experiment attribute. You can then use any logging methods from the Neptune client library to track your metadata, such as append(), assign() (=), and upload().

metadata = ...
neptune_logger.experiment["your/metadata/structure"] = metadata
...
neptune_logger.close()

NeptuneSaver#

Handler that saves an input checkpoint to the Neptune server.

Windows note

NeptuneSaver is currently not supported on Windows.

Parameters

Name Type Default Description
neptune_logger NeptuneLogger - An instance of the NeptuneLogger class.

Example

Set up the logger:

from ignite.contrib.handlers.neptune_logger import *

neptune_logger = NeptuneLogger()
...
evaluator = create_supervised_evaluator(model, metrics=metrics, ...)
...


def score_function(engine):
    return engine.state.metrics["accuracy"]


to_save = {"model": model}

Pass the Neptune logger to the saver:

from ignite.handlers import Checkpoint

handler = Checkpoint(
    to_save,
    NeptuneSaver(neptune_logger), n_saved=2,
    filename_prefix="best", score_function=score_function,
    score_name="validation_accuracy",
    global_step_transform=global_step_from_engine(trainer),
)
evaluator.add_event_handler(Events.COMPLETED, handler)

Close the logger when done:

neptune_logger.close()

You can access example checkpoints and download them from this example run.