在 MLflow 模型注册表中管理 MLflow 模型的生命周期 Manage the Lifecycle of MLflow Models in MLflow Model Registry

重要

此功能目前以公共预览版提供。This feature is in Public Preview.

MLflow 模型注册表是一个集中式模型存储,还是一个 UI 和 API 集,可用于管理 MLflow 模型的完整生命周期。MLflow Model Registry is a centralized model store and a UI and set of APIs that enable you to manage the full lifecycle of MLflow Models. 模型注册表提供按时间顺序记录的模型世系(MLflow 试验和运行在给定时间生成模型)、模型版本控制、阶段转换(例如从“暂存”到“生产”或“已存档”)以及模型、模型版本注释和说明。Model Registry provides chronological model lineage (which MLflow experiment and run produced the model at a given time), model versioning, stage transitions (for example, from staging to production or archived), and model and model version annotations and descriptions.

有两个可与模型注册表进行交互的工作流:模型注册表 UI 和模型注册表 API。There are two workflows to interact with the Model Registry: the Model Registry UI and the Model Registry API.

模型注册表概念Model Registry concepts

  • 模型:从使用模型风格的 mlflow.<model-flavor>.log_model 方法之一记录的试验或运行记录的 MLflow 模型。Model: An MLflow Model logged from an experiment or run that is logged with one of the model flavor’s mlflow.<model-flavor>.log_model methods. 记录后,你可以将模型注册到模型注册表。Once logged, you can register the model with the Model Registry.
  • 已注册的模型:已注册到模型注册表的 MLflow 模型。Registered model: An MLflow Model that has been registered with the Model Registry. 已注册的模型具有唯一的名称、版本、模型世系和其他元数据。The registered model has a unique name, versions, model lineage, and other metadata.
  • 模型版本:已注册的模型的版本。Model version: A version of a registered model. 向模型注册表添加新模型时,它将添加为“版本 1”。When a new model is added to the Model Registry, it is added as Version 1. 注册到同一模型名称的每个模型的版本号将递增。Each model registered to the same model name increments the version number.
  • 模型阶段:可以为一个模型版本分配一个或多个阶段。Model stage: A model version can be assigned one or more stages. MLflow 为常见用例提供了预定义的阶段:无、暂存、生产和已存档。 MLflow provides predefined stages for the common use-cases None, Staging, Production, and Archived. 使用适当的权限,你可以在不同的阶段之间转换模型版本,也可以请求模型阶段转换。With the appropriate permission you can transition a model version between stages or you can request a model stage transition.
  • 说明:你可以为模型的意图添加注释,包括说明和对团队有用的任何相关信息,例如算法说明、所采用的数据集,或者方法。Description: You can annotate a model’s intent, including description and any relevant information useful for the team such as algorithm description, dataset employed, or methodology.
  • 活动:记录每个已注册的模型的活动(例如,阶段转换请求)。Activities: Each registered model’s activities—such as request for stage transition—is recorded. 活动跟踪提供了模型的发展(从试验到暂存版本再到生产)的世系和可审核性。The trace of activities provides lineage and auditability of the model’s evolution, from experimentation to staged versions to production.

控制对模型注册表中模型的访问Control access to models in Model Registry

若要了解如何控制对模型注册表中模型的访问,请参阅 MLflow 模型权限To learn how to control access to models registered in Model Registry, see MLflow Model permissions.

要求Requirements

模型注册表 UI 工作流Model Registry UI workflow

本部分介绍了如何注册模型、编辑模型版本说明、转换模型版本、查看模型版本活动、显示已注册的模型,以及如何使用模型注册表 UI 删除模型版本或模型。This section describes how to register a model, edit a model version description, transition a model version, view model version activities, display registered models, and delete a model version or model using the Model Registry UI.

在模型注册表中注册模型Register a model in the Model Registry

可以通过两种方式在模型注册表中注册模型。There are two ways to register a model in the Model Registry. 可以注册已记录到 MLflow 的现有模型,也可以创建并注册一个新的空模型,然后将某个已记录的模型分配给它。You can register an existing model that has been logged to MLflow, or you can create and register a new, empty model and then assign a previously logged model to it.

