使用工作室部署在设计器中训练的模型Use the studio to deploy models trained in the designer

本文介绍如何在 Azure 机器学习工作室中将设计器中经过训练的模型作为实时终结点进行部署。In this article, you learn how to deploy a trained model from the designer as a real-time endpoint in Azure Machine Learning studio.

工作室中的部署包括以下步骤:Deployment in the studio consists of the following steps:

  1. 注册已训练的模型。Register the trained model.
  2. 下载模型的输入脚本和 conda 依赖项文件。Download the entry script and conda dependencies file for the model.
  3. 将模型部署到计算目标。Deploy the model to a compute target.

还可以直接在设计器中部署模型,以跳过模型注册和文件下载步骤。You can also deploy models directly in the designer to skip model registration and file download steps. 这对于快速部署非常有用。This can be useful for rapid deployment. 有关详细信息,请参阅通过设计器部署模型For more information see, Deploy a model with the designer.

在设计器中训练的模型也可以通过 SDK 或命令行接口 (CLI) 进行部署。Models trained in the designer can also be deployed through the SDK or command-line interface (CLI). 有关详细信息,请参阅使用 Azure 机器学习部署现有模型For more information, see Deploy your existing model with Azure Machine Learning.

先决条件Prerequisites

注册模型Register the model

训练管道完成后,将训练的模型注册到 Azure 机器学习工作区,以在其他项目中访问该模型。After the training pipeline completes, register the trained model to your Azure Machine Learning workspace to access the model in other projects.

  1. 选择“训练模型”模块Select the Train Model module.

  2. 在有窗口中选择“输出 + 日志”选项卡。Select the Outputs + logs tab in the right pane.

  3. 选择“注册模型”图标齿轮图标的屏幕截图Select the Register Model icon Screenshot of the gear icon.

    右窗格“训练模型”模块的屏幕截图

  4. 输入模型的名称,然后选择“保存”。Enter a name for your model, then select Save .

注册模型后,该模型将显示在工作室中的“模型”资产页面。After registering your model, you can find it in the Models asset page in the studio.

“模型”资产页中的已注册模型的屏幕截图

下载输入口脚本文件和 conda 依赖项文件Download the entry script file and conda dependencies file

需要以下文件才能在 Azure 机器学习工作室中部署模型:You need the following files to deploy a model in Azure Machine Learning studio:

  • 入口脚本文件 - 加载训练的模型、处理来自请求的输入数据、执行实时推理,并返回结果。Entry script file - loads the trained model, processes input data from requests, does real-time inferences, and returns the result. 当“训练模型”模块完成时,设计器将自动生成 score.py 入口脚本文件。The designer automatically generates a score.py entry script file when the Train Model module completes.

  • Conda 依赖项文件 - 指定 Web 服务依赖的 pip 和 conda 包。Conda dependencies file - specifies which pip and conda packages your webservice depends on. 当“训练模型”模块完成时,设计器将自动创建 conda_env.yaml 文件。The designer automatically creates a conda_env.yaml file when the Train Model module completes.

可以在右窗格“训练模型”模块中下载这两个文件:You can download these two files in the right pane of the Train Model module:

  1. 选择 训练模型 模块。Select the Train Model module.

  2. 在“输出 + 日志”选项卡中,选择文件夹 trained_model_outputsIn the Outputs + logs tab, select the folder trained_model_outputs.

  3. 下载 conda_env.yaml 文件和 score.py 文件。Download the conda_env.yaml file and score.py file.

    用于在右窗格中进行部署的下载文件的屏幕截图

或者,可以在注册模型后,从“模型”资产页中下载这些文件:Alternatively, you can download the files from the Models asset page after registering your model:

  1. 导航到“模型”资产页。Navigate to the Models asset page.

  2. 选择要部署的模型。Select the model you want to deploy.

  3. 选择“项目”选项卡。Select the Artifacts tab.

  4. 选择 trained_model_outputs 文件夹。Select the trained_model_outputs folder.

  5. 下载 conda_env.yaml 文件和 score.py 文件。Download the conda_env.yaml file and score.py file.

    用于在模型详细信息页中进行部署的下载文件的屏幕截图

备注

