Neptune client for R

What will you get with Neptune client for R?

You can interact with Neptune from R and enjoy almost the same functionality that is available in Python. The Neptune client for R is available as a CRAN package and a Github project.

With Neptune client for R you can:

  • save model hyperparameters for every experiment you run

  • see learning curves for losses and metrics during training

  • see hardware consumption and stdout/stderr output during training

  • log data versions and other properties

  • log model weights and other artifacts

  • log performance charts

  • tage experiments to organize them

Note

This integration is tested with R version 3.6.3, neptune_0.1.0, and Python 3.6.12.

Remember that even if you are using R you still need to have Python installed on your system.

Where to start?

To get started with using Neptune client for R, follow the quickstart below.

You can also skip the basics and take a look at how to log model weights and performance charts in the more options section.

If you want to try things out right away you can either:

Quickstart

This quickstart will show you how to:

  • Install the necessary Neptune package from CRAN

  • Connect Neptune to your training code and create the first experiment

  • Log metrics and hardware consumption to Neptune

  • Explore learning curves in the Neptune UI

Before you start

To log experiments to Neptune from R you need to satisfy the following prerequisites:

Tip

If you are using R studio you can just follow along and you will be prompted by the console to install miniconda.

Step 1: Install Neptune package

Install neptune package for R from CRAN by running the following code in your R console or script.

install.packages('neptune', dependencies = TRUE)

Step 2: Import neptune package and initialize Neptune

Add the following snippet at the top of your script.

library(neptune)

init_neptune(project_name = 'shared/r-integration', api_token = 'ANONYMOUS')

Tip

You can also use your personal API token. Read more about how to find your Neptune API token.

Note

This will use your default Python. If you are hitting Error: could not find a Python environment error or you want to use some other Python version you have on your system jump to this section and read how.

Step 3: Create an experiment

Run the code below to create a Neptune experiment:

create_experiment(name='minimal example')

This also creates a link to the experiment. Open the link in a new tab. The charts will currently be empty, but keep the window open. You will be able to see live metrics once logging starts.

Step 4: Log metrics

Log your performance metrics during or after training with the log_metric function.

log_metric('accuracy', 0.92)

for (i in 0:100){
  log_metric('random_training_metric', i * 0.6)
}
R learning curves

Step 5: Stop experiment

When you are finished logging you should stop your current Neptune experiment.

stop_experiment()

Step 6: Run your training script

Run your script as you normally would. Neptune works with Rstudio, R notebooks or R scripts.

For example:

Rscript train.R

Step 7: Go to Neptune see your training live and compare experiment runs

R compare experiments

More Options

In this section you will see how to:

Log hyperparameters

You can log training and model hyperparameters. To do that just pass the parameter list to the params argument of the create_experiment function:

params = list(ntree=625,
              mtry=13,
              maxnodes=50
              )

create_experiment(name='training on Sonar',
                  params = params
)
R hyperparameter logging

Log hardware consumption and console outputs

Neptune logs your hardware consumption and console outputs automatically if the python package psutil is installed on your system.

Note

psutil should be installed automatically when you install the neptune package but you can always do it manually with:

reticulate::py_install(packages = 'psutil')

Go to the Monitoring in your Neptune experiment to see it.

R logging hardware consumption

Tag your experiment

You can add tags to your experiments to organize them.

To do that just pass an array of tags to the tags argument of the create_experiment function:

create_experiment(name='training on Sonar',
                  tags = c('random-forest','sonar')
)

or use the append_tag function:

append_tag(c('random-forest','sonar'))
R experiment tags

Log data versions and other properties

Keeping track of your data is an important part of the job. With Neptune, you can log a fingerprint (hash) of your data for every experiment.

Add a property to your experiment:

library(digest)
set_property(property = 'data-version', value = digest(dataset))

SEED=1234
set.seed(SEED)
set_property(property = 'seed', value = SEED)
R data versioning

Log model weights and other files

You can also save your model files, PDF report files or other objects in Neptune.

All you need to do is pass the filepath to the log_artifact() method and it will be logged to your experiment.

save(model, file="model.Rdata")
log_artifact('model.Rdata')
R saving models

Log images and charts

Logging images and charts to Neptune is very simple, as well.

Just use the log_image() method that takes the name of the logging channel and a path to image as arguments. You can log more than one chart to the same channel to organize things - just send another image to the same channel.

for (t in c(1,2)){
  jpeg('importance_plot.jpeg')
  varImpPlot(model,type=t)
  dev.off()
  log_image('feature_importance', 'importance_plot.jpeg')
}
R logging images and charts

Use a non default Python path

If you don’t want to use default Python you can customize it with python and python_path arguments.

  • Python

init_neptune(project_name = 'shared/r-integration',
             api_token = 'ANONYMOUS'
             python='python',
             python_path='/usr/bin/python3')
  • venv

    init_neptune(project_name = 'shared/r-integration',
                 api_token = 'ANONYMOUS'
                 python='venv',
                 python_path='my_venv')
    
  • conda

    init_neptune(project_name = 'shared/r-integration',
                 api_token = 'ANONYMOUS'
                 python='conda',
                 python_path='my_conda_env')
    
  • miniconda

    init_neptune(project_name = 'shared/r-integration',
                 api_token = 'ANONYMOUS'
                 python='miniconda',
                 python_path='my_miniconda_env')
    

Remember that you can try it out with zero setup:

How to ask for help?

Please visit the Getting help page. Everything regarding support is there.