从笔记本中注册现有的已记录模型Register an existing logged model from a notebook

  1. 在工作区中,标识包含要注册的模型的 MLflow 运行。In the Workspace, identify the MLflow run containing the model you want to register.

    1. 单击笔记本工具栏中的“试验”图标试验Click the Experiment icon Experiment in the notebook tool bar.

      笔记本工具栏Notebook toolbar

    2. 在“试验运行”边栏中单击In the Experiment Runs sidebar, click the 外部链接 图标,该图标位于运行日期旁。icon next to the date of the run.

      运行日期Run Date

      MLflow 运行页显示所有运行详细信息,包括参数、指标、标记和项目列表。The MLflow Run page displays all the run details, including parameters, metrics, tags, and list of artifacts.

  2. 在“项目”部分,单击名为“xxx-model”的目录。In the Artifacts section, click the directory named xxx-model.

    注册模型Register model

  3. 单击最右侧的“注册模型”按钮。Click the Register Model button at the far right.

  4. 在“注册模型”对话框中,从“模型”下拉菜单中选择“新建模型”,然后在“模型名称”字段中输入模型名称,例如 scikit-learn-power-forecastingIn the Register Model dialog, select Create New Model from the Model drop-down menu, and in the Model Name field, enter a model name, for example scikit-learn-power-forecasting.

    创建模型Create model

  5. 单击“注册”。Click Register. 这将注册一个名为 scikit-learn-power-forecasting 的模型,将该模型复制到由 MLflow 模型注册表管理的安全位置,并创建该模型的新版本。This registers a model named scikit-learn-power-forecasting, copies the model into a secure location managed by the MLflow Model Registry, and creates a new version of the model.

    几分钟后,MLflow 运行 UI 会将“注册模型”按钮替换为指向新注册的模型版本的链接。After a few moments, the MLflow Run UI replaces the Register Model button with a link to the new registered model version.

    创建模型Create model

  6. 单击此链接可在模型注册表 UI 中打开新的模型版本。Click the link to open the new model version in the Model Registry UI. 通过单击左侧导航栏中的“模型”图标You can also find the model in the Model Registry by clicking the 也能 找到该模型。in the left navigation bar.

创建新注册的模型并向其分配已记录的模型Create a new registered model and assign a logged model to it

可以使用已注册的模型页上的“创建模型”按钮创建一个新的空模型,然后为其分配一个已记录的模型。You can use the Create Model button on the registered models page to create a new, empty model and then assign a logged model to it. 请执行下列步骤:Follow these steps:

  1. 在“已注册的模型”页上,单击“创建模型”。On the registered models page, click Create Model. 为该模型输入一个名称,然后单击“创建”。Enter a name for the model and click Create.

  2. 执行在模型注册表中注册模型中的步骤 1 到 3。Follow Steps 1 through 3 in Register a model in the Model Registry.

  3. 在“注册模型”对话框中,选择在步骤 1 中创建的模型的名称,然后单击“注册”。In the Register Model dialog, select the name of the model you created in Step 1 and click Register. 该操作的结果是采用之前创建的名称注册一个模型,将该模型复制到由 MLflow 模型注册表管理的安全位置,并创建模型版本:Version 1This registers a model with the name you created, copies the model into a secure location managed by the MLflow Model Registry, and creates a model version: Version 1.

    几分钟后,MLflow 运行 UI 会将“注册模型”按钮替换为指向新注册的模型版本的链接。After a few moments, the MLflow Run UI replaces the Register Model button with a link to the new registered model version. 现可从“试验运行”页上的“注册模型”对话框中的“模型”下拉列表中选择模型 。You can now select the model from the Model drop-down list in the Register Model dialog on the Experiment Runs page. 还可以通过在 API 命令(如 Create ModelVersion)中指定模型名称来注册模型的新版本。You can also register new versions of the model by specifying its name in API commands like Create ModelVersion.

模型版本页面Model version page

模型版本页显示有关已注册模型的版本的信息:已注册的模型的创建时间、创建者、上次修改时间、在其生命周期中的阶段、源运行 ID、说明、挂起的暂存转换请求,以及与模型版本相关的所有活动的时间顺序。The model version page displays information about a version of a registered model: creation time for the registered model, creator, last modified time, stage in its lifecycle, source run ID, description, pending requests for stage transitions, and a chronological order of all the activities pertaining to the model version.

