How to use Neptune in Google 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.
To use Neptune in your Colab notebook:
-
Before your code cells, install the Neptune client library:
-
Import the Neptune client library:
-
Set your Neptune API token:
- 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 bottom-left corner of the Neptune app, open the user menu 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!
- If you haven't registered, you can set it to
-
Set your 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 copy the name to your clipboard, navigate to the project details ( → Details & privacy).
- You can also find a pre-filled
project
string in Experiments → Create a new run.
-
Initialize a Neptune
Run
.Pass your API token and project variables as arguments:
-
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.
Sample output
[neptune] [info ] Neptune initialized. Open in the app:
https://app.neptune.ai/workspace/project/e/RUN-1
-
You can now use the
run
object to log values and metrics, upload files, and track artifacts.Try it out in the Colab example .
-
To stop the connection to Neptune and sync all data, call the
stop()
method:
Jupyter Notebook example 
Setting your Neptune credentials as environment variables#
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"
Note that these won't persist when the notebook kernel is terminated. You'll need to declare the variables again if you restart the notebook.
Snapshotting source code#
In Colab, Neptune cannot access the notebook code from within the notebook itself.
You can use Neptune to snapshot the executed code in the following ways:
- Run your notebook locally and pass its path to the
source_files
argument when initializing Neptune. - Work with Neptune in a Python script that you execute locally.
Related