H2O¶
Users can now use H2O with BentoML with the following API: load
, save
, and load_runner
as follow:
import bentoml
import h2o
import h2o.model
import h2o.automl
H2O_PORT = 54323
def train_h2o_aml() -> h2o.automl.H2OAutoML:
h2o.init(port=H2O_PORT)
h2o.no_progress()
df = h2o.import_file(
"https://github.com/yubozhao/bentoml-h2o-data-for-testing/raw/master/"
"powerplant_output.csv"
)
splits = df.split_frame(ratios=[0.8], seed=1)
train = splits[0]
test = splits[1]
aml = h2o.automl.H2OAutoML(
max_runtime_secs=60, seed=1, project_name="powerplant_lb_frame"
)
aml.train(y="HourlyEnergyOutputMW", training_frame=train, leaderboard_frame=test)
return aml
model = train_h2o_aml()
# `save` a model to BentoML modelstore:
tag = bentoml.h2o.save("h2o_model", model.leader)
# retrieve metadata with `bentoml.models.get`:
metadata = bentoml.models.get(tag)
# `load` the model back in memory:
h2o_loaded: h2o.model.model_base.ModelBase = bentoml.h2o.load(
tag,
init_params=dict(port=H2O_PORT),
)
Note
You can find more examples for H2O in our gallery repo.
- bentoml.h2o.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 (h2o.model.model_base.ModelBase) – Instance of h2o model to be saved.
labels (
Dict[str, str]
, optional, default toNone
) – user-defined labels for managing models, e.g. team=nlp, stage=devcustom_objects (
Dict[str, Any]]
, optional, default toNone
) – user-defined additional python objects to be saved alongside the model, e.g. a tokenizer instance, preprocessor function, model configuration jsonmetadata (
Dict[str, Any]
, optional, default toNone
) – Custom metadata for given model.model_store (
ModelStore
, default toBentoMLContainer.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
Examples:
import bentoml import h2o import h2o.model import h2o.automl H2O_PORT = 54323 def train_h2o_aml() -> h2o.automl.H2OAutoML: h2o.init(port=H2O_PORT) h2o.no_progress() df = h2o.import_file( "https://github.com/yubozhao/bentoml-h2o-data-for-testing/raw/master/" "powerplant_output.csv" ) splits = df.split_frame(ratios=[0.8], seed=1) train = splits[0] test = splits[1] aml = h2o.automl.H2OAutoML( max_runtime_secs=60, seed=1, project_name="powerplant_lb_frame" ) aml.train(y="HourlyEnergyOutputMW", training_frame=train, leaderboard_frame=test) return aml model = train_h2o_aml() tag = bentoml.h2o.save("h2o_model", model.leader)
- bentoml.h2o.load(tag, init_params=None, model_store=<simple_di.providers.SingletonFactory object>)¶
Load a model from BentoML local modelstore with given tag.
- Parameters
tag (
Union[str, Tag]
) – Tag of a saved model in BentoML local modelstore.init_params (
Dict[str, Union[str, Any]]
, optional, defaults to None) – Params for h2o server initializationmodel_store (
ModelStore
, default toBentoMLContainer.model_store
) – BentoML modelstore, provided by DI Container.
- Returns
an instance of h2o.model.model_base.ModelBase from BentoML modelstore.
- Return type
h2o.model.model_base.ModelBase
Examples:
import bentoml model = bentoml.h2o.load(tag, init_params=dict(port=54323))
- bentoml.h2o.load_runner(tag, predict_fn_name='predict', *, init_params, name=None)¶
Runner represents a unit of serving logic that can be scaled horizontally to maximize throughput. bentoml.h2o.load_runner implements a Runner class that wrap around a h2o 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 topredict
) – Options for inference functions. Default to predictinit_params (
Dict[str, Union[str, Any]]
, default toNone
) – Parameters for h2o.init(). Refers to H2O Python API for more information
- Returns
Runner instances for
bentoml.h2o
- Return type
Runner
Examples:
import bentoml runner = bentoml.h2o.load_runner("h2o_model") runner.run_batch(data)