模型版本Model version

若要从侧栏导航到模型版本页,请执行以下操作:To navigate to a model version page from the sidebar:

  1. 单击Click the 侧栏中的 “模型”图标。icon in the sidebar.
  2. 单击模型名称。Click a model name.
  3. 单击模型版本。Click a model version.

更新模型版本说明Update a model version description

可以通过对模型版本进行批注来提供有关模型版本的有用信息。You can provide useful information about a model version by annotating it. 例如,你可能需要在模型批注中提供问题概述、所用方法和算法,或数据集的相关信息和链接。For example, you may want to annotate a model with an overview of the problem, the methodology and algorithm used, or information about and links to datasets.

若要更新说明,请执行以下操作:To update a description:

  1. 单击“说明”“编辑”图标 图标。Click the Description Edit Icon icon.

  2. 在编辑窗口中编辑有关模型版本的详细信息。Edit the details about the model version in the edit window.

    编辑模型版本说明Edit model version description

  3. 单击“保存”按钮。Click the Save button. 说明将显示在“说明”列中。The description appears in the Description column. 该列最多可显示 32 个字符或一行文本,以较短者为准。The column displays a maximum of 32 characters or one line of text, whichever is shorter.

转换模型版本阶段Transition a model version stage

模型版本具有以下阶段之一:“无”、“暂存”、“生产”和“已存档” 。A model version has one of the following stages: None, Staging, Production, and Archived. “暂存”是指模型处于测试和验证阶段,“生产”是指模型版本已完成测试或审核流程,并已部署到应用程序进行实时评分。Staging is meant for model testing and validating, while Production is for model versions that have completed the testing or review processes and have been deployed to applications for live scoring. “已存档”阶段的模型版本视为非活动状态,可以考虑将其删除An Archived model version is assumed to be inactive, at which point you can consider deleting it.

不同的模型版本可以处于不同的阶段。Different versions of a model can be in different stages.

具有适当权限的用户可以在不同阶段之间转换模型版本。A user with appropriate permission can transition a model version between stages. 如果你有权将模型版本转换到某个特定阶段,可以直接进行转换。If you have permission to transition a model version to a particular stage, you can make the transition directly. 如果你没有相应权限,可以请求阶段转换,有权转换模型版本的用户可以批准、拒绝或取消该请求If you do not have permission, you can request a stage transition and a user that has permission to transition model versions can approve, reject, or cancel the request.

  1. 若要显示可用模型阶段和可用选项的列表,请在“模型版本”页中单击“阶段: ”按钮,然后请求或选择转换到另一阶段。To display the list of available model stages and your available options, in a model version page, click the Stage: button and request or select a transition to another stage.

    阶段转换选项Stage transition options

  2. 输入注释,然后单击“确定”。Enter a comment, and click OK.

    转换为“暂存”Transition to staging

将模型版本转换为“生产”阶段Transition a model version to the Production stage

完成测试和验证流程后,可以转换或者请求转换至“生产”阶段。After testing and validation, you can transition or request a transition to the Production stage.

模型注册表允许每个阶段中存在已注册模型的多个版本;若希望“生产”阶段中只有一个版本,可选中对应的复选框,以便将当前处于“生产”阶段的所有模型版本都转换至“已存档”阶段。Model Registry allows more than one version of the registered model in each stage; if you want to have only one version in Production, you can transition all versions of the model currently in Production to Archived by checking the checkbox.

转换至“生产”Transition to production

批准、拒绝或取消模型版本阶段转换请求 Approve, reject, or cancel a model version stage transition request

无权转换阶段的用户可以发起阶段转换请求。A user without stage transition permission can request a stage transition. 请求显示在模型版本页的“待定的请求”部分中:The request appears in the Pending Requests section in the model version page:

转换至“生产”Transition to production

若要批准、拒绝或取消阶段转换请求,请单击“批准”、“拒绝”或“取消”链接 。To approve, reject, or cancel a stage transition request, click the Approve, Reject, or Cancel link.

转换请求的创建者也可以取消请求。The creator of a transition request can also cancel the request.

查看模型版本活动View model version activities

