LightGBM

Users can now use LightGBM with BentoML with the following API: load, save, and load_runner as follow:

import bentoml
import lightgbm as lgb
import pandas as pd

# load a dataset
df_train = pd.read_csv("regression.train", header=None, sep="\t")
df_test = pd.read_csv("regression.test", header=None, sep="\t")

y_train = df_train[0]
y_test = df_test[0]
X_train = df_train.drop(0, axis=1)
X_test = df_test.drop(0, axis=1)

# create dataset for lightgbm
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)

# specify your configurations as a dict
params = {
   "boosting_type": "gbdt",
   "objective": "regression",
   "metric": {"l2", "l1"},
   "num_leaves": 31,
   "learning_rate": 0.05,
}

# train
gbm = lgb.train(
   params, lgb_train, num_boost_round=20, valid_sets=lgb_eval
)

# `save` a given classifier and retrieve coresponding tag:
tag = bentoml.lightgbm.save("my_lightgbm_model", gbm, booster_params=params)

# retrieve metadata with `bentoml.models.get`:
metadata = bentoml.models.get(tag)

# `load` the model back in memory:
model = bentoml.lightgbm.load("my_lightgbm_model")

# Run a given model under `Runner` abstraction with `load_runner`
input_data = pd.from_csv("/path/to/csv")
runner = bentoml.lightgbm.load_runner("my_lightgbm_model:latest")
runner.run(input_data)

Note

You can find more examples for LightGBM in our gallery repo.

bentoml.lightgbm.save(name, model, *, booster_params=None, labels=None, custom_objects=None, metadata=None)

Save a model instance to BentoML modelstore.

Parameters
  • name (str) – Name for given model instance. This should pass Python identifier check.

  • model (Union[lightgbm.basic.Booster, lightgbm.LGBMModel, lightgbm.LGBMClassifier, lightgbm.LGBMRegressor, lightgbm.LGBMRanker]) – Instance of model to be saved.

  • booster_params (Dict[str, Union[str, int]], optional, defaults to None) – Parameters for boosters. Refers to Parameters Doc for more information.

  • labels (Dict[str, str], optional, default to None) – user-defined labels for managing models, e.g. team=nlp, stage=dev

  • custom_objects (Dict[str, Any]], optional, default to None) – user-defined additional python objects to be saved alongside the model, e.g. a tokenizer instance, preprocessor function, model configuration json

  • metadata (Dict[str, Any], optional, default to None) – Custom metadata for given model.

  • model_store (ModelStore, default to BentoMLContainer.model_store) – BentoML modelstore, provided by DI Container.

Returns

A tag with a format name:version where name is the user-defined model’s name, and a generated version by BentoML.

Return type

Tag

Examples:

import bentoml

import lightgbm as lgb
import pandas as pd

# load a dataset
df_train = pd.read_csv("regression.train", header=None, sep="   ")
df_test = pd.read_csv("regression.test", header=None, sep="     ")

y_train = df_train[0]
y_test = df_test[0]
X_train = df_train.drop(0, axis=1)
X_test = df_test.drop(0, axis=1)

# create dataset for lightgbm
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_test, y_test, reference=lgb_train)

# specify your configurations as a dict
params = {
    "boosting_type": "gbdt",
    "objective": "regression",
    "metric": {"l2", "l1"},
    "num_leaves": 31,
    "learning_rate": 0.05,
}

# train
gbm = lgb.train(
    params, lgb_train, num_boost_round=20, valid_sets=lgb_eval
)

tag = bentoml.lightgbm.save("my_lightgbm_model", gbm, booster_params=params)
bentoml.lightgbm.load(tag, booster_params=None, model_store=<simple_di.providers.SingletonFactory object>)

Load a model from BentoML local modelstore with given name.

Parameters
  • tag (Union[str, Tag]) – Tag of a saved model in BentoML local modelstore.

  • booster_params (Dict[str, Union[str, int]], optional, defaults to None) – Parameters for boosters. Refers to Parameters Docs for more information.

  • model_store (ModelStore, default to BentoMLContainer.model_store) – BentoML modelstore, provided by DI Container.

Returns

obj:Union[lightgbm.LGBMModel, lightgbm.LGBMClassifier, lightgbm.LGBMRegressor, lightgbm.LGBMRanker, lightgbm.basic.Booster]: An instance of either :obj:Union[lightgbm.LGBMModel, lightgbm.LGBMClassifier, lightgbm.LGBMRegressor, lightgbm.LGBMRanker] or lightgbm.basic.Booster from BentoML modelstore.

Return type

Union[lgb.basic.Booster, _LightGBMModelType]

Examples:

import bentoml
gbm = bentoml.lightgbm.load("my_lightgbm_model:latest")
bentoml.lightgbm.load_runner(tag, infer_api_callback='predict', *, booster_params=None, name=None)

Runner represents a unit of serving logic that can be scaled horizontally to maximize throughput. bentoml.lightgbm.load_runner() implements a Runner class that wrap around a LightGBM model, which optimize it for the BentoML runtime.

Parameters
  • tag (Union[str, Tag]) – Tag of a saved model in BentoML modelstore.

  • infer_api_callback (str, optional, default to predict) – Inference API callback from given model. If not specified, BentoML will use default predict. Users can also choose to use predict_proba for supported model.

  • booster_params (Dict[str, Union[str, int]], optional, defaults to None) –

    Parameters for boosters. Refers to Parameters Doc for more information.

Returns

Runner instances for bentoml.lightgbm model

Return type

Runner

Examples:

import bentoml

runner = bentoml.lightgbm.load_runner("my_lightgbm_model:latest")
runner.run_batch(X_test, num_iteration=gbm.best_iteration)