你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用“更新资源”活动更新机器学习工作室(经典版)模型

适用于:Azure 数据工厂 Azure Synapse Analytics

提示

试用 Microsoft Fabric 中的数据工厂,这是一种适用于企业的一站式分析解决方案。 Microsoft Fabric 涵盖从数据移动到数据科学、实时分析、商业智能和报告的所有内容。 了解如何免费开始新的试用

重要

对 Azure 机器学习工作室(经典)的支持将于 2024 年 8 月 31 日结束。 建议在该日期之前转换到 Azure 机器学习

自 2021 年 12 月 1 日起,无法创建新的机器学习工作室(经典)资源(工作区和 Web 服务计划)。 在 2024 年 8 月 31 日之前,可继续使用现有的机器学习工作室(经典)试验和 Web 服务。 有关详细信息,请参阅:

机器学习工作室(经典版)文档即将停用,未来可能不再更新。

注意

由于在 2021 年 12 月 1 日之后无法再创建机器学习工作室(经典)资源,因此鼓励用户将 Azure 机器学习机器学习执行管道活动一起使用,而不是使用更新资源活动来更新机器学习工作室(经典)模型.

本文是对主要的机器学习工作室(经典版)集成文章“使用机器学习工作室(经典版)创建预测管道”的补充。 如果尚未执行此操作,请在阅读本文之前查阅此主要文章。

概述

在操作化机器学习工作室(经典版)模型的过程中会训练并保存模型。 然后使用它来创建预测 Web 服务。 然后可以在网站、仪表板和移动应用中使用 Web 服务。

使用机器学习工作室(经典版)创建的模型通常不是静态的。 当新数据变得可用时,或当 API 使用者拥有其自己的数据时,需要重新训练模型。

重新训练可能会经常发生。 通过批处理执行活动和更新资源活动,可以使机器学习工作室(经典版)模型可操作化,重新训练和更新预测性 Web 服务。

下图描述了训练与预测性 Web 服务之间的关系。

Web 服务

机器学习工作室(经典版)Update Resource 活动

以下 JSON 代码片段定义了一个机器学习工作室(经典版)批处理执行活动。

{
    "name": "amlUpdateResource",
    "type": "AzureMLUpdateResource",
    "description": "description",
    "linkedServiceName": {
        "type": "LinkedServiceReference",
        "referenceName": "updatableScoringEndpoint2"
    },
    "typeProperties": {
        "trainedModelName": "ModelName",
        "trainedModelLinkedServiceName": {
                    "type": "LinkedServiceReference",
                    "referenceName": "StorageLinkedService"
                },
        "trainedModelFilePath": "ilearner file path"
    }
}
属性 描述 必需
name 管道中活动的名称
description 描述活动用途的文本。
type 对于机器学习工作室(经典版)“更新资源”活动,活动类型为 AzureMLUpdateResource。
linkedServiceName 包含 updateResourceEndpoint 属性的机器学习工作室(经典版)链接服务。
trainedModelName 将在 Web 服务实验中进行更新的“已训练模型”模块的名称
trainedModelLinkedServiceName 存储着由更新操作上传的 ilearner 文件的 Azure 存储链接服务的名称
trainedModelFilePath trainedModelLinkedService 中表示由更新操作上传的 ilearner 文件的相对路径

端到端工作流

对模型进行重新训练以及更新预测性 Web 服务的整个操作过程涉及以下步骤:

  • 使用“批处理执行”活动调用训练 Web 服务。 调用训练 Web 服务的过程与使用机器学习工作室(经典版)和批处理执行活动创建预测管道中介绍的调用预测性 Web 服务的过程是相同的。 训练 Web 服务的输出是一个 iLearner 文件,可以用来更新预测性 Web 服务。
  • 通过使用“更新资源”活动调用预测性 Web 服务更新资源终结点来使用新训练的模型更新 Web 服务。

机器学习工作室(经典版)链接服务