若要查看请求、批准、待定和应用于模型版本的所有转换,请转到“活动”部分。To view all the transitions requested, approved, pending, and applied to a model version, go to the Activities section. 此处的活动记录包含模型生命周期的世系,可供审核或检查。This record of activities provides a lineage of the model’s lifecycle for auditing or inspection.

已注册的模型的活动Registered model activities

显示已注册的模型Display registered models

MLflow 模型注册表是所有已注册模型的中心。MLflow Model Registry is the hub for all registered models. 若要显示所有已注册的模型,请单击To display all registered models, click the 侧栏中的 “模型”图标。icon in the sidebar.

若要搜索特定模型,请在搜索框中键入模型名称。To search for a specific model, type in the model name in the search box.

搜索已注册模型Registered models search

已注册模型页Registered model page

已注册模型页显示有关已注册模型的信息:创建时间和上次修改时间、可编辑的说明、带有相应注册时间的版本列表、阶段和挂起的转换请求。The registered model page displays information about a registered model: creation and last modified times, an editable description, and a list of versions with the respective registration times, stages, and pending transition requests. 若要导航到已注册模型页,请执行以下操作:To navigate to a registered model page:

  1. 单击Click the 侧栏中的 “模型”图标。icon in the sidebar.

    已注册的模型Registered models

  2. 单击模型名称。Click a model name.

    已注册的模型Registered model

删除模型版本或已注册模型Delete a model version or a registered model

警告

不能撤消此操作。You cannot undo this action. 可以将模型版本转换到“已存档”阶段,而无需在注册表中删除它。You can transition a model version to the Archived stage rather than deleting it from the registry. 删除某一模型时,将删除模型注册表存储的所有模型项目以及与该已注册模型关联的所有元数据。When you delete a model, all model artifacts stored by the Model Registry and all the metadata associated with the registered model are deleted.

备注

只能删除处于“无”或“已存档”阶段的模型和模型版本。You can only delete models and model versions in the None or Archived stage. 如果某个已注册模型具有处于“暂存”或“生产”阶段的版本,需要将这些版本转换为“无”或“已存档”阶段,然后才能删除该模型。If a registered model has versions in the Staging or Production stage, you must transition them to either the None or Archived stage before deleting the model.

删除模型版本:To delete a model version:

  1. 单击Click the 侧栏中的 “模型”图标。icon in the sidebar.

  2. 单击模型名称。Click a model name.

  3. 单击模型版本。Click a model version.

  4. 选择Select the 下拉按钮 (位于版本号旁)。next to the version number.

    删除模型版本Delete model version

  5. 单击“删除”按钮。Click the Delete button.

删除模型:To delete a model:

  1. 单击Click the 侧栏中的 “模型”图标。icon in the sidebar.
  2. 单击模型名称。Click a model name.
  3. 选择Select the 下拉按钮 (位于模型旁)。next to the model.
  4. 单击“删除”按钮。Click the Delete button.

模型注册表 API 工作流Model Registry API workflow

本部分介绍了如何注册模型、编辑模型版本说明、转换模型版本、查看模型版本活动、显示已注册的模型,以及如何使用模型注册表 API 删除模型版本或模型。This section describes how to register a model, edit a model version description, transition a model version, view model version activities, display registered models, and delete a model version or model using the Model Registry API.

要求Requirements

  • Databricks Runtime 6.5 或更高版本Databricks Runtime 6.5 or above
  • Databricks Runtime 6.4 及更低版本,mlflow>=1.7.0Databricks Runtime 6.4 and lower, mlflow>=1.7.0

在模型注册表中注册模型Register a model in the Model Registry

