MLflow

关于此页

这是在 BentoML 中使用 MLflow 的 API 参考。有关如何在 BentoML 中使用 MLflow 的更多信息,请参阅 MLflow 指南

注意

您可以在我们的 examples/mlflow 目录下找到更多 MLflow 的示例。

bentoml.mlflow.import_model(name: Tag | str, model_uri: str, *, signatures: dict[str, ModelSignature] | dict[str, ModelSignatureDict] | 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

将 MLflow 模型从 artifact URI 导入到 BentoML 模型库。

参数:
  • name – 在 BentoML 库中赋予模型的名称。这必须是有效的 Tag 名称。

  • model_uri – 要保存的 MLflow 模型。

  • signatures – 要使用的 predict 方法的签名。如果未提供,签名默认为 {“predict”: {“batchable”: False}}。有关更多详细信息,请参阅 ModelSignature

  • labels – 与模型关联的一组默认管理标签。例如:{"training-set": "data-v1"}

  • custom_objects – 要随模型一起保存的自定义对象。例如 {"my-normalizer": normalizer}。自定义对象使用 cloudpickle 序列化。

  • metadata

    与模型关联的元数据。例如 {"param_a": .2}

    元数据旨在用于模型管理 UI 中显示,因此元数据字典中的所有值都必须是 Python 基本类型,例如 strint

返回值:

一个 Model 实例,引用了本地 BentoML 模型库中保存的模型。

示例

import bentoml

bentoml.mlflow.import_model(
    'my_mlflow_model',
    model_uri="runs:/<mlflow_run_id>/run-relative/path/to/model",
    signatures={
        "predict": {"batchable": True},
    }
)
bentoml.mlflow.load_model(bento_model: str | Tag | Model) mlflow.pyfunc.PyFuncModel

从本地 BentoML 模型库加载具有给定 tag 的 MLflow PyFunc 模型。

参数:

bento_model – 要从库中获取的模型的 tag,或一个 BentoML ~bentoml.Model 实例,用于从中加载模型。

返回值:

从 BentoML 模型库加载为 PyFuncModel 的 MLflow 模型。

示例

import bentoml
pyfunc_model = bentoml.mlflow.load_model('my_model:latest')
pyfunc_model.predict( input_df )
bentoml.mlflow.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

按 tag 获取模型。如果 tag 是字符串,将在 model_aliases dict 中查找。

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

从 MLflow 模型获取 BentoML 服务。

参数:
  • model_name – 要加载的模型的名称。

  • **config – 服务的附加配置。

返回值:

一个 BentoML 服务实例,可用于提供 MLflow 模型服务。

示例

import bentoml

service = bentoml.mlflow.get_service("my_mlflow_model")