若要使上面提到的端到端工作流能够工作,需要创建两项机器学习工作室(经典版)链接服务:

  1. 训练 Web 服务的机器学习工作室(经典版)链接服务,此链接服务由批处理执行活动使用,使用方式与使用机器学习工作室(经典版)和批处理执行活动创建预测管道中提到的方式相同。 不同之处是训练 Web 服务的输出是一个 iLearner 文件,“更新资源”活动随后使用该文件来更新预测性 Web 服务。
  2. 预测性 Web 服务的更新资源终结点的机器学习工作室(经典版)链接服务。 此链接服务由“更新资源”活动用来使用上一步骤中返回的 iLearner 文件更新预测性 Web 服务。

对于第二项机器学习工作室(经典版)链接服务,如果机器学习工作室(经典版)Web 服务是经典 Web 服务或新的 Web 服务,配置则是不同的。 下面的各部分分别讨论了不同之处。

Web 服务是新的 Azure 资源管理器 Web 服务时

如果 Web 服务是公开 Azure 资源管理器终结点的新类型 Web 服务,则无需添加第二个非默认终结点。 链接服务中的 updateResourceEndpoint 的格式如下:

https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resource-group-name}/providers/Microsoft.MachineLearning/webServices/{web-service-name}?api-version=2016-05-01-preview

可以在机器学习工作室(经典版)Web 服务门户上查询 Web 服务时获取 URL 中占位符的值。

新类型的更新资源终结点要求执行服务主体身份验证。 若要使用服务主体身份验证,请在 Microsoft Entra ID 中注册应用程序实体,并向其授予 Web 服务所属的订阅或资源组的“参与者”或“所有者”角色。 请参阅如何创建服务主体和分配权限来管理 Azure 资源。 记下下面的值,这些值用于定义链接服务:

  • 应用程序 ID
  • 应用程序密钥
  • 租户 ID

下面是一个示例链接服务定义:

{
    "name": "AzureMLLinkedService",
    "properties": {
        "type": "AzureML",
        "description": "The linked service for AML web service.",
        "typeProperties": {
            "mlEndpoint": "https://ussouthcentral.services.azureml.net/workspaces/0000000000000000	000000000000000000000/services/0000000000000000000000000000000000000/jobs?api-version=2.0",
            "apiKey": {
                "type": "SecureString",
                "value": "APIKeyOfEndpoint1"
            },
            "updateResourceEndpoint": "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resource-group-name}/providers/Microsoft.MachineLearning/webServices/{web-service-name}?api-version=2016-05-01-preview",
            "servicePrincipalId": "000000000-0000-0000-0000-0000000000000",
            "servicePrincipalKey": {
                "type": "SecureString",
                "value": "servicePrincipalKey"
            },
            "tenant": "mycompany.com"
        }
    }
}

以下方案提供更多详细信息。 其中包含从管道重新训练和更新机器学习工作室(经典版)模型的示例。

示例:重新训练和更新机器学习工作室(经典版)模型

本部分提供了一个示例管道,该管道使用 Azure 机器学习工作室(经典版)“批处理执行”活动来重新训练模型。 该管道还使用 Azure 机器学习工作室(经典版)更新资源活动来更新评分 Web 服务中的模型。 此外,本部分为示例中的所有链接服务、数据集和管道提供 JSON 片段。

Azure Blob 存储链接服务:

Azure 存储保留以下数据:

  • 定型数据。 机器学习工作室(经典版)训练 Web 服务的输入数据。
  • iLearner 文件。 机器学习工作室(经典版)训练 Web 服务的输出。 此文件也是更新资源活动的输入。

下面是链接服务的示例 JSON 定义:

{
    "name": "StorageLinkedService",
    "properties": {
        "type": "AzureStorage",
        "typeProperties": {
            "connectionString": "DefaultEndpointsProtocol=https;AccountName=name;AccountKey=key"
        }
    }
}

机器学习工作室(经典版)训练终结点的链接服务

以下 JSON 代码片段定义指向训练 Web 服务默认终结点的机器学习工作室(经典版)链接服务。

{
    "name": "trainingEndpoint",
    "properties": {
        "type": "AzureML",
        "typeProperties": {
            "mlEndpoint": "https://ussouthcentral.services.azureml.net/workspaces/xxx/services/--training experiment--/jobs",
            "apiKey": "myKey"
        }
    }
}