可以通过三种编程式方法在模型注册表中注册模型。There are three programmatic ways to register a model in the Model Registry. 所有方法都将模型复制到由 MLflow 模型注册表管理的安全位置。All methods copy the model into a secure location managed by the MLflow Model Registry.

  • 若要记录模型并在 MLflow 试验期间将其注册为指定名称,请使用 mlflow.<model-flavor>.log_model(...) 方法。To log a model and register it with the specified name during an MLflow experiment, use the mlflow.<model-flavor>.log_model(...) method. 如果还没有模型注册为该名称,该方法将注册一个新模型,创建版本 1,并返回 ModelVersion MLflow 对象。If a registered model with the name doesn’t exist, the method registers a new model, creates Version 1, and returns a ModelVersion MLflow object. 如果已有模型注册为该名称,该方法将创建一个新的模型版本并返回版本对象。If a registered model with the name exists already, the method creates a new model version and returns the version object.

    with mlflow.start_run(run_name=<run-name>) as run:
      ...
      mlflow.<model-flavor>.log_model(<model-flavor>=<model>,
        artifact_path="<model-path>",
        registered_model_name="<model-name>"
      )
    
  • 若要在所有试验运行完成后使用指定名称注册模型,并且已确定最适合添加到注册表的模型,请使用 mlflow.register_model() 方法。To register a model with the specified name after all your experiment runs complete and you have decided which model is most suitable to add to the registry, use the mlflow.register_model() method. 采用此方法时,需要 mlruns:URI 参数的运行 ID。For this method, you need the run ID for the mlruns:URI argument. 如果还没有模型注册为该名称,该方法将注册一个新模型,创建版本 1,并返回 ModelVersion MLflow 对象。If a registered model with the name doesn’t exist, the method registers a new model, creates Version 1, and returns a ModelVersion MLflow object. 如果已有模型注册为该名称,该方法将创建一个新的模型版本并返回版本对象。If a registered model with the name exists already, the method creates a new model version and returns the version object.

    result=mlflow.register_model("runs:<model-path>", "<model-name>")
    
  • 若要创建具有指定名称的新的注册模型,请使用 MLflow 客户端 API create_registered_model() 方法。To create a new registered model with the specified name, use the MLflow Client API create_registered_model() method. 如果模型名称存在,此方法将引发 MLflowExceptionIf the model name exists, this method throws an MLflowException.

    client = MlflowClient()
    result = client.create_registered_model("<model-name>")
    

更新模型版本说明Update model version description

若要更新模型版本说明,请使用 MLflow 客户端 API update_model_version() 方法:To update a model version description, use the MLflow Client API update_model_version() method:

client = MlflowClient()
client.update_model_version(
  name="<model-name>",
  version=<model-version>,
  description="<description>"
)

重命名模型Rename a model

若要重命名已注册模型,请使用 MLflow 客户端 API rename_registered_model() 方法:To rename a registered model, use the MLflow Client API rename_registered_model() method:

client=MlflowClient()
client.rename_registered_model("<model-name>", "<new-model-name>")

备注

仅当已注册模型没有任何版本或所有版本都处于“无”或“已存档”阶段时,才能重命名该模型。You can rename a registered model only if it has no versions, or all versions are in the None or Archived stage.

转换模型版本阶段Transition a model version stage

具有适当权限的用户可以将模型版本转换至新的阶段。Users with appropriate permissions can transition a model version to a new stage.

若要将模型版本阶段更新至新阶段,请使用 MLflow 客户端 API transition_model_version_stage() 方法:To update a model version stage to a new stage, use the MLflow Client API transition_model_version_stage() method:

  client = MlflowClient()
  client.transition_model_version_stage(
    name="<model-name>",
    version=<model-version>,
    stage="<stage>",
    description="<description>"
  )

<stage> 可以为以下值:"Staging"|"staging""Archived"|"archived""Production"|"production""None"|"none"The accepted values for <stage> are: "Staging"|"staging", "Archived"|"archived", "Production"|"production", "None"|"none".

列出和搜索已注册的模型List and search registered models

若要检索所有已注册模型的列表,请使用 MLflow 客户端 API list_model_versions() 方法:To retrieve a list of all registered models, use the MLflow Client API list_model_versions() method:

from pprint import pprint

client = MlflowClient()
for rm in client.list_registered_models():
  pprint(dict(rm), indent=4)

输出:This outputs:

{   'creation_timestamp': 1582671933216,
    'description': None,
    'last_updated_timestamp': 1582671960712,
    'latest_versions': [<ModelVersion: creation_timestamp=1582671933246, current_stage='Production', description='A random forest model containing 100 decision trees trained in scikit-learn', last_updated_timestamp=1582671960712, name='sk-learn-random-forest-reg-model', run_id='ae2cc01346de45f79a44a320aab1797b', source='./mlruns/0/ae2cc01346de45f79a44a320aab1797b/artifacts/sklearn-model', status='READY', status_message=None, user_id=None, version=1>,
    <ModelVersion: creation_timestamp=1582671960628, current_stage='None', description=None, last_updated_timestamp=1582671960628, name='sk-learn-random-forest-reg-model', run_id='d994f18d09c64c148e62a785052e6723', source='./mlruns/0/d994f18d09c64c148e62a785052e6723/artifacts/sklearn-model', status='READY', status_message=None, user_id=None, version=2>],
    'name': 'sk-learn-random-forest-reg-model'}

