mlflow.h2o
mlflow.h2o 模块提供了一个用于记录和加载 H2O 模型的 API。该模块支持以下格式的 H2O 模型:
- H20 (原生) 格式
这是可以加载回 H2O 的主要格式。
mlflow.pyfuncProduced for use by generic pyfunc-based deployment tools and batch inference.
- mlflow.h2o.get_default_conda_env()[源代码]
- 返回
调用
save_model()和log_model()生成的 MLflow 模型默认的 Conda 环境。
- mlflow.h2o.get_default_pip_requirements()[源代码]
- 返回
此格式生成的 MLflow 模型默认的 pip 依赖项列表。调用
save_model()和log_model()会生成一个至少包含这些依赖项的 pip 环境。
- mlflow.h2o.load_model(model_uri, dst_path=None)[源代码]
从本地文件(如果
run_id为None)或运行中加载 H2O 模型。此函数需要一个已使用
h2o.init初始化 H2O 实例。- 参数
model_uri –
MLflow 模型在 URI 格式中的位置。例如:
/Users/me/path/to/local/modelrelative/path/to/local/models3://my_bucket/path/to/modelruns:/<mlflow_run_id>/run-relative/path/to/modelmodels:/<model_name>/<model_version>models:/<model_name>/<stage>
For more information about supported URI schemes, see Referencing Artifacts.
dst_path – The local filesystem path to which to download the model artifact. This directory must already exist. If unspecified, a local output path will be created.
- 返回
- mlflow.h2o.log_model(h2o_model, artifact_path: str | None = None, conda_env=None, code_paths=None, registered_model_name=None, signature: mlflow.models.signature.ModelSignature = None, input_example: Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list, csr_matrix, csc_matrix, str, bytes, tuple] = None, pip_requirements=None, extra_pip_requirements=None, metadata=None, name: str | None = None, params: dict[str, typing.Any] | None = None, tags: dict[str, typing.Any] | None = None, model_type: str | None = None, step: int = 0, model_id: str | None = None, **kwargs)[源代码]
将 H2O 模型记录为当前运行的 MLflow 工件。
- 参数
h2o_model – 要保存的 H2O 模型。
artifact_path – Deprecated. Use name instead.
conda_env –
Conda 环境的字典表示形式或 conda 环境 yaml 文件的路径。如果提供,则描述该模型应运行的环境。至少应指定 get_default_conda_env() 中包含的依赖项。如果为
None,则将一个通过mlflow.models.infer_pip_requirements()推断的 pip 依赖项环境添加到模型中。如果推断失败,则回退使用 get_default_pip_requirements。来自conda_env的 pip 依赖项将写入 piprequirements.txt文件,完整的 conda 环境将写入conda.yaml。以下是 conda 环境的字典表示的示例:{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "h2o==x.y.z" ], }, ], }
code_paths –
A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded. Files declared as dependencies for a given model should have relative imports declared from a common root path if multiple files are defined with import dependencies between them to avoid import errors when loading the model.
For a detailed explanation of
code_pathsfunctionality, recommended usage patterns and limitations, see the code_paths usage guide.registered_model_name – 如果提供,则在
registered_model_name下创建一个模型版本,如果给定名称的注册模型不存在,也会创建该注册模型。signature –
ModelSignature类的实例,用于描述模型的输入和输出。如果未指定但提供了input_example,则将根据提供的输入示例和模型自动推断签名。要在提供输入示例时禁用自动签名推断,请将signature设置为False。要手动推断模型签名,请在具有有效模型输入的(例如,省略目标列的训练数据集)和有效模型输出(例如,在训练数据集上进行的模型预测)的数据集上调用infer_signature(),例如:from mlflow.models import infer_signature train = df.drop_column("target_label") predictions = ... # compute model predictions signature = infer_signature(train, predictions)
input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。pip_requirements – pip 依赖项字符串的可迭代对象(例如
["h2o", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip 依赖项文件的字符串路径(例如"requirements.txt")。如果提供,则描述该模型应运行的环境。如果为None,则通过mlflow.models.infer_pip_requirements()根据用户当前软件环境推断默认依赖项列表。如果推断失败,则回退使用 get_default_pip_requirements。依赖项和约束都将自动解析并分别写入requirements.txt和constraints.txt文件,并作为模型的一部分存储。依赖项也会写入模型 conda 环境(conda.yaml)文件的pip部分。extra_pip_requirements –
pip 依赖项字符串的可迭代对象(例如
["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip 依赖项文件的字符串路径(例如"requirements.txt")。如果提供,则描述将附加到根据用户当前软件环境自动生成的默认 pip 依赖项集中的其他 pip 依赖项。依赖项和约束都将自动解析并分别写入requirements.txt和constraints.txt文件,并作为模型的一部分存储。依赖项也会写入模型 conda 环境(conda.yaml)文件的pip部分。警告
以下参数不能同时指定
conda_envpip_requirementsextra_pip_requirements
此示例演示了如何使用
pip_requirements和extra_pip_requirements指定 pip requirements。metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。
name – 模型名称。
params – 要与模型一起记录的参数字典。
tags – 要与模型一起记录的标签字典。
model_type – 模型的类型。
step – 记录模型输出和指标的步骤
model_id – 模型的 ID。
kwargs – 传递给
h2o.save_model方法的关键字参数。
- 返回
一个
ModelInfo实例,其中包含已记录模型的元数据。
- mlflow.h2o.save_model(h2o_model, path, conda_env=None, code_paths=None, mlflow_model=None, settings=None, signature: mlflow.models.signature.ModelSignature = None, input_example: Union[pandas.core.frame.DataFrame, numpy.ndarray, dict, list, csr_matrix, csc_matrix, str, bytes, tuple] = None, pip_requirements=None, extra_pip_requirements=None, metadata=None)[源代码]
将 H2O 模型保存到本地文件系统上的路径。
- 参数
h2o_model – 要保存的 H2O 模型。
path – 要保存模型的本地路径。
conda_env –
Conda 环境的字典表示形式或 conda 环境 yaml 文件的路径。如果提供,则描述该模型应运行的环境。至少应指定 get_default_conda_env() 中包含的依赖项。如果为
None,则将一个通过mlflow.models.infer_pip_requirements()推断的 pip 依赖项环境添加到模型中。如果推断失败,则回退使用 get_default_pip_requirements。来自conda_env的 pip 依赖项将写入 piprequirements.txt文件,完整的 conda 环境将写入conda.yaml。以下是 conda 环境的字典表示的示例:{ "name": "mlflow-env", "channels": ["conda-forge"], "dependencies": [ "python=3.8.15", { "pip": [ "h2o==x.y.z" ], }, ], }
code_paths –
A list of local filesystem paths to Python file dependencies (or directories containing file dependencies). These files are prepended to the system path when the model is loaded. Files declared as dependencies for a given model should have relative imports declared from a common root path if multiple files are defined with import dependencies between them to avoid import errors when loading the model.
For a detailed explanation of
code_pathsfunctionality, recommended usage patterns and limitations, see the code_paths usage guide.mlflow_model – 要添加此 flavor 的
mlflow.models.Model。settings – 在加载模型时传递给
h2o.init()的设置。signature –
ModelSignature类的实例,用于描述模型的输入和输出。如果未指定但提供了input_example,则将根据提供的输入示例和模型自动推断签名。要在提供输入示例时禁用自动签名推断,请将signature设置为False。要手动推断模型签名,请在具有有效模型输入的(例如,省略目标列的训练数据集)和有效模型输出(例如,在训练数据集上进行的模型预测)的数据集上调用infer_signature(),例如:from mlflow.models import infer_signature train = df.drop_column("target_label") predictions = ... # compute model predictions signature = infer_signature(train, predictions)
input_example – 一个或多个有效的模型输入实例。输入示例用作要馈送给模型的数据的提示。它将被转换为 Pandas DataFrame,然后使用 Pandas 的面向拆分(split-oriented)格式序列化为 json,或者转换为 numpy 数组,其中示例将通过转换为列表来序列化为 json。字节将进行 base64 编码。当
signature参数为None时,输入示例用于推断模型签名。pip_requirements – pip 依赖项字符串的可迭代对象(例如
["h2o", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip 依赖项文件的字符串路径(例如"requirements.txt")。如果提供,则描述该模型应运行的环境。如果为None,则通过mlflow.models.infer_pip_requirements()根据用户当前软件环境推断默认依赖项列表。如果推断失败,则回退使用 get_default_pip_requirements。依赖项和约束都将自动解析并分别写入requirements.txt和constraints.txt文件,并作为模型的一部分存储。依赖项也会写入模型 conda 环境(conda.yaml)文件的pip部分。extra_pip_requirements –
pip 依赖项字符串的可迭代对象(例如
["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上的 pip 依赖项文件的字符串路径(例如"requirements.txt")。如果提供,则描述将附加到根据用户当前软件环境自动生成的默认 pip 依赖项集中的其他 pip 依赖项。依赖项和约束都将自动解析并分别写入requirements.txt和constraints.txt文件,并作为模型的一部分存储。依赖项也会写入模型 conda 环境(conda.yaml)文件的pip部分。警告
以下参数不能同时指定
conda_envpip_requirementsextra_pip_requirements
此示例演示了如何使用
pip_requirements和extra_pip_requirements指定 pip requirements。metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。