教程 3:部署信用风险模型 - Azure 机器学习工作室(经典版)Tutorial 3: Deploy credit risk model - Azure Machine Learning Studio (classic)

适用于: yes机器学习工作室(经典) no Azure 机器学习APPLIES TO: yesMachine Learning Studio (classic) noAzure Machine Learning

在本教程中,我们将深入探讨开发预测分析解决方案的过程。In this tutorial, you take an extended look at the process of developing a predictive analytics solution. 我们将在机器学习工作室(经典版)中开发一个简单模型。You develop a simple model in Machine Learning Studio (classic). 然后将该模型部署为 Azure 机器学习 Web 服务。You then deploy the model as an Azure Machine Learning web service. 部署的模型将使用新数据进行预测。This deployed model can make predictions using new data. 本教程是 由三个部分构成的系列教程的第三部分This tutorial is part three of a three-part tutorial series.

假设用户需要根据他们提供的贷款申请相关信息预测个人的信用风险。Suppose you need to predict an individual's credit risk based on the information they gave on a credit application.

信用风险评估是个较为复杂的问题,但本教程会将其适当简化。Credit risk assessment is a complex problem, but this tutorial will simplify it a bit. 我们将使用它作为示例,展示如何使用 Microsoft Azure 机器学习工作室(经典版)来创建预测分析解决方案。You'll use it as an example of how you can create a predictive analytics solution using Microsoft Azure Machine Learning Studio (classic). 对此解决方案,我们将使用 Azure 机器学习工作室(经典)和机器学习 Web 服务。You'll use Azure Machine Learning Studio (classic) and a Machine Learning web service for this solution.

在这篇由三个部分构成的教程中,我们将从公开的信用风险数据着手。In this three-part tutorial, you start with publicly available credit risk data. 然后开发并训练预测模型。You then develop and train a predictive model. 最后将该模型部署为 Web 服务。Finally you deploy the model as a web service.

本教程的第一部分中,已创建了一个机器学习工作室(经典版)工作区、上传了数据,并创建了试验。In part one of the tutorial, you created a Machine Learning Studio (classic) workspace, uploaded data, and created an experiment.

本教程的第二部分,我们已训练并评估模型。In part two of the tutorial, you trained and evaluated models.

本教程部分介绍以下操作:In this part of the tutorial you:

  • 准备部署Prepare for deployment
  • 部署 Web 服务Deploy the web service
  • 测试 Web 服务Test the web service
  • 管理 Web 服务Manage the web service
  • 访问 Web 服务Access the web service

先决条件Prerequisites

完成本教程的第二部分Complete part two of the tutorial.

准备部署Prepare for deployment

为了让其他人可以使用本教程中开发的预测模型,可将它部署为 Azure 上的 Web 服务。To give others a chance to use the predictive model you've developed in this tutorial, you can deploy it as a web service on Azure.

截至目前,我们一直在试验如何训练模型。Up to this point you've been experimenting with training our model. 但是已部署的服务不再进行训练,它会根据模型,通过对用户的输入进行评分来生成新的预测。But the deployed service is no longer going to do training - it's going to generate new predictions by scoring the user's input based on our model. 因此我们需要做好一些准备工作,将此试验从训练试验转换为预测试验。So we're going to do some preparation to convert this experiment from a training _ experiment to a _predictive*_ experiment.

部署准备过程由三个步骤构成:Preparation for deployment is a three-step process:

  1. 删除一个模型Remove one of the models
  2. 将创建的训练试验转换为预测试验Convert the _training experiment* you've created into a predictive experiment
  3. 将预测试验外部署为 Web 服务Deploy the predictive experiment as a web service

删除一个模型Remove one of the models

首先,需要稍微修整一下此试验。First, you need to trim this experiment down a little. 目前试验中有两个不同的模型,但将试验部署为 Web 服务时,只需要一个模型。you currently have two different models in the experiment, but you only want to use one model when you deploy this as a web service.

假设已确定提升树模型的性能强于 SVM 模型。Let's say you've decided that the boosted tree model performed better than the SVM model. 因此,首先是删除双类支持向量机模块,以及用于训练该模块的其他模块。So the first thing to do is remove the Two-Class Support Vector Machine module and the modules that were used for training it. 可能需要单击试验画布底部的“另存为”,先创建一个试验副本。You may want to make a copy of the experiment first by clicking Save As at the bottom of the experiment canvas.

