Skip to content

TensorBoard integration guide#

Open in Colab

Custom dashboard displaying metadata logged with TensorBoard

With the Neptune-TensorBoard integration, you can track metrics and log metadata to both TensorBoard and Neptune at the same time. You can also use the CLI utility to export existing TensorBoard logs to Neptune.

See example in Neptune  Code examples 

Before you start#

Installing the integration#

To use your preinstalled version of Neptune together with the integration:

pip install -U neptune-tensorboard

To install both Neptune and the integration:

pip install -U "neptune[tensorboard]"
How do I save my credentials as environment variables?

Set your Neptune API token and full project name to the NEPTUNE_API_TOKEN and NEPTUNE_PROJECT environment variables, respectively.

export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
export NEPTUNE_PROJECT="ml-team/classification"
export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
export NEPTUNE_PROJECT="ml-team/classification"
setx NEPTUNE_API_TOKEN "h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
setx NEPTUNE_PROJECT "ml-team/classification"

You can also navigate to SettingsEdit the system environment variables and add the variables there.

%env NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
%env NEPTUNE_PROJECT="ml-team/classification"

To find your credentials:

  • API token: In the bottom-left corner of the Neptune app, expand your user menu and select Get your API token. If you need the token of a service account, go to the workspace or project settings and enter the Service accounts settings.
  • Project name: Your full project name has the form workspace-name/project-name. You can copy it from the project menu ( Edit project details).

If you're working in Google Colab, you can set your credentials with the os and getpass libraries:

import os
from getpass import getpass
os.environ["NEPTUNE_API_TOKEN"] = getpass("Enter your Neptune API token: ")
os.environ["NEPTUNE_PROJECT"] = "workspace-name/project-name"

If you'd rather follow the guide without any setup, you can run the example in Colab .

Basic logging example#

In this guide, we'll show you how to use the enable_tensorboard_logging() function to log metadata to both TensorBoard and Neptune at the same time.

  1. Create a Neptune run:

    import neptune
    
    run = neptune.init_run() # (1)!
    
    1. If you haven't set up your credentials, you can log anonymously:

      neptune.init_run(
              api_token=neptune.ANONYMOUS_API_TOKEN,
              project="common/tensorboard-integration",
      )
      

    For more run customization options, see neptune.init_run().

  2. Import the logging function:

    from neptune_tensorboard import enable_tensorboard_logging
    
  3. Call the integration function, passing the Neptune run object as the argument.

    enable_tensorboard_logging(run)
    

    This will log the metadata to both the tensorboard directory and the Neptune run.

    Tip

    The function also works with:

    • SummaryWriter from PyTorch and tensorboardX
    • TensorBoard callback for Keras
  4. Proceed with your model training script. For a full example, see the TensorBoard code samples on GitHub .

  5. To stop the connection to Neptune and sync all data, call the stop() method:

    run.stop()
    
  6. Run your script as you normally would.

    To open the run, click the Neptune link that appears in the console output.

    Sample output

    [neptune] [info ] Neptune initialized. Open in the app: https://app.neptune.ai/workspace/project/e/RUN-1

    In the above example, the run ID is RUN-1.

    If Neptune can't find your project name or API token

    As a best practice, you should save your Neptune API token and project name as environment variables:

    export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8"
    
    export NEPTUNE_PROJECT="ml-team/classification"
    

    Alternatively, you can pass the information when using a function that takes api_token and project as arguments:

    run = neptune.init_run( # (1)!
        api_token="h0dHBzOi8aHR0cHM6Lkc78ghs74kl0jv...Yh3Kb8",  # your token here
        project="ml-team/classification",  # your full project name here
    )
    
    1. Also works for init_model(), init_model_version(), init_project(), and integrations that create Neptune runs underneath the hood, such as NeptuneLogger or NeptuneCallback.

    2. API token: In the bottom-left corner, expand the user menu and select Get my API token.

    3. Project name: You can copy the path from the project details ( Edit project details).

    If you haven't registered, you can log anonymously to a public project:

    api_token=neptune.ANONYMOUS_API_TOKEN
    project="common/quickstarts"
    

    Make sure not to publish sensitive data through your code!

Analyzing the results#

You'll find the logged metadata in All metadatatensorboard.

You can also see the metadata visualized in the other dashboards, as well as create your own dashboard. See the Neptune example for inspiration.

See example in Neptune 

Exporting TensorBoard logs to Neptune#

You can export TensorBoard logs from the logs directory to Neptune with the neptune tensorboard CLI command:

neptune tensorboard logs

If you haven't set your credentials as environment variables, you can also pass them as arguments:

neptune tensorboard --api_token YourNeptuneApiToken --project YourProjectName logs
How do I save my credentials as environment variables?

Set your Neptune API token and full project name to the NEPTUNE_API_TOKEN and NEPTUNE_PROJECT environment variables, respectively.

export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
export NEPTUNE_PROJECT="ml-team/classification"
export NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
export NEPTUNE_PROJECT="ml-team/classification"
setx NEPTUNE_API_TOKEN "h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
setx NEPTUNE_PROJECT "ml-team/classification"

You can also navigate to SettingsEdit the system environment variables and add the variables there.

%env NEPTUNE_API_TOKEN="h0dHBzOi8aHR0cHM.4kl0jvYh3Kb8...ifQ=="
%env NEPTUNE_PROJECT="ml-team/classification"

To find your credentials:

  • API token: In the bottom-left corner of the Neptune app, expand your user menu and select Get your API token. If you need the token of a service account, go to the workspace or project settings and enter the Service accounts settings.
  • Project name: Your full project name has the form workspace-name/project-name. You can copy it from the project menu ( Edit project details).

If you're working in Google Colab, you can set your credentials with the os and getpass libraries:

import os
from getpass import getpass
os.environ["NEPTUNE_API_TOKEN"] = getpass("Enter your Neptune API token: ")
os.environ["NEPTUNE_PROJECT"] = "workspace-name/project-name"

neptune-notebooks incompatibility

Currently, the CLI component of the integration does not work together with the Neptune-Jupyter extension (neptune-notebooks).

Until a fix is released, if you have neptune-notebooks installed, you must uninstall it to be able to use the neptune tensorboard command.


Related