score.py 文件提供与“评分模型”模块几乎相同的功能。The score.py file provides nearly the same functionality as the Score Model modules. 但是,某些模块(如为 SVD 推荐器评分为 Wide and Deep 推荐器评分对 Vowpal Wabbit 模型评分)具有不同计分模式的参数。However, some modules like Score SVD Recommender, Score Wide and Deep Recommender, and Score Vowpal Wabbit Model have parameters for different scoring modes. 你还可以在入口脚本中更改这些参数。You can also change those parameters in the entry script.

有关在 score.py 文件中设置参数的详细信息,请参阅配置入口脚本For more information on setting parameters in the score.py file, see the section, Configure the entry script.

部署模型Deploy the model

下载所需的文件后,就可以部署该模型了。After downloading the necessary files, you're ready to deploy the model.

  1. 在“模型”资产页,选择已注册的模型。In the Models asset page, select the registered model.

  2. 选择“部署”按钮。Select the Deploy button.

  3. 在“配置”菜单中,输入以下信息:In the configuration menu, enter the following information:

    提示

    在高级设置中,可以为部署设置 CPU/内存容量和其他参数。In Advanced setting, you can set CPU/Memory capacity and other parameters for deployment. 这些设置对于某些模型(如 PyTorch 模型)非常重要,它们消耗大量内存(约 4 GB)。These settings are important for certain models such as PyTorch models, which consume considerable amount of memery (about 4 GB).

  4. 选择“部署”将模型部署为实时终结点。Select Deploy to deploy your model as a real-time endpoint.

    在模型资产页中部署模型的屏幕截图

使用实时终结点Consume the real-time endpoint

部署成功后,可以在“终结点”资产页中找到实时终结点。After deployment succeeds, you can find the real-time endpoint in the Endpoints asset page. 找到 REST 终结点之后,客户端可以使用该终结点将请求提交到实时终结点。Once there, you will find a REST endpoint, which clients can use to submit requests to the real-time endpoint.

备注

设计器还会生成用于测试的示例数据 json 文件,你可以在 trained_model_outputs 文件夹中下载 _samples.jsonThe designer also generates a sample data json file for testing, you can download _samples.json in the trained_model_outputs folder.

使用以下代码示例使用实时终结点。Use the following code sample to consume a real-time endpoint.


import json
from pathlib import Path
from azureml.core.workspace import Workspace, Webservice
 
service_name = 'YOUR_SERVICE_NAME'
ws = Workspace.get(
    name='WORKSPACE_NAME',
    subscription_id='SUBSCRIPTION_ID',
    resource_group='RESOURCEGROUP_NAME'
)
service = Webservice(ws, service_name)
sample_file_path = '_samples.json'
 
with open(sample_file_path, 'r') as f:
    sample_data = json.load(f)
score_result = service.run(json.dumps(sample_data))
print(f'Inference result = {score_result}')

配置入口脚本Configure the entry script

设计器中的某些模块(如为 SVD 推荐器评分为 Wide and Deep 推荐器评分为 Vowpal Wabbit 模型评分)具有不同计分模式的参数。Some modules in the designer like Score SVD Recommender, Score Wide and Deep Recommender, and Score Vowpal Wabbit Model have parameters for different scoring modes. 本部分介绍如何在条目脚本文件中更新这些参数。In this section, you learn how to update these parameters in the entry script file too.

以下示例更新训练的“Wide & Deep 推荐器”模型的默认行为。The following example updates the default behavior for a trained Wide & Deep recommender model. 默认情况下,score.py 文件指示 Web 服务预测用户和项目之间的评级。By default, the score.py file tells the web service to predict ratings between users and items.

可以修改入口脚本文件以进行项目建议,并通过更改 recommender_prediction_kind 参数返回建议的项目。You can modify the entry script file to make item recommendations, and return recommended items by changing the recommender_prediction_kind parameter.

import os
import json
from pathlib import Path
from collections import defaultdict
from azureml.studio.core.io.model_directory import ModelDirectory
from azureml.designer.modules.recommendation.dnn.wide_and_deep.score. \
    score_wide_and_deep_recommender import ScoreWideAndDeepRecommenderModule
from azureml.designer.serving.dagengine.utils import decode_nan
from azureml.designer.serving.dagengine.converter import create_dfd_from_dict