需要删除以下模块:you need to delete the following modules:

选择每个模块并按 Delete 键,或右键单击模块并选择“删除”。Select each module and press the Delete key, or right-click the module and select Delete.

突出显示要删除的模块以删除支持向量机模型

模型现在看起来应与下图类似:Our model should now look something like this:

删除支持向量机模型时产生的试验

现已准备好使用双类提升决策树部署此模型。Now we're ready to deploy this model using the Two-Class Boosted Decision Tree.

将训练实验转换为预测实验Convert the training experiment to a predictive experiment

要使此模型准备好进行部署,需要将此训练试验转换为预测试验。To get this model ready for deployment, you need to convert this training experiment to a predictive experiment. 这涉及到三个步骤:This involves three steps:

  1. 保存已训练的模型,并替换训练模块Save the model you've trained and then replace our training modules
  2. 修整试验,删除只有训练需要的模块Trim the experiment to remove modules that were only needed for training
  3. 定义 Web 服务接受输入的位置以及生成输出的位置Define where the web service will accept input and where it generates the output

可以手动执行此操作,不过幸好上述三个步骤只需单击试验画布底部的“设置 Web 服务”(以及选择“预测 Web 服务”选项)即可完成。you could do this manually, but fortunately all three steps can be accomplished by clicking Set Up Web Service at the bottom of the experiment canvas (and selecting the Predictive Web Service option).

提示

要更详细地了解在将训练试验转换为预测试验时会发生什么情况,请参阅如何准备模型以便在 Azure 机器学习工作室(经典版)中进行部署If you want more details on what happens when you convert a training experiment to a predictive experiment, see How to prepare your model for deployment in Azure Machine Learning Studio (classic).