在 Azure 机器学习工作室(经典版)中,执行以下操作以获取 mlEndpoint 和 apiKey 的值 :

  1. 在左侧菜单上,单击“Web 服务”
  2. 在 Web 服务列表中,单击“定型 Web 服务”
  3. 单击“API 密钥”文本框旁的“复制”。 将剪贴板中的密钥粘贴到数据工厂 JSON 编辑器。
  4. 在 Azure 机器学习工作室(经典版)中,单击“批处理执行”链接 。
  5. 从“请求”部分复制“请求 URI”,然后将其粘贴到 JSON 编辑器中 。

Azure 机器学习工作室(经典版)可更新评分终结点的链接服务:

以下 JSON 代码片段定义了指向评分 Web 服务的可更新终结点的 Azure 机器学习工作室(经典版)链接服务。

{
    "name": "updatableScoringEndpoint2",
    "properties": {
        "type": "AzureML",
        "typeProperties": {
            "mlEndpoint": "https://ussouthcentral.services.azureml.net/workspaces/00000000eb0abe4d6bbb1d7886062747d7/services/00000000026734a5889e02fbb1f65cefd/jobs?api-version=2.0",
            "apiKey": "sooooooooooh3WvG1hBfKS2BNNcfwSO7hhY6dY98noLfOdqQydYDIXyf2KoIaN3JpALu/AKtflHWMOCuicm/Q==",
            "updateResourceEndpoint": "https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/Default-MachineLearning-SouthCentralUS/providers/Microsoft.MachineLearning/webServices/myWebService?api-version=2016-05-01-preview",
            "servicePrincipalId": "fe200044-c008-4008-a005-94000000731",
            "servicePrincipalKey": "zWa0000000000Tp6FjtZOspK/WMA2tQ08c8U+gZRBlw=",
            "tenant": "mycompany.com"
        }
    }
}

管道

管道具有两个活动:AzureMLBatchExecutionAzureMLUpdateResource。 “批处理执行”活动采用训练数据作为输入,并生成 iLearner 文件作为输出。 然后,“更新资源”活动采用此 iLearner 文件并使用它来更新预测性 Web 服务。

{
    "name": "LookupPipelineDemo",
    "properties": {
        "activities": [
            {
                "name": "amlBEGetilearner",
                "description": "Use AML BES to get the ileaner file from training web service",
                "type": "AzureMLBatchExecution",
                "linkedServiceName": {
                    "referenceName": "trainingEndpoint",
                    "type": "LinkedServiceReference"
                },
                "typeProperties": {
                    "webServiceInputs": {
                        "input1": {
                            "LinkedServiceName":{
                                "referenceName": "StorageLinkedService",
                                "type": "LinkedServiceReference"
                            },
                            "FilePath":"azuremltesting/input"
                        },
                        "input2": {
                            "LinkedServiceName":{
                                "referenceName": "StorageLinkedService",
                                "type": "LinkedServiceReference"
                            },
                            "FilePath":"azuremltesting/input"
                        }
                    },
                    "webServiceOutputs": {
                        "output1": {
                            "LinkedServiceName":{
                                "referenceName": "StorageLinkedService",
                                "type": "LinkedServiceReference"
                            },
                            "FilePath":"azuremltesting/output"
                        }
                    }
                }
            },
            {
                "name": "amlUpdateResource",
                "type": "AzureMLUpdateResource",
                "description": "Use AML Update Resource to update the predict web service",
                "linkedServiceName": {
                    "type": "LinkedServiceReference",
                    "referenceName": "updatableScoringEndpoint2"
                },
                "typeProperties": {
                    "trainedModelName": "ADFV2Sample Model [trained model]",
                    "trainedModelLinkedServiceName": {
                        "type": "LinkedServiceReference",
                        "referenceName": "StorageLinkedService"
                    },
                    "trainedModelFilePath": "azuremltesting/output/newModelForArm.ilearner"
                },
                "dependsOn": [
                    {
                        "activity": "amlbeGetilearner",
                        "dependencyConditions": [ "Succeeded" ]
                    }
                ]
            }
        ]
    }
}

参阅以下文章了解如何以其他方式转换数据: