mlflow.spacy

The mlflow.spacy 模块提供了一个用于记录和加载 spaCy 模型的 API。该模块以以下形式导出 spacy 模型:

spaCy (原生) 格式

这是可以加载回 spaCy 的主要格式。

mlflow.pyfunc

此格式是为通用的基于 pyfunc 的部署工具和批处理推理而创建的,仅当 spaCy 的模型管道至少包含一个 TextCategorizer 时才会创建。

mlflow.spacy.get_default_conda_env()[source]
返回

通过调用 save_model()log_model() 生成的 MLflow Models 的默认 Conda 环境。

mlflow.spacy.get_default_pip_requirements()[source]
返回

此格式生成的 MLflow Models 的默认 pip requirements 列表。调用 save_model()log_model() 会生成一个 pip 环境,该环境至少包含这些 requirements。

mlflow.spacy.load_model(model_uri, dst_path=None)[source]

从本地文件(如果 run_idNone)或 run 中加载 spaCy 模型。

参数
  • model_uri

    MLflow 模型在 URI 格式中的位置。例如:

    • /Users/me/path/to/local/model

    • relative/path/to/local/model

    • s3://my_bucket/path/to/model

    • runs:/<mlflow_run_id>/run-relative/path/to/model

    • models:/<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)[source]

将 spaCy 模型作为 MLflow artifact 记录到当前 run 中。

参数
  • 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 requirements 的 conda 环境添加到模型中。如果推断失败,则回退使用 get_default_pip_requirements。来自 conda_env 的 pip requirements 被写入 pip requirements.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_paths functionality, 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 requirements 字符串的可迭代对象(例如 ["spacy", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了模型应该运行的环境。如果为 None,则由 mlflow.models.infer_pip_requirements() 根据用户当前软件环境推断的默认 requirements 列表。如果推断失败,则回退使用 get_default_pip_requirements。requirements 和 constraints 都会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。requirements 也会被写入模型 conda 环境(conda.yaml)文件的 pip 部分。

  • extra_pip_requirements

    pip requirements 字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了将附加到根据用户当前软件环境自动生成的默认 pip requirements 集中的其他 pip requirements。requirements 和 constraints 都会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。requirements 也会被写入模型 conda 环境(conda.yaml)文件的 pip 部分。

    警告

    以下参数不能同时指定

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    此示例演示了如何使用 pip_requirementsextra_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)[source]

将 spaCy 模型保存到本地文件系统上的一个路径。

参数
  • spacy_model – 要保存的 spaCy 模型。

  • path – 要保存模型的本地路径。

  • conda_env

    Conda 环境的字典表示或 conda 环境 yaml 文件的路径。如果提供,这描述了模型应该运行的环境。至少,它应该指定 get_default_conda_env() 中包含的依赖项。如果为 None,则将一个由 mlflow.models.infer_pip_requirements() 推断的 pip requirements 的 conda 环境添加到模型中。如果推断失败,则回退使用 get_default_pip_requirements。来自 conda_env 的 pip requirements 被写入 pip requirements.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_paths functionality, 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 requirements 字符串的可迭代对象(例如 ["spacy", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了模型应该运行的环境。如果为 None,则由 mlflow.models.infer_pip_requirements() 根据用户当前软件环境推断的默认 requirements 列表。如果推断失败,则回退使用 get_default_pip_requirements。requirements 和 constraints 都会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。requirements 也会被写入模型 conda 环境(conda.yaml)文件的 pip 部分。

  • extra_pip_requirements

    pip requirements 字符串的可迭代对象(例如 ["pandas", "-r requirements.txt", "-c constraints.txt"])或本地文件系统上 pip requirements 文件的字符串路径(例如 "requirements.txt")。如果提供,这描述了将附加到根据用户当前软件环境自动生成的默认 pip requirements 集中的其他 pip requirements。requirements 和 constraints 都会被自动解析并分别写入 requirements.txtconstraints.txt 文件,并作为模型的一部分存储。requirements 也会被写入模型 conda 环境(conda.yaml)文件的 pip 部分。

    警告

    以下参数不能同时指定

    • conda_env

    • pip_requirements

    • extra_pip_requirements

    此示例演示了如何使用 pip_requirementsextra_pip_requirements 指定 pip requirements。

  • metadata – 传递给模型并存储在 MLmodel 文件中的自定义元数据字典。