model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'trained_model_outputs')
schema_file_path = Path(model_path) / '_schema.json'
with open(schema_file_path) as fp:
    schema_data = json.load(fp)


def init():
    global model
    model = ModelDirectory.load(load_from_dir=model_path)


def run(data):
    data = json.loads(data)
    input_entry = defaultdict(list)
    for row in data:
        for key, val in row.items():
            input_entry[key].append(decode_nan(val))

    data_frame_directory = create_dfd_from_dict(input_entry, schema_data)

    # The parameter names can be inferred from Score Wide and Deep Recommender module parameters:
    # convert the letters to lower cases and replace whitespaces to underscores.
    score_params = dict(
        trained_wide_and_deep_recommendation_model=model,
        dataset_to_score=data_frame_directory,
        training_data=None,
        user_features=None,
        item_features=None,
        ################### Note #################
        # Set 'Recommender prediction kind' parameter to enable item recommendation model
        recommender_prediction_kind='Item Recommendation',
        recommended_item_selection='From All Items',
        maximum_number_of_items_to_recommend_to_a_user=5,
        whether_to_return_the_predicted_ratings_of_the_items_along_with_the_labels='True')
    result_dfd, = ScoreWideAndDeepRecommenderModule().run(**score_params)
    result_df = result_dfd.data
    return json.dumps(result_df.to_dict("list"))

对于“Wide & Deep 推荐器”和“Vowpal Wabbit”模型,可以使用以下方法配置评分模式参数 :For Wide & Deep recommender and Vowpal Wabbit models, you can configure the scoring mode parameter using the following methods:

  • 参数名称是为 Vowpal Wabbit 模型评分为 Wide and Deep 推荐器评分的小写和下划线组合的参数名称;The parameter names are the lowercase and underscore combinations of parameter names for Score Vowpal Wabbit Model and Score Wide and Deep Recommender;
  • 模式类型参数值是相应选项名称的字符串。Mode type parameter values are strings of the corresponding option names. 以上述代码中的“推荐器预测类型”为例,该值可以是 'Rating Prediction''Item Recommendation'Take Recommender prediction kind in the above codes as example, the value can be 'Rating Prediction'or 'Item Recommendation'. 不允许其他值。Other values are not allowed.

对于 SVD 推荐器训练模型,参数名称和值可能不太明显,你可以查找下表以决定如何设置参数。For SVD recommender trained model, the parameter names and values maybe less obvious, and you can look up the tables below to decide how to set parameters.

为 SVD 推荐器评分中的参数名称Parameter name in Score SVD Recommender 入口脚本文件中的参数名称Parameter name in the entry script file
推荐器预测类型Recommender prediction kind prediction_kindprediction_kind
推荐的项选择内容Recommended item selection recommended_item_selectionrecommended_item_selection
单个用户的建议池的最小大小Minimum size of the recommendation pool for a single user min_recommendation_pool_sizemin_recommendation_pool_size
要推荐给用户的最大项目数Maximum number of items to recommend to a user max_recommended_item_countmax_recommended_item_count
是否返回项目的预测评级以及标签Whether to return the predicted ratings of the items along with the labels return_ratingsreturn_ratings

以下代码演示了如何为 SVD 推荐器设置参数,该推荐器使用所有六个参数来推荐附加了预测评级的评级项目。The following code shows you how to set parameters for an SVD recommender, which uses all six parameters to recommend rated items with predicted ratings attached.

score_params = dict(
        learner=model,
        test_data=DataTable.from_dfd(data_frame_directory),
        training_data=None,
        # RecommenderPredictionKind has 2 members, 'RatingPrediction' and 'ItemRecommendation'. You
        # can specify prediction_kind parameter with one of them.
        prediction_kind=RecommenderPredictionKind.ItemRecommendation,
        # RecommendedItemSelection has 3 members, 'FromAllItems', 'FromRatedItems', 'FromUndatedItems'.
        # You can specify recommended_item_selection parameter with one of them.
        recommended_item_selection=RecommendedItemSelection.FromRatedItems,
        min_recommendation_pool_size=1,
        max_recommended_item_count=3,
        return_ratings=True,
    )

后续步骤Next steps