Working with 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.
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:
- Work in a JupyterLab or Jupyter Notebook environment, with the neptune-notebooks extension installed. For instructions, see Working with Jupyter.
- 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.
To use Neptune in your Colab notebook:
-
Before your code cells, install the Neptune client library:
-
Import Neptune:
-
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 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!
- 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 in Neptune, navigate to your project → Settings → Properties.
-
Initialize a Neptune object, such as a
Run
.Pass your API token and project variables as arguments:
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. -
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.
-
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:Note for interactive sessions
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.