更新已部署的 Web 服务Update a deployed web service

本文介绍如何更新通过 Azure 机器学习部署的 Web 服务。In this article, you learn how to update a web service that was deployed with Azure Machine Learning.

必备条件Prerequisites

本教程假定你已使用 Azure 机器学习部署了 Web 服务。This tutorial assumes you have already deployed a web service with Azure Machine Learning. 如果需要了解如何部署 Web 服务,请执行这些步骤If you need to learn how to deploy a web service, follow these steps.

更新 Web 服务Update web service

若要更新 Web 服务,请使用 update 方法。To update a web service, use the update method. 你可以更新 Web 服务,以使用可以在推理配置中指定的新模型、新入口脚本或新依赖项。You can update the web service to use a new model, a new entry script, or new dependencies that can be specified in an inference configuration. 有关详细信息,请参阅 Webservice.update 的文档。For more information, see the documentation for Webservice.update.

请参阅 AKS 服务更新方法。See AKS Service Update Method.

请参阅 ACI 服务更新方法。See ACI Service Update Method.

重要

创建模型的新版本时,必须手动更新要使用的每个服务。When you create a new version of a model, you must manually update each service that you want to use it.

不能使用 SDK 来更新从 Azure 机器学习设计器发布的 Web 服务。You can not use the SDK to update a web service published from the Azure Machine Learning designer.

使用 SDKUsing the SDK

下面的代码演示如何使用 SDK 更新 Web 服务的模型、环境和入口脚本:The following code shows how to use the SDK to update the model, environment, and entry script for a web service:

from azureml.core import Environment
from azureml.core.webservice import Webservice
from azureml.core.model import Model, InferenceConfig

# Register new model.
new_model = Model.register(model_path="outputs/sklearn_mnist_model.pkl",
                           model_name="sklearn_mnist",
                           tags={"key": "0.1"},
                           description="test",
                           workspace=ws)

# Use version 3 of the environment.
deploy_env = Environment.get(workspace=ws,name="myenv",version="3")
inference_config = InferenceConfig(entry_script="score.py",
                                   environment=deploy_env)

service_name = 'myservice'
# Retrieve existing service.
service = Webservice(name=service_name, workspace=ws)



# Update to new model(s).
service.update(models=[new_model], inference_config=inference_config)
service.wait_for_deployment(show_output=True)
print(service.state)
print(service.get_logs())

使用 CLIUsing the CLI

还可以使用 ML CLI 更新 Web 服务。You can also update a web service by using the ML CLI. 以下示例演示如何注册新模型,然后更新 Web 服务以使用新模型:The following example demonstrates registering a new model and then updating a web service to use the new model:

az ml model register -n sklearn_mnist  --asset-path outputs/sklearn_mnist_model.pkl  --experiment-name myexperiment --output-metadata-file modelinfo.json
az ml service update -n myservice --model-metadata-file modelinfo.json

提示

此示例使用 JSON 文档将模型信息从注册命令传递到更新命令。In this example, a JSON document is used to pass the model information from the registration command into the update command.

若要更新服务以使用新的入口脚本或环境,请创建推理配置文件并使用 ic 参数指定它。To update the service to use a new entry script or environment, create an inference configuration file and specify it with the ic parameter.

有关详细信息,请参阅 az ml 服务更新文档。For more information, see the az ml service update documentation.

后续步骤Next steps