将模型部署到 Azure 机器学习计算实例Deploy a model to Azure Machine Learning compute instances

适用于:是基本版是企业版               (升级到企业版APPLIES TO: yesBasic edition yesEnterprise edition                    (Upgrade to Enterprise edition)

了解如何使用 Azure 机器学习将模型部署为 Azure 机器学习计算实例上的 Web 服务。Learn how to use Azure Machine Learning to deploy a model as a web service on your Azure Machine Learning compute instance. 如果满足下列任一条件,请使用计算实例:Use compute instances if one of the following conditions is true:

  • 你需要快速部署并验证你的模型。You need to quickly deploy and validate your model.
  • 正在测试一个开发中的模型。You are testing a model that is under development.

提示

将模型从计算实例上的 Jupyter Notebook 部署到同一 VM 上的 Web 服务是本地部署。Deploying a model from a Jupyter Notebook on a compute instance, to a web service on the same VM is a local deployment. 在这种情况下,“本地”计算机是计算实例。In this case, the 'local' computer is the compute instance. 有关部署的详细信息,请参阅使用 Azure 机器学习部署模型For more information on deployments, see Deploy models with Azure Machine Learning.

先决条件Prerequisites

部署到计算实例Deploy to the compute instances

用于演示本地部署的示例笔记本包含在计算实例中。An example notebook that demonstrates local deployments is included on your compute instance. 使用以下步骤加载笔记本,并将模型部署为 VM 上的 Web 服务:Use the following steps to load the notebook and deploy the model as a web service on the VM:

  1. Azure 机器学习工作室选择 Azure 机器学习计算实例。From Azure Machine Learning studio, select your Azure Machine Learning compute instances.

  2. 打开 samples-* 子目录,然后打开 how-to-use-azureml/deploy-to-local/register-model-deploy-local.ipynbOpen the samples-* subdirectory, and then open how-to-use-azureml/deploy-to-local/register-model-deploy-local.ipynb. 打开后,运行笔记本。Once open, run the notebook.

    笔记本上运行的本地服务的屏幕截图

  3. 笔记本会显示其上有服务运行的 URL 和端口。The notebook displays the URL and port that the service is running on. 例如,https://localhost:6789For example, https://localhost:6789. 还可以运行包含 print('Local service port: {}'.format(local_service.port)) 的单元格以显示端口。You can also run the cell containing print('Local service port: {}'.format(local_service.port)) to display the port.

    正在运行的本地服务端口的屏幕截图

  4. 若要从计算实例测试服务,请使用 https://localhost:<local_service.port> URL。To test the service from a compute instance, use the https://localhost:<local_service.port> URL. 若要从远程客户端进行测试,请获取在计算实例上运行的服务的公共 URL。To test from a remote client, get the public URL of the service running on the compute instance. 可以使用以下公式来确定公共 URL;The public URL can be determined use the following formula;

    • 笔记本 VM:https://<vm_name>-<local_service_port>.<azure_region_of_workspace>.notebooks.azureml.net/scoreNotebook VM: https://<vm_name>-<local_service_port>.<azure_region_of_workspace>.notebooks.azureml.net/score.
    • 计算实例:https://<vm_name>-<local_service_port>.<azure_region_of_workspace>.instances.azureml.net/scoreCompute instance: https://<vm_name>-<local_service_port>.<azure_region_of_workspace>.instances.azureml.net/score.

    例如,For example,

    • 笔记本 VM:https://vm-name-6789.northcentralus.notebooks.azureml.net/scoreNotebook VM: https://vm-name-6789.northcentralus.notebooks.azureml.net/score
    • 计算实例:https://vm-name-6789.northcentralus.instances.azureml.net/scoreCompute instance: https://vm-name-6789.northcentralus.instances.azureml.net/score

测试服务Test the service

若要将示例数据提交到正在运行的服务,请使用以下代码。To submit sample data to the running service, use the following code. service_url 值替换为在上一步获取的 URL:Replace the value of service_url with the URL of from the previous step:

备注

对计算实例上的部署进行身份验证时,将使用 Azure Active Directory 进行身份验证。When authenticating to a deployment on the compute instance, the authentication is made using Azure Active Directory. 对示例代码中 interactive_auth.get_authentication_header() 的调用将使用 AAD 对你进行身份验证,并返回一个标头,然后可使用该标头向计算实例上的服务进行身份验证。The call to interactive_auth.get_authentication_header() in the example code authenticates you using AAD, and returns a header that can then be used to authenticate to the service on the compute instance. 有关详细信息,请参阅为 Azure 机器学习资源和工作流设置身份验证For more information, see Set up authentication for Azure Machine Learning resources and workflows.

对 Azure Kubernetes 服务或 Azure 容器实例上的部署进行身份验证时,将使用不同的身份验证方法。When authenticating to a deployment on Azure Kubernetes Service or Azure Container Instances, a different authentication method is used. 有关详细信息,请参阅为 Azure 机器学习资源和工作流设置身份验证For more information on, see Set up authentication for Azure Machine Learning resources and workflows.

import requests
import json
from azureml.core.authentication import InteractiveLoginAuthentication

# Get a token to authenticate to the compute instance from remote
interactive_auth = InteractiveLoginAuthentication()
auth_header = interactive_auth.get_authentication_header()

# Create and submit a request using the auth header
headers = auth_header
# Add content type header
headers.update({'Content-Type':'application/json'})

# Sample data to send to the service
test_sample = json.dumps({'data': [
    [1,2,3,4,5,6,7,8,9,10],
    [10,9,8,7,6,5,4,3,2,1]
]})
test_sample = bytes(test_sample,encoding = 'utf8')

# Replace with the URL for your compute instance, as determined from the previous section
service_url = "https://vm-name-6789.northcentralus.notebooks.azureml.net/score"
# for a compute instance, the url would be https://vm-name-6789.northcentralus.instances.azureml.net/score
resp = requests.post(service_url, test_sample, headers=headers)
print("prediction:", resp.text)

后续步骤Next steps