XGBoostΒΆ

About this page

This is an API reference for XGBoost in BentoML. Please refer to the XGBoost guide for more information about how to use XGBoost in BentoML.

Note

You can find more examples for XGBoost in our examples/xgboost directory.

bentoml.xgboost.save_model(name: Tag | str, model: xgb.Booster | xgb.XGBModel, *, signatures: ModelSignaturesType | None = None, labels: dict[str, str] | None = None, custom_objects: dict[str, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, metadata: dict[str, t.Any] | None = None) bentoml.ModelΒΆ

Save an XGBoost model instance to the BentoML model store.

Parameters:
  • name – The name to give to the model in the BentoML store. This must be a valid Tag name.

  • model – The XGBoost model to be saved.

  • signatures – Signatures of predict methods to be used. If not provided, the signatures default to {"predict": {"batchable": False}}. See ModelSignature for more details.

  • labels – A default set of management labels to be associated with the model. An example is {"training-set": "data-1"}.

  • custom_objects –

    Custom objects to be saved with the model. An example is {"my-normalizer": normalizer}.

    Custom objects are currently serialized with cloudpickle, but this implementation is subject to change.

  • external_modules – user-defined additional python modules to be saved alongside the model or custom objects, e.g. a tokenizer module, preprocessor module, model configuration module

  • metadata –

    Metadata to be associated with the model. An example is {"max_depth": 2}.

    Metadata is intended for display in model management UI and therefore must be a default Python type, such as str or int.

Returns:

A BentoML tag with the user-defined name and a generated version.

Example:

import xgboost as xgb
import bentoml

# read in data
dtrain = xgb.DMatrix('demo/data/agaricus.txt.train')
dtest = xgb.DMatrix('demo/data/agaricus.txt.test')
# specify parameters via map
param = dict(max_depth=2, eta=1, objective='binary:logistic')
num_round = 2
bst = xgb.train(param, dtrain, num_round)
...

# `save` the booster to BentoML modelstore:
bento_model = bentoml.xgboost.save_model("my_xgboost_model", bst, booster_params=param)
bentoml.xgboost.load_model(bento_model: str | Tag | bentoml.Model) xgb.Booster | xgb.XGBModelΒΆ

Load the XGBoost model with the given tag from the local BentoML model store.

Parameters:

bento_model (str | Tag | Model) – Either the tag of the model to get from the store, or a BentoML ~bentoml.Model instance to load the model from.

Returns:

The XGBoost model loaded from the model store or BentoML Model.

Example:

import bentoml
# target model must be from the BentoML model store
booster = bentoml.xgboost.load_model("my_xgboost_model")
bentoml.xgboost.get(tag: t.Union[Tag, str], *, _model_store: ModelStore = <simple_di.providers.SingletonFactory object>, model_aliases: t.Dict[str, str] = <simple_di.providers.Static object>) ModelΒΆ

Get a model by tag. If the tag is a string, it will be looked up in the model_aliases dict.

bentoml.xgboost.get_service(model_name: str, **config: Unpack[ServiceConfig]) Service[t.Any]ΒΆ

Get a BentoML service instance from an XGBoost model.

Parameters:
  • model_name (str) – The name of the model to get the service for.

  • **config (Unpack[ServiceConfig]) – Configuration options for the service.

Returns:

A BentoML service instance that wraps the XGBoost model.

Example:

import bentoml

service = bentoml.xgboost.get_service("my_xgboost_model")