Neptune-LightGBM Integration

What will you get with this integration?

LightGBM is a popular gradient boosting library.

With the Neptune + LightGBM integration, you can:

  • log training and evaluation metrics

  • vizualize learning curves for losses and metrics during training

  • see hardware consumption during training

  • save model artifacts

Tip

You can log many other experiment metadata like interactive charts, video, audio, and more. See the full list.

Note

This integration is tested with neptune-client==0.4.132, neptune-contrib==0.25.0, and lightgbm==2.2.3

Quickstart

This quickstart will show you how to:

  • Install the necessary Neptune packages

  • Log lightGBM metrics to and visualize them with Neptune

Before you start

  1. Ensure that you have Python 3.x and following libraries installed:

    pip install --quiet lightgbm==2.2.3 neptune-client neptune-contrib[monitoring]
    
  2. You also need minimal familiarity with LightGBM. Have a look at the LightGBM Quickstart guide to get started.

Step 1: Initialize Neptune

import neptune

neptune.init(api_token='ANONYMOUS', project_qualified_name='shared/LightGBM-integration')

Tip

You can also use your personal API token. Read more about how to securely set the Neptune API token.

Step 2: Create an Experiment

neptune.create_experiment(name='LightGBM-training')

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 3: Pass neptune_monitor to lgb.train

Simply pass neptune_monitor to the callbacks argument of lgb.train

from neptunecontrib.monitoring.lightgbm import neptune_monitor

gbm = lgb.train(params,
        lgb_train,
        num_boost_round = 500,
        valid_sets = [lgb_train, lgb_eval],
        valid_names = ['train','valid'],
        callbacks = [neptune_monitor()], # Just add this callback
       )

Step 4: Monitor your LightGBM training in Neptune

Now you can switch to the Neptune tab which you had opened previously to watch the training live!

LightGBM neptune.ai integration

More Options

Log hyperparameters

You can log training and model hyperparameters. To do that just pass the parameter dictionary to create_experiment() method:

params = {'boosting_type': 'gbdt',
      'objective': 'multiclass',
      'num_class': 3,
      'num_leaves': 31,
      'learning_rate': 0.05,
      'feature_fraction': 0.9
      }

neptune.create_experiment(name='LightGBM-training', params=params)
LightGBM hyperparameter logging

Save Model artifacts

You can save model artifacts to Neptune both during and after training.

To do that just use a log_artifact() method on the saved model file.

gbm.save_model('lightgbm.pkl')

# Log model
neptune.log_artifact('lightgbm.pkl')
LightGBM model artifacts

Log interactive charts

You can log interactive charts to Neptune.

For example, let’s log an interactive ROC AUC curve.

  1. Install dependencies

pip install neptune-contrib scikit-plot matplotlib==3.2.0 plotly==4.12.0

Warning

Automatic conversion between matplotlib figures and plotly charts works only with matplotlib==3.2.0 and plotly==4.12.0.

  1. Create an ROC AUC curve

import matplotlib.pyplot as plt
from scikitplot.metrics import plot_roc

y_test_pred = model.predict(x_test)

fig, ax = plt.subplots()
plot_roc(y_test, y_test_pred, ax=ax)
  1. Log it to Neptune via log_chart() function.

from neptunecontrib.api import log_chart

log_chart(name='ROC curve', chart=fig)
LightGBM logging interactive html charts

How to ask for help?

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