还可以使用 MLflow 客户端 API search_model_versions() 方法搜索特定的模型名称并列出其版本详细信息:You can also search for a specific model name and list its version details using MLflow Client API search_model_versions() method:

from pprint import pprint

client=MlflowClient()
[pprint(mv) for mv in client.search_model_versions("name=<model-name>")]

输出:This outputs:

{   'creation_timestamp': 1582671933246,
    'current_stage': 'Production',
    'description': 'A random forest model containing 100 decision trees '
                   'trained in scikit-learn',
    'last_updated_timestamp': 1582671960712,
    'name': 'sk-learn-random-forest-reg-model',
    'run_id': 'ae2cc01346de45f79a44a320aab1797b',
    'source': './mlruns/0/ae2cc01346de45f79a44a320aab1797b/artifacts/sklearn-model',
    'status': 'READY',
    'status_message': None,
    'user_id': None,
    'version': 1 }

{   'creation_timestamp': 1582671960628,
    'current_stage': 'None',
    'description': None,
    'last_updated_timestamp': 1582671960628,
    'name': 'sk-learn-random-forest-reg-model',
    'run_id': 'd994f18d09c64c148e62a785052e6723',
    'source': './mlruns/0/d994f18d09c64c148e62a785052e6723/artifacts/sklearn-model',
    'status': 'READY',
    'status_message': None,
    'user_id': None,
    'version': 2 }

删除模型版本或已注册模型Delete a model version or registered model

警告

不能撤消此操作。You cannot undo this action. 可以将模型版本转换到“已存档”阶段,而不是在注册表中删除它。You can transition a model version to an Archive stage rather than deleting it from the registry. 删除某一模型时,将删除模型注册表存储的所有模型项目以及与该已注册模型关联的所有元数据。When you delete a model, all model artifacts stored by the Model Registry and all the metadata associated with the registered model are deleted.

备注

只能删除处于“无”或“已存档”阶段的模型和模型版本。You can only delete models and model versions in the None or Archived stage. 如果某个已注册模型具有处于“暂存”或“生产”阶段的版本,需要将这些版本转换为“无”或“已存档”阶段,然后才能删除该模型。If a registered model has versions in the Staging or Production stage, you must transition them to either the None or Archived stage before deleting the model.

删除模型版本Delete a model version

若要删除模型版本,请使用 MLflow 客户端 API delete_model_version() 方法:To delete a model version, use the MLflow Client API delete_model_version() method:

# Delete versions 1,2, and 3 of the model
client = MlflowClient()
versions=[1, 2, 3]
for version in versions:
  client.delete_model_version(name="<model-name>", version=version)

删除模型Delete a model

若要删除模型,请使用 MLflow 客户端 API delete_registered_model() 方法:To delete a model, use the MLflow Client API delete_registered_model() method:

client = MlflowClient()
client.delete_registered_model(name="<model-name>")

使用模型注册表跨工作区共享模型Use the Model Registry to share models across workspaces

Azure Databricks 支持在多个工作区之间共享模型。Azure Databricks supports sharing models across multiple workspaces. 例如,你可以在自己的工作区中开发和记录模型,然后将其注册到集中式模型注册表。For example, you can develop and log a model in your own workspace and then register it in a centralized model registry. 此特性非常适合多个团队共享模型访问权限的情况。This is useful when multiple teams share access to models. 可以创建多个工作区,并在这些环境中使用和管理模型。You can create multiple workspaces and use and manage models across these enviroments.

示例Example

若要通过示例来了解如何使用模型注册表来构建机器学习应用程序,以预测风力发电场的每日电力输出,请参阅:For an example that illustrates how to use the Model Registry to build a machine learning application that forecasts the daily power output of a wind farm, see: