R package setup and tutorial#
Deprecated integration
This integration is not actively maintained and does not work on Windows.
Unless there's a clear signal from the user community, we do not plan on updating it.
This tutorial walks you through installing and using the Neptune R client package.
Installing the Neptune client package#
Underneath the hood, the Neptune package uses Python through the reticulate package.
The following one-time setup is needed. If you already have a Python environment that you want to use, skip to Using an existing virtual environment.
Creating a new virtual Python environment#
The following installs miniconda (a minimalistic Python environment) and sets it up for you:
install.packages("reticulate")
library(reticulate)
install_miniconda()
install.packages("neptune")
library(neptune)
neptune_install()
To work with Neptune after this initial setup, you only need to run the last two lines:
Using an existing virtual environment#
If you have a virtual Python environment (conda, miniconda, virtualenv, or venv) already set up, you can use it instead of creating a new one. All the needed Python libraries will be installed there.
For details on creating virtual environments, see the Python documentation .
Creating a run#
The code below creates a run in the specified project. You log metadata by assigning it to the run
variable in your code.
run <- neptune_init_run(
project = "workspace-name/project-name",
api_token = "Your Neptune API token"
)
Replace the project
and api_token
arguments with your own information.
To find and copy your project name:
- In the Neptune app, navigate to your project.
- Open the settings menu ( ) and select Details & privacy.
- Copy the name.
To find and copy your API token:
- In the Neptune app, in the bottom-left corner, expand your user menu.
- Select Get your API token.
- Copy the token.
Haven't registered?
If you haven't registered and want to start a run anonymously in a public project, you can initialize Neptune in the following way:
Saving your credentials as environment variables#
As a best practice in your workflow, set your Neptune API token and full project name to the NEPTUNE_API_TOKEN
and NEPTUNE_PROJECT
environment variables, respectively.
You can then leave the credentials out of your neptune_init_run()
command.
You can also set the API token for the current session with the following R command:
Tracking metadata#
Log hyperparamaters#
If you have parameters in the form of a dictionary, you can log them to Neptune in batch:
params <- list(
"dense_units" = 128,
"activation" = "relu",
"dropout" = 0.23,
"learning_rate" = 0.15,
"batch_size" = 64,
"n_epochs" = 30
)
run["parameters"] <- params
It will create a field with an appropriate type for each dictionary entry. You can update the parameters or add new ones later in the code:
run["model/parameters/n_epochs"] <- epoch
Log training metrics#
You can log training metrics to Neptune using series fields. In Neptune there are three types of series: FloatSeries
, StringSeries
, and FileSeries
.
Each neptune_append()
adds a new value at the end of the series:
for (i in 1:epochs) {
[...] # My training loop
neptune_append(run["train/epoch/loss"], loss)
neptune_append(run["train/epoch/accuracy"], acc)
}
You can also use reticulate-based syntax:
Related
To learn more about field types, see the Field types reference.
Log evaluation results#
To log evaluation metrics, assign them to a field of your choice. In the snippet below, both evaluation metrics will be stored in the same evaluation
namespace.
You can log plots and charts using the neptune_upload()
function.
A ggplot object will be converted to an image file, but you can also upload images from the local disk.
You can upload ggplot plots directly without saving them to a file:
If you want to control additional parameters, like the size of the plot, you can pass the same arguments as to ggsave
:
You can also use reticulate-based syntax:
Upload model file#
You can upload any binary file from disk using the neptune_upload()
method.
If your content consists of multiple files, you can upload a whole folder as a FileSet
with neptune_upload_files()
.
Track artifacts#
The neptune_track_files()
function logs artifact metadata: version (hash), location (path), size, folder structure, and contents. It works for files, folders, and S3-compatible storage.
It's a handy alternative when you want to track an artifact without uploading it in full.
Tag the run#
You can add tags to the run with the neptune_add()
function:
Stopping the run#
Once you are finished with tracking metadata, stop the connection for that particular run:
Note
If you're executing a script, tracking stops automatically at the end.
In interactive environments such as RStudio, however, you need to stop runs explicitly with neptune_stop()
.
Exploring the results#
Head to the app to explore your tracked runs.
- In the experiments table, click on a run to display the logged metadata.
- To compare metadata between runs, toggle the eye icons ( ).
Related
Querying metadata#
To resume a run in order to fetch metadata from it:
resumed_run <- neptune_init_run(
api_token = ANONYMOUS_API_TOKEN,
project = "common-r/quickstarts",
run = "HEL-3",
mode = "read-only" # (1)!
)
- You can always initialize Neptune objects in read-only mode if you're only fetching data from them and not adding to or modifying the data.
Single values#
Use neptune_fetch()
for single values:
Last value from series#
Downloading files#
Anything uploaded, you can download.
If you have logged your images as a series, you can use neptune_download_last()
to access only the last file:
Fetching all runs as table#
To fetch the metadata of all your runs as a table:
Related
- Example scripts: Neptune R examples on GitHub
- API reference: Neptune package CRAN page
- Query metadata via the Neptune API
- Download metadata from the Neptune web app