CatBoost

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

import bentoml
import catboost as cbt
import pandas as pd

from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer()

X = cancer.data
y = cancer.target

clf = cbt.CatBoostClassifier(
    iterations=2,
    depth=2,
    learning_rate=1,
    loss_function="Logloss",
    verbose=False,
)

# train the model
clf.fit(X, y)

# `save` a given classifier and retrieve coresponding tag:
tag = bentoml.catboost.save("cancer_clf", clf)

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

# `load` the model back in memory:
model = bentoml.catboost.load("cancer_clf:latest")

# Run a given model under `Runner` abstraction with `load_runner`
input_data = pd.read_csv("/path/to/csv")
runner = bentoml.catboost.load_runner("cancer_clf:latest")
runner.run(cbt.Pool(input_data))

Note

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

bentoml.catboost.save(name, model, *, model_params=None, model_export_parameters=None, model_pool=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[catboost.core.CatBoost, catboost.core.CatBoostClassifier, catboost.CatBoostRegressor]) – Instance of model to be saved

  • model_params (Dict[str, Union[str, Any]], optional, default to None) –

    Parameters for a CatBoost model. Following parameters can be specified:
    • model_type(str): classifier (CatBoostClassifier) or regressor (CatBoostRegressor)

  • model_export_parameters (Dict[str, Union[str, Any]], optional, default to None) – Export parameters for given model.

  • model_pool (cbt.core.Pool, optional, default to None) – CatBoost data pool for given model.

  • 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:

from sklearn.datasets import load_breast_cancer

import catboost as cbt

# read in data
cancer = load_breast_cancer()
X = cancer.data
y = cancer.target

# create and train model
model = cbt.CatBoostClassifier(iterations=2,
                           depth=2,
                           learning_rate=1,
                           loss_function='Logloss',
                           verbose=True)

model.fit(X, y)
...

tag = bentoml.catboost.save("my_catboost_model", model, model_params=dict(model_type="classifier"))

# load the booster back:
loaded = bentoml.catboost.load("my_catboost_model:latest")
# or:
loaded = bentoml.catboost.load(tag)
bentoml.catboost.load(tag, model_params=None, model_store=<simple_di.providers.SingletonFactory object>)

Load a CatBoost model from BentoML local modelstore with given name.

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

  • model_params (Dict[str, Union[str, Any]], optional, default to None) –

    Parameters for a CatBoost model. Following parameters can be specified:

    • model_type(str): classifier (CatBoostClassifier) or regressor (CatBoostRegressor)

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

Returns

one of catboost.core.CatBoostClassifier, catboost.core.CatBoostRegressor or catboost.core.CatBoost from BentoML modelstore.

Return type

Union[catboost.core.CatBoost, catboost.core.CatBoostClassifier, catboost.core.CatBoostRegressor]

Examples:

import bentoml
booster = bentoml.catboost.load("my_model:latest", model_params=dict(model_type="classifier"))
bentoml.catboost.load_runner(tag, predict_fn_name='predict', *, model_params=None, name=None)

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

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

  • predict_fn_name (str, default to predict) – Options for inference functions. predict are the default function.

  • model_params (Dict[str, Union[str, Any]], optional, default to None) –

    Parameters for a CatBoost model. Following parameters can be specified:

    • model_type(str): classifier (CatBoostClassifier) or regressor (CatBoostRegressor)

Returns

Runner instances for bentoml.catboost model

Return type

Runner

Examples:

import catboost as cbt
import pandas as pd

input_data = pd.read_csv("/path/to/csv")
runner = bentoml.catboost.load_runner("my_model:latest"")
runner.run(cbt.Pool(input_data))