Scikit-Learn

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

import bentoml

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
iris = load_iris()
X = iris.data[:, :4]
Y = iris.target
model.fit(X, Y)

# `save` a given classifier and retrieve coresponding tag:
tag = bentoml.sklearn.save('kneighbors', model)

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

# load the model back:
loaded = bentoml.sklearn.load("kneighbors:latest")

# Run a given model under `Runner` abstraction with `load_runner`
runner = bentoml.sklearn.load_runner(tag)
runner.run([[1,2,3,4,5]])

Note

You can find more examples for scikit-learn in our gallery repo.

bentoml.sklearn.save(name, model, *, 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[BaseEstimator, Pipeline]) – Instance of model to be saved.

  • 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

from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier

model = KNeighborsClassifier()
iris = load_iris()
X = iris.data[:, :4]
Y = iris.target
model.fit(X, Y)

tag = bentoml.sklearn.save('kneighbors', model)

# load the model back:
loaded = bentoml.sklearn.load("kneighbors:latest")
# or:
loaded = bentoml.sklearn.load(tag)
bentoml.sklearn.load(tag, 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.

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

Returns

an instance of sklearn model from BentoML modelstore.

Return type

Union[BaseEstimator, Pipeline]

Examples:

import bentoml

sklearn = bentoml.sklearn.load('my_model:latest')
bentoml.sklearn.load_runner(tag, function_name='predict', *, name=None)

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

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

  • function_name (str, optional, default to predict) – Predict function used by a given sklearn model.

Returns

Runner instances for the target bentoml.sklearn model

Return type

Runner

Examples:

import bentoml

runner = bentoml.sklearn.load_runner("my_model:latest")
runner.run([[1,2,3,4]])