单击“设置 Web 服务”时,会发生以下情况:When you click Set Up Web Service, several things happen:

  • 训练的模型已转换为单个“已训练模型”模块并存储在实验画布左侧的模块控制板中(可以在“已训练模型”下找到此模块)The trained model is converted to a single Trained Model module and stored in the module palette to the left of the experiment canvas (you can find it under Trained Models)
  • 用于训练的模块被删除,具体包括:Modules that were used for training are removed; specifically:
  • 保存的训练模型已添加回实验中The saved trained model is added back into the experiment
  • “Web 服务输入”和“Web 服务输出”模块已添加(这些模块标识用户的数据会在何处进入模型、返回什么数据、何时访问 Web 服务)Web service input and Web service output modules are added (these identify where the user's data will enter the model, and what data is returned, when the web service is accessed)

备注

可以看到,实验分两部分保存在多个选项卡下,这些选项卡已添加到实验画布顶部。You can see that the experiment is saved in two parts under tabs that have been added at the top of the experiment canvas. 原始训练实验位于“训练实验”选项卡下,新创建的预测实验位于“预测实验”下。The original training experiment is under the tab Training experiment, and the newly created predictive experiment is under Predictive experiment. 将预测试验是将要部署为 Web 服务的试验。The predictive experiment is the one you'll deploy as a web service.

需要对此特定试验执行一个额外的步骤。you need to take one additional step with this particular experiment. 已添加两个执行 R 脚本模块来向数据提供加权函数。you added two Execute R Script modules to provide a weighting function to the data. 该函数仅用于训练和测试,因此可以在最终模型中移除这些模块。That was just a trick you needed for training and testing, so you can take out those modules in the final model. 机器学习工作室已在删除拆分模块时删除了一个执行 R 脚本模块。Machine Learning Studio (classic) removed one Execute R Script module when it removed the Split module. 现在可以删除另一个模块,并直接将元数据编辑器连接到评分模型Now you can remove the other and connect Metadata Editor directly to Score Model.

试验现在应如下所示:Our experiment should now look like this:

为训练的模型评分

备注

你可能会疑惑我们为何要在预测试验中留下“UCI 德国信用卡数据”数据集。You may be wondering why you left the UCI German Credit Card Data dataset in the predictive experiment. 服务要评分的是用户数据,而不是原始数据集,为何要让原始数据集保留在模型中?The service is going to score the user's data, not the original dataset, so why leave the original dataset in the model?

服务的确无需原始信用卡数据,It's true that the service doesn't need the original credit card data. 但需要该数据的架构,例如,有多少个列及哪些列是数值等信息。But it does need the schema for that data, which includes information such as how many columns there are and which columns are numeric. 需要此架构信息才能解释用户的数据。This schema information is necessary to interpret the user's data. 保留连接这些组件的目的是在运行服务时,评分模型可获得数据集架构。you leave these components connected so that the scoring module has the dataset schema when the service is running. 使用的不是数据,而只是架构。The data isn't used, just the schema.

需要注意的一件重要事情是,如果原始数据集包含标签,那么Web 输入中的所需架构也会需要带标签的列!One important thing to note is that if your original dataset contained the label, then the expected schema from the web input will also expect a column with the label! 解决此问题的一种方法是在将 Web 输入和训练数据集连接到通用模块之前,删除标签以及在训练数据集中但不在 Web 输入中的任何其他数据。A way around this is to remove the label, and any other data that was in the training dataset, but will not be in the web inputs, before connecting the web input and training dataset into a common module.

最后一次运行试验(单击“运行”)。要验证模型是否仍然正常工作,请单击评分模型模块的输出,并选择“查看结果”。Run the experiment one last time (click Run.) If you want to verify that the model is still working, click the output of the Score Model module and select View Results. 此时将显示原始数据,以及信用风险值(“评分标签”)和评分概率值(“评分概率”)。You can see that the original data is displayed, along with the credit risk value ("Scored Labels") and the scoring probability value ("Scored Probabilities".)

部署 Web 服务Deploy the web service

可以将实验部署为经典 Web 服务或基于 Azure 资源管理器的新 Web 服务。You can deploy the experiment as either a Classic web service, or as a New web service that's based on Azure Resource Manager.

部署为经典 Web 服务Deploy as a Classic web service

要部署派生自试验的经典 Web 服务,请单击画布下面的“部署 Web 服务”,并选择“部署 Web 服务 [经典]”。To deploy a Classic web service derived from our experiment, click Deploy Web Service below the canvas and select Deploy Web Service [Classic]. 机器学习工作室(经典版)会将试验部署为 Web 服务,并你将转到该 Web 服务的仪表板。Machine Learning Studio (classic) deploys the experiment as a web service and takes you to the dashboard for that web service. 在此页中可以返回试验(“查看快照”或“查看最新”),以及运行简单的 Web 服务测试(请参阅下面的 测试 Web 服务)。From this page, you can return to the experiment (View snapshot or View latest) and run a simple test of the web service (see Test the web service below). 此处,还提供了有关创建可访问 Web 服务的应用程序的信息(本教程的下一步骤提供了更多相关信息)。There is also information here for creating applications that can access the web service (more on that in the next step of this tutorial).

Web 服务仪表板

可以单击“配置”选项卡来配置服务。可以在此处修改服务名称(默认指定试验名称)并输入说明。You can configure the service by clicking the CONFIGURATION tab. Here you can modify the service name (it's given the experiment name by default) and give it a description. 此外,可为输入和输出数据指定更友好的标签。You can also give more friendly labels for the input and output data.

配置 Web 服务

部署为新 Web 服务Deploy as a New web service

备注

若要部署新的 Web 服务,必须对要部署 Web 服务的订阅拥有充分的权限。To deploy a New web service you must have sufficient permissions in the subscription to which you are deploying the web service. 有关详细信息,请参阅使用 Azure 机器学习 Web 服务门户管理 Web 服务For more information, see Manage a web service using the Azure Machine Learning Web Services portal.

若要部署从实验派生的新的 Web 服务,请执行以下操作:To deploy a New web service derived from our experiment:

  1. 单击画布下面的“部署 Web 服务”,并选择“部署 Web 服务 [新]”。Click Deploy Web Service below the canvas and select Deploy Web Service [New]. 机器学习工作室(经典版)会你将转到 Azure 机器学习 Web 服务的“部署试验”页。Machine Learning Studio (classic) transfers you to the Azure Machine Learning web services Deploy Experiment page.

  2. 输入 Web 服务的名称。Enter a name for the web service.

  3. 对于“价格计划”,可以选择现有的定价计划,也可以选择“新建”,并为新计划提供一个名称并选择每月计划选项。For Price Plan, you can select an existing pricing plan, or select "Create new" and give the new plan a name and select the monthly plan option. 计划层默认为默认区域的计划,并且 Web 服务将部署到该区域。The plan tiers default to the plans for your default region and your web service is deployed to that region.

  4. 单击“部署”。Click Deploy.

数分钟后,会打开 Web 服务的“快速入门”页。After a few minutes, the Quickstart page for your web service opens.

可以单击“配置”选项卡来配置服务。可以在此处修改服务标题并为其提供说明。You can configure the service by clicking the Configure tab. Here you can modify the service title and give it a description.

若要测试 Web 服务,请单击“测试”选项卡(请参阅下面的 测试 Web 服务)。To test the web service, click the Test tab (see Test the web service below). 有关创建可访问 Web 服务的应用程序的信息,请单击“使用”选项卡(本教程的下一步骤会提供更多详细信息)。For information on creating applications that can access the web service, click the Consume tab (the next step in this tutorial will go into more detail).

提示

部署 Web 服务后可以更新它。You can update the web service after you've deployed it. 例如,如果想要更改模型,请编辑训练实验、调整模型参数,并单击“部署 Web 服务”,选择“部署 Web 服务 [经典]”或“部署 Web 服务 [新]”。For example, if you want to change your model, then you can edit the training experiment, tweak the model parameters, and click Deploy Web Service, selecting Deploy Web Service [Classic] or Deploy Web Service [New]. 重新部署试验时,该试验将替换 Web 服务,然后,使用的就是已更新的模型了。When you deploy the experiment again, it replaces the web service, now using your updated model.

测试 Web 服务Test the web service

访问 Web 服务时,用户的数据将通过“Web 服务输入”模块传递给评分模型模块并评分。When the web service is accessed, the user's data enters through the Web service input module where it's passed to the Score Model module and scored. 根据设置预测试验的方式,模型预期采用的数据格式与原始信用风险数据集的格式相同。The way you've set up the predictive experiment, the model expects data in the same format as the original credit risk dataset. 然后,结果将通过“Web 服务输出”模块从 Web 服务返回给用户。The results are returned to the user from the web service through the Web service output module.

提示

根据预测试验的配置方式,将返回评分模型模块的整个结果。The way you have the predictive experiment configured, the entire results from the Score Model module are returned. 这包括所有输入数据以及信用风险值和评分概率。This includes all the input data plus the credit risk value and the scoring probability. 如果希望返回不同的结果 - 例如,只返回信用风险值,But you can return something different if you want - for example, you could return just the credit risk value. 可以在 评分模型Web 服务输出 之间插入 选择列模块,用于排除不想要让 Web 服务返回的列。To do this, insert a Select Columns module between Score Model and the Web service output to eliminate columns you don't want the web service to return.

可以在 机器学习工作室(经典版)Azure 机器学习 Web 服务 门户中测试经典 Web 服务。You can test a Classic web service either in Machine Learning Studio (classic) or in the Azure Machine Learning Web Services portal. 只能在 Azure 机器学习 Web 服务 门户中测试新的 Web 服务。You can test a New web service only in the Machine Learning Web Services portal.

提示

在 Azure 机器学习 Web 服务门户中测试时,可以让门户创建用于测试请求-响应服务的示例数据。When testing in the Azure Machine Learning Web Services portal, you can have the portal create sample data that you can use to test the Request-Response service. 在“配置”页上,针对“启用示例数据?”选择“是”。On the Configure page, select "Yes" for Sample Data Enabled?. 打开“测试”页上的“请求-响应”选项卡时,门户会在其中填充取自原始信用风险数据集的示例数据。When you open the Request-Response tab on the Test page, the portal fills in sample data taken from the original credit risk dataset.

测试经典 Web 服务Test a Classic web service

可以在机器学习工作室(经典版)或机器学习 Web 服务门户中测试经典 Web 服务。You can test a Classic web service in Machine Learning Studio (classic) or in the Machine Learning Web Services portal.

在机器学习工作室(经典版)中测试Test in Machine Learning Studio (classic)

  1. 在 Web 服务的“仪表板”页上,单击“默认终结点”下的“测试”按钮。On the DASHBOARD page for the web service, click the Test button under Default Endpoint. 将弹出一个对话框,要求用户输入服务的输入数据。A dialog pops up and asks you for the input data for the service. 这些是在原始信用风险数据集中出现的列。These are the same columns that appeared in the original credit risk dataset.

  2. 输入一组数据,并单击“确定”。Enter a set of data and then click OK.

在机器学习 Web 服务门户中测试Test in the Machine Learning Web Services portal

  1. 在 Web 服务的“仪表板”页上,单击“默认终结点”下的“测试预览”链接。On the DASHBOARD page for the web service, click the Test preview link under Default Endpoint. Azure 机器学习 Web 服务门户中的 Web 服务终结点测试页将打开,并要求提供服务的输入数据。The test page in the Azure Machine Learning Web Services portal for the web service endpoint opens and asks you for the input data for the service. 这些是在原始信用风险数据集中出现的列。These are the same columns that appeared in the original credit risk dataset.

  2. 单击“测试请求-响应”。Click Test Request-Response.

测试新的 Web 服务Test a New web service

只能在 Azure 机器学习 Web 服务门户中测试新的 Web 服务。You can test a New web service only in the Machine Learning Web Services portal.

  1. Azure 机器学习 Web 服务门户中,单击页面顶部的“测试”。In the Azure Machine Learning Web Services portal, click Test at the top of the page. 此时会打开“测试”页,可在其中输入服务的数据。The Test page opens and you can input data for the service. 显示的输入字段对应于原始信用风险数据集中出现的列。The input fields displayed correspond to the columns that appeared in the original credit risk dataset.

  2. 输入一组数据,并单击“测试请求-响应”。Enter a set of data and then click Test Request-Response.

测试结果会显示在页面右侧的输出列中。The results of the test are displayed on the right-hand side of the page in the output column.

管理 Web 服务Manage the web service

部署 Web 服务(经典或新式)后,可以从 Microsoft Azure 机器学习 Web 服务门户管理它。Once you've deployed your web service, whether Classic or New, you can manage it from the Microsoft Azure Machine Learning Web Services portal.

监视 Web 服务的性能:To monitor the performance of your web service:

  1. 登录到 Microsoft Azure 机器学习 Web 服务门户Sign in to the Microsoft Azure Machine Learning Web Services portal
  2. 单击“Web 服务”Click Web services
  3. 单击 Web 服务Click your web service
  4. 单击“仪表板”Click the Dashboard

访问 Web 服务Access the web service

本教程的上一步骤已部署了使用信用风险预测模型的 Web 服务。In the previous step in this tutorial, you deployed a web service that uses your credit risk prediction model. 现在用户能够向其发送数据并收到结果。Now users are able to send data to it and receive results.

Web 服务是一种 Azure Web 服务,可以两种方式之一使用 REST API 接收和返回数据:The Web service is an Azure web service that can receive and return data using REST APIs in one of two ways:

  • 请求/响应 - 用户通过使用 HTTP 协议,向服务发送信用数据的一个或多个行,而服务会使用一个或多个结果集进行响应。Request/Response - The user sends one or more rows of credit data to the service by using an HTTP protocol, and the service responds with one or more sets of results.
  • Batch 执行 - 用户将信用数据的一个或多个行存储在 Azure blob 中,然后将 blob 位置发送到服务。Batch Execution - The user stores one or more rows of credit data in an Azure blob and then sends the blob location to the service. 服务将对输入 blob 中数据的所有行进行评分,将结果存储到另一 blob 中,并返回该容器的 URL。The service scores all the rows of data in the input blob, stores the results in another blob, and returns the URL of that container.

备注

工作室(经典)中的功能列名称区分大小写。Feature column names in Studio (classic) are case sensitive. 请确保调用 Web 服务的输入数据与训练数据集中的列名称相同。Make sure your input data for invoking the web service has the same column names as in the training dataset.

有关访问和使用 Web 服务的详细信息,请参阅通过 Web 应用模板使用 Azure 机器学习 Web 服务For more information on accessing and consuming the web service, see Consume an Azure Machine Learning Web service with a web app template.

清理资源Clean up resources

如果不再需要通过本文创建的资源,请删除它们,以免产生费用。If you no longer need the resources you created using this article, delete them to avoid incurring any charges. 导出和删除产品内用户数据一文中了解具体信息。Learn how in the article, Export and delete in-product user data.

后续步骤Next steps

在本教程中,我们已完成以下步骤:In this tutorial, you completed these steps:

  • 准备部署Prepare for deployment
  • 部署 Web 服务Deploy the web service
  • 测试 Web 服务Test the web service
  • 管理 Web 服务Manage the web service
  • 访问 Web 服务Access the web service

还可使用 R、C# 和 Python 编程语言中提供的起始代码开发自定义应用程序,以访问 Web 服务。You can also develop a custom application to access the web service using starter code provided for you in R, C#, and Python programming languages.