mlflow.spacy
mlflow.spacy 模块提供了一个用于记录和加载 spaCy 模型的 API。此模块以以下形式导出 spacy 模型
- spaCy(原生)格式
这是可以加载回 spaCy 的主要格式。
mlflow.pyfunc此格式专为通用的基于 pyfunc 的部署工具和批处理推理而生成,仅当 spaCy 的模型管道中至少包含一个 TextCategorizer 时才会创建。
- mlflow.spacy.get_default_conda_env()[源代码]
- 返回
对
save_model()和log_model()的 MLflow 模型默认的 Conda 环境。
- mlflow.spacy.get_default_pip_requirements()[源代码]
- 返回
此格式生成的 MLflow 模型的默认 pip 需求列表。对
save_model()和log_model()的调用会生成一个至少包含这些需求的 pip 环境。
- mlflow.spacy.load_model(model_uri, dst_path=None)[源代码]
从本地文件(如果
run_id为None)或一个运行中加载 spaCy 模型。- 参数
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.
- 返回
一个加载的 spaCy 模型
- mlflow.spacy.log_model(spacy_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)[源代码]
将 spaCy 模型记录为当前运行的 MLflow artifact。
- 参数
spacy_model – 要保存的 spaCy 模型。
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": [ "spacy==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描述了模型输入和输出Schema。模型签名可以从具有有效模型输入的数据集(例如,省略目标列的训练数据集)和有效模型输出的数据集(例如,在训练数据集上生成的模型预测)中推断,例如: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 需求字符串(例如
["spacy", "-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 – 传递给
spacy.save_model方法的 kwargs。
- 返回
一个
ModelInfo实例,其中包含已记录模型的元数据。
- mlflow.spacy.save_model(spacy_model, path, conda_env=None, code_paths=None, mlflow_model=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)[源代码]
将 spaCy 模型保存到本地文件系统上的路径。
- 参数
spacy_model – 要保存的 spaCy 模型。
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": [ "spacy==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。signature –
ModelSignature描述了模型输入和输出Schema。模型签名可以从具有有效模型输入的数据集(例如,省略目标列的训练数据集)和有效模型输出的数据集(例如,在训练数据集上生成的模型预测)中推断,例如: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 需求字符串(例如
["spacy", "-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 文件中的自定义元数据字典。