Skip to content

Working with Google Colab#

Open in Colab

Google Colaboratory is a temporary runtime environment.

By running your model training in a Colab notebook and keeping track of it with Neptune, you can log and access your metadata and artifacts even after the Colab kernel stops.

Note on code snapshotting

Neptune cannot access the notebook code from within the notebook itself.

If you want Neptune to snapshot the code you execute, you need to:

  1. Work in a JupyterLab or Jupyter Notebook environment, with the neptune-notebooks extension installed. For instructions, see Working with Jupyter.
  2. Run your notebook locally and pass its path to the source_files argument when initializing Neptune.
  3. Work with Neptune in a Python script that you execute locally.

To use Neptune in your Colab notebook:

  1. Before your code cells, install the Neptune client library:

    ! pip install neptune-client
    
  2. Import Neptune:

    import neptune.new as neptune
    
  3. Set your Neptune API token:

    from getpass import getpass
    my_api_token = getpass("Enter your Neptune API token: ")  # (1)
    
    1. If you haven't registered, you can set it to neptune.ANONYMOUS_API_TOKEN to enable anonymous logging.
    How do I find my API token?

    In the top-right corner of the Neptune app, click your avatar and select Get your API token.

    You can copy your token from the dialog that opens. It's very long – make sure to copy and paste it in full!

  4. Set your project name:

    my_project = "workspace-name/project-name"
    
    How do I find my project name?

    Your full project name has the form workspace-name/project-name.

    For example, if your workspace name (shown in the top-left corner) is "ml-team" and your project is named "classification", your project string is: "ml-team/classification".

    To find and copy the name in Neptune, navigate to your projectSettingsProperties.

  5. Initialize a Neptune object, such as a Run.

    Pass your API token and project variables as arguments:

    run = neptune.init_run(
        api_token=my_api_token,
        project=my_project,
    )
    

    Tip

    If you want to store your model metadata in the model registry, you can also work with Model objects. For instructions, see Model registry overview.

  6. Execute the cell and you should see a Neptune link printed to the console output.

    Click the link to see the metadata appear as it gets logged.

  7. You can now use the run object to log values and metrics, upload files, and track artifacts.

    Try it out in the Colab example .

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

    run.stop()
    
    Warning

    Always call stop() in interactive environments, such as a Python interpreter or Jupyter notebook. The connection to Neptune is not stopped when the cell has finished executing, but rather when the entire notebook stops.

    If you're running a script, the connection is stopped automatically when the script finishes executing. However, it's a best practice to call stop() when the connection is no longer needed.

See in Neptune  Code example