API reference guide
Overview of the Neptune client which helps you log, query, and download model-building metadata
The Neptune client is an open-source Python library. With the Neptune API, you can:
  • Record model-building metadata to Neptune
  • Query and download model-building metadata from Neptune
  • Manage Neptune projects and users
The API is lightweight and designed to integrate with your MLOps tech stack in a flexible manner. It's up to you to define what to log for each ML run and in what kind of structure.

Working with the Neptune API

To get started, install the Neptune client library:
pip
conda
1
pip install neptune-client
Copied!
1
conda install -c conda-forge neptune-client
Copied!

Recording metadata to Neptune

In your model training script, import Neptune and start a run:
example_training_script.py
1
import neptune.new as neptune
2
3
run = neptune.init(
4
project="WORKSPACE_NAME/PROJECT_NAME",
5
api_token="NEPTUNE_API_TOKEN",
6
)
Copied!
where
  • PROJECT_NAME is the name of the Neptune project to record metadata to. For example, computer-vision.
  • WORKSPACE_NAME is the name of the workspace where your project is located. For example, ML-research-group.
  • NEPTUNE_API_TOKEN is the API token associated with your Neptune account. You can find it by clicking your avatar icon in the top-right corner of the Neptune app.
You can omit the project and api_token arguments if you have saved them as environment variables. For instructions, see Installation and setup.
Define some metadata and hyperparameters to track for the experiment and record them to the run object:
example_training_script.py
1
...
2
3
run["JIRA"] = "NPT-952"
4
5
params = {
6
"batch_size": 64,
7
"dropout": 0.2,
8
"optimizer": {"learning_rate": 0.001, "optimizer": "Adam"},
9
}
10
11
run["parameters"] = params
Copied!
Track the training process by logging your training metrics and the final score:
example_training_script.py
1
...
2
3
for epoch in range(100):
4
run["train/accuracy"].log(epoch * 0.6)
5
run["train/loss"].log(epoch * 0.4)
6
7
run["f1_score"] = 0.66
Copied!
If you're running the experiment in a Jupyter notebook, once the training and evaluation is done, end the run with the stop() method.
1
run.stop()
Copied!
Execute your script:
1
python example_training_script.py
Copied!
Follow the link in the output to see the tracked run and metadata in Neptune.

Querying metadata from Neptune

To query metadata from a given run, initialize Neptune with an existing run id:
1
import neptune.new as neptune
2
3
run = neptune.init(
4
project="WORKSPACE_NAME/PROJECT_NAME",
5
api_token="NEPTUNE_API_TOKEN",
6
run="RUN_ID",
7
)
Copied!
where
  • WORKSPACE_NAME/PROJECT_NAME is the name of the Neptune workspace and project where your run is located. For example, ML-research-group/computer-vision.
  • NEPTUNE_API_TOKEN is the API token associated with your Neptune account. You can find it by clicking your avatar icon in the top-right corner of the Neptune app.
  • RUN_ID is the Neptune id of the run you want to access. For example, CV-17.
You can omit the above arguments if you have saved them as environment variables. For instructions, see Installation and setup.
Next, use the fetch() method to query any metadata for the run:
1
params = run["parameters"].fetch()
2
f1_score = run["f1_score"].fetch()
Copied!

Related pages