安装和使用 Azure 机器学习的 CLI 扩展Install & use the CLI extension for Azure Machine Learning

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

Azure 机器学习 CLI 是 Azure CLI(适用于 Azure 平台的跨平台命令行接口)的一个扩展。The Azure Machine Learning CLI is an extension to the Azure CLI, a cross-platform command-line interface for the Azure platform. 此扩展提供用于操作 Azure 机器学习的命令。This extension provides commands for working with Azure Machine Learning. 通过此扩展可将机器学习活动自动化。It allows you to automate your machine learning activities. 以下列表提供了可以使用 CLI 扩展执行的一些示例操作:The following list provides some example actions that you can do with the CLI extension:

  • 运行试验以创建机器学习模型Run experiments to create machine learning models

  • 注册机器学习模型以供客户使用Register machine learning models for customer usage

  • 打包、部署和跟踪机器学习模型的生命周期Package, deploy, and track the lifecycle of your machine learning models

CLI 不能取代 Azure 机器学习 SDK。The CLI is not a replacement for the Azure Machine Learning SDK. 它是一个经过优化的补充工具,可以处理高度参数化的、非常适合自动化的任务。It is a complementary tool that is optimized to handle highly parameterized tasks which suit themselves well to automation.

先决条件Prerequisites

  • 若要使用 CLI,必须拥有 Azure 订阅。To use the CLI, you must have an Azure subscription. 如果没有 Azure 订阅,请在开始前创建一个试用帐户。If you don't have an Azure subscription, create a trial account before you begin. 立即试用Azure 试用版Try the Azure trial today.

  • 若要在本地环境中使用本文档所述的 CLI 命令,需要安装 Azure CLITo use the CLI commands in this document from your local environment, you need the Azure CLI.

完整参考文档Full reference docs

查找 Azure CLI 的 azure-cli-ml 扩展的完整参考文档Find the full reference docs for the azure-cli-ml extension of Azure CLI.

将 CLI 连接到 Azure 订阅Connect the CLI to your Azure subscription

可通过多种方法从 CLI 对 Azure 订阅进行身份验证。There are several ways that you can authenticate to your Azure subscription from the CLI. 最基本的方法是使用浏览器进行交互式身份验证。The most basic is to interactively authenticate using a browser. 若要以交互方式进行身份验证,请打开命令行或终端,然后使用以下命令:To authenticate interactively, open a command line or terminal and use the following command:

az login

如果 CLI 可以打开默认的浏览器,则它会打开该浏览器并加载登录页。If the CLI can open your default browser, it will do so and load a sign-in page. 否则,需要打开浏览器并按照命令行中的说明操作。Otherwise, you need to open a browser and follow the instructions on the command line. 按说明操作时,需要浏览到 https://aka.ms/devicelogin 并输入授权代码。The instructions involve browsing to https://aka.ms/devicelogin and entering an authorization code.

提示

登录后,你将看到与你的 Azure 帐户关联的订阅列表。After logging in, you see a list of subscriptions associated with your Azure account. isDefault: true 的订阅信息是当前已激活的 Azure CLI 命令订阅。The subscription information with isDefault: true is the currently activated subscription for Azure CLI commands. 此订阅必须与包含 Azure 机器学习工作区的订阅相同。This subscription must be the same one that contains your Azure Machine Learning workspace. 通过访问工作区的概述页,可以从 Azure 门户中找到订阅 ID。You can find the subscription ID from the Azure portal by visiting the overview page for your workspace. 还可以使用 SDK 从工作区对象获取订阅 ID。You can also use the SDK to get the subscription ID from the workspace object. 例如,Workspace.from_config().subscription_idFor example, Workspace.from_config().subscription_id.

若要选择另一个订阅,请使用 az account set -s <subscription name or ID> 命令,并指定要切换到的订阅名称或 ID。To select another subscription, use the az account set -s <subscription name or ID> command and specify the subscription name or ID to switch to. 有关订阅选择的详细信息,请参阅使用多个 Azure 订阅For more information about subscription selection, see Use multiple Azure Subscriptions.

有关其他身份验证方法,请参阅使用 Azure CLI 登录For other methods of authenticating, see Sign in with Azure CLI.

安装扩展Install the extension

若要安装机器学习 CLI 扩展,请使用以下命令:To install the Machine Learning CLI extension, use the following command:

az extension add -n azure-cli-ml

提示

可以在此处找到可与以下命令配合使用的示例文件。Example files you can use with the commands below can be found here.

出现提示时,选择 y 安装该扩展。When prompted, select y to install the extension.

若要验证是否已安装该扩展,请使用以下命令显示特定于机器学习的子命令列表:To verify that the extension has been installed, use the following command to display a list of ML-specific subcommands:

az ml -h

更新扩展Update the extension

若要更新机器学习 CLI 扩展,请使用以下命令:To update the Machine Learning CLI extension, use the following command:

az extension update -n azure-cli-ml

删除扩展Remove the extension

若要删除 CLI 扩展,请使用以下命令:To remove the CLI extension, use the following command:

az extension remove -n azure-cli-ml

资源管理Resource management

以下命令演示如何使用 CLI 来管理 Azure 机器学习所用的资源。The following commands demonstrate how to use the CLI to manage resources used by Azure Machine Learning.

  • 如果没有资源组,请创建一个资源组:If you do not already have one, create a resource group:

    az group create -n myresourcegroup -l chinaeast
    
  • 创建 Azure 机器学习工作区:Create an Azure Machine Learning workspace:

    az ml workspace create -w myworkspace -g myresourcegroup
    

    提示

    此命令创建一个基本版工作区。This command creates a basic edition workspace. 若要创建企业版工作区,请在 az ml workspace create 命令中使用 --sku enterprise 开关。To create an enterprise workspace, use the --sku enterprise switch with the az ml workspace create command. 有关 Azure 机器学习版本的详细信息,请参阅什么是 Azure 机器学习For more information on Azure Machine Learning editions, see What is Azure Machine Learning.

    有关详细信息,请参阅 az ml workspace createFor more information, see az ml workspace create.

  • 将工作区配置附加到某个文件夹,以启用 CLI 上下文感知。Attach a workspace configuration to a folder to enable CLI contextual awareness.

    az ml folder attach -w myworkspace -g myresourcegroup
    

    此命令创建包含示例 runconfig 和 conda 环境文件的 .azureml 子目录。This command creates a .azureml subdirectory that contains example runconfig and conda environment files. 此子目录还包含用来与 Azure 机器学习工作区通信的 config.json 文件。It also contains a config.json file that is used to communicate with your Azure Machine Learning workspace.

    有关详细信息,请参阅 az ml folder attachFor more information, see az ml folder attach.

  • 将某个 Azure Blob 容器附加为数据存储。Attach an Azure blob container as a Datastore.

    az ml datastore attach-blob  -n datastorename -a accountname -c containername
    

    有关详细信息,请参阅 az ml datastore attach-blobFor more information, see az ml datastore attach-blob.

  • 将文件上传到数据存储。Upload files to a Datastore.

    az ml datastore upload  -n datastorename -p sourcepath
    

    有关详细信息,请参阅 az ml datastore uploadFor more information, see az ml datastore upload.

  • 将某个 AKS 群集附加为计算目标。Attach an AKS cluster as a Compute Target.

    az ml computetarget attach aks -n myaks -i myaksresourceid -g myresourcegroup -w myworkspace
    

    有关详细信息,请参阅 az ml computetarget attach aksFor more information, see az ml computetarget attach aks

计算群集Compute clusters

  • 创建新的托管计算群集。Create a new managed compute cluster.

    az ml computetarget create amlcompute -n cpu --min-nodes 1 --max-nodes 1 -s STANDARD_D3_V2
    
  • 创建具有托管标识的新托管计算群集Create a new managed compute cluster with managed identity

    • 用户分配的托管标识User-assigned managed identity

      az ml computetarget create amlcompute --name cpu-cluster --vm-size Standard_NC6 --max-nodes 5 --assign-identity '/subscriptions/<subcription_id>/resourcegroups/<resource_group>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<user_assigned_identity>'
      
    • 系统分配的托管标识System-assigned managed identity

      az ml computetarget create amlcompute --name cpu-cluster --vm-size Standard_NC6 --max-nodes 5 --assign-identity '[system]'
      
  • 将托管标识添加到现有群集:Add a managed identity to an existing cluster:

    • 用户分配的托管标识User-assigned managed identity

      az ml computetarget amlcompute identity assign --name cpu-cluster '/subscriptions/<subcription_id>/resourcegroups/<resource_group>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<user_assigned_identity>'
      
    • 系统分配的托管标识System-assigned managed identity

      az ml computetarget amlcompute identity assign --name cpu-cluster '[system]'
      

    有关详细信息,请参阅 az ml computetarget create amlcomputeFor more information, see az ml computetarget create amlcompute.

备注

Azure 机器学习计算群集只支持一个系统分配的标识或支持多个用户分配的标识,而不能同时支持这二者。Azure Machine Learning compute clusters support only one system-assigned identity or multiple user-assigned identities, not both concurrently.

计算实例Compute instance

管理计算实例。Manage compute instances. 在下面的所有示例中,计算实例的名称均为“cpu”In all the examples below, the name of the compute instance is cpu

+ <span data-ttu-id="27905-167">创建新的 computeinstance。</span><span class="sxs-lookup"><span data-stu-id="27905-167">Create a new computeinstance.</span></span>

    ```azurecli
    az ml computetarget create computeinstance  -n cpu -s "STANDARD_D3_V2" -v
    ```

    <span data-ttu-id="27905-168">有关详细信息,请参阅 [az ml computetarget create computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/create?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-create-computeinstance)。</span><span class="sxs-lookup"><span data-stu-id="27905-168">For more information, see [az ml computetarget create computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/create?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-create-computeinstance).</span></span>

+ <span data-ttu-id="27905-169">停止 computeinstance。</span><span class="sxs-lookup"><span data-stu-id="27905-169">Stop a computeinstance.</span></span>

    ```azurecli
    az ml computetarget stop computeinstance -n cpu -v
    ```

    <span data-ttu-id="27905-170">有关详细信息,请参阅 [az ml computetarget stop computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-computeinstance-stop)。</span><span class="sxs-lookup"><span data-stu-id="27905-170">For more information, see [az ml computetarget stop computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-computeinstance-stop).</span></span>

+ <span data-ttu-id="27905-171">启动 computeinstance。</span><span class="sxs-lookup"><span data-stu-id="27905-171">Start a computeinstance.</span></span>

    ```azurecli
    az ml computetarget start computeinstance -n cpu -v
   ```

    <span data-ttu-id="27905-172">有关详细信息,请参阅 [az ml computetarget start computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-computeinstance-start)。</span><span class="sxs-lookup"><span data-stu-id="27905-172">For more information, see [az ml computetarget start computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-computeinstance-start).</span></span>

+ <span data-ttu-id="27905-173">重启 computeinstance。</span><span class="sxs-lookup"><span data-stu-id="27905-173">Restart a computeinstance.</span></span>

    ```azurecli
    az ml computetarget restart computeinstance -n cpu -v
   ```

    <span data-ttu-id="27905-174">有关详细信息,请参阅 [az ml computetarget restart computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-computeinstance-restart)。</span><span class="sxs-lookup"><span data-stu-id="27905-174">For more information, see [az ml computetarget restart computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget/computeinstance?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-computeinstance-restart).</span></span>

+ <span data-ttu-id="27905-175">删除 computeinstance。</span><span class="sxs-lookup"><span data-stu-id="27905-175">Delete a computeinstance.</span></span>

    ```azurecli
    az ml computetarget delete -n cpu -v
   ```

    <span data-ttu-id="27905-176">有关详细信息,请参阅 [az ml computetarget delete computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-delete)。</span><span class="sxs-lookup"><span data-stu-id="27905-176">For more information, see [az ml computetarget delete computeinstance](https://docs.microsoft.com/cli/azure/ext/azure-cli-ml/ml/computetarget?view=azure-cli-latest#ext-azure-cli-ml-az-ml-computetarget-delete).</span></span>

运行试验Run experiments

  • 开始运行试验。Start a run of your experiment. 使用此命令时,请为 -c 参数指定 runconfig 文件的名称(如果查看的是文件系统,此名称为 *.runconfig 前面的文本)。When using this command, specify the name of the runconfig file (the text before *.runconfig if you are looking at your file system) against the -c parameter.

    az ml run submit-script -c sklearn -e testexperiment train.py
    

    提示

    az ml folder attach 命令创建一个 .azureml 子目录,其中包含两个示例 runconfig 文件。The az ml folder attach command creates a .azureml subdirectory, which contains two example runconfig files.

    如果你的某个 Python 脚本以编程方式创建运行配置对象,则你可以使用 RunConfig.save() 将此对象另存为 runconfig 文件。If you have a Python script that creates a run configuration object programmatically, you can use RunConfig.save() to save it as a runconfig file.

    可在此 JSON 文件中找到完整的 runconfig 架构。The full runconfig schema can be found in this JSON file. 该架构通过每个对象的 description 键自行记录。The schema is self-documenting through the description key of each object. 此外,还有可能值的枚举,在末尾还有模板代码片段。Additionally, there are enums for possible values, and a template snippet at the end.

    有关详细信息,请参阅 az ml run submit-scriptFor more information, see az ml run submit-script.

  • 查看试验列表:View a list of experiments:

    az ml experiment list
    

    有关详细信息,请参阅 az ml experiment listFor more information, see az ml experiment list.

HyperDrive 运行HyperDrive run

可以结合使用 HyperDrive 与 Azure CLI 以执行参数优化运行。You can use HyperDrive with Azure CLI to perform parameter tuning runs. 首先,创建采用以下格式的 HyperDrive 配置文件。First, create a HyperDrive configuration file in the following format. 有关超参数优化参数的详细信息,请参阅优化模型的超参数一文。See Tune hyperparameters for your model article for details on hyperparameter tuning parameters.

# hdconfig.yml
sampling: 
    type: random # Supported options: Random, Grid, Bayesian
    parameter_space: # specify a name|expression|values tuple for each parameter.
    - name: --penalty # The name of a script parameter to generate values for.
      expression: choice # supported options: choice, randint, uniform, quniform, loguniform, qloguniform, normal, qnormal, lognormal, qlognormal
      values: [0.5, 1, 1.5] # The list of values, the number of values is dependent on the expression specified.
policy: 
    type: BanditPolicy # Supported options: BanditPolicy, MedianStoppingPolicy, TruncationSelectionPolicy, NoTerminationPolicy
    evaluation_interval: 1 # Policy properties are policy specific. See the above link for policy specific parameter details.
    slack_factor: 0.2
primary_metric_name: Accuracy # The metric used when evaluating the policy
primary_metric_goal: Maximize # Maximize|Minimize
max_total_runs: 8 # The maximum number of runs to generate
max_concurrent_runs: 2 # The number of runs that can run concurrently.
max_duration_minutes: 100 # The maximum length of time to run the experiment before cancelling.

将此文件与运行配置文件一起添加。Add this file alongside the run configuration files. 然后使用以下命令提交 HyperDrive 运行:Then submit a HyperDrive run using:

az ml run submit-hyperdrive -e <experiment> -c <runconfig> --hyperdrive-configuration-name <hdconfig> my_train.py

请注意运行配置中的参数部分和 HyperDrive 配置中的参数空间 。这些参数包含要传递给训练脚本的命令行参数。Note the arguments section in runconfig and parameter space in HyperDrive config. They contain the command-line arguments to be passed to training script. 在每次迭代中,运行配置中的值保持不变,而 HyperDrive 配置中的范围将会更迭。The value in runconfig stays the same for each iteration, while the range in HyperDrive config is iterated over. 请不要在这两个文件中指定相同的参数。Do not specify the same argument in both files.

数据集管理Dataset management

以下命令演示如何在 Azure 机器学习中处理数据集:The following commands demonstrate how to work with datasets in Azure Machine Learning:

  • 注册数据集:Register a dataset:

    az ml dataset register -f mydataset.json
    

    如需用于定义数据集的 JSON 文件的格式信息,请使用 az ml dataset register --show-templateFor information on the format of the JSON file used to define the dataset, use az ml dataset register --show-template.

    有关详细信息,请参阅 az ml dataset registerFor more information, see az ml dataset register.

  • 列出工作区中的所有数据集:List all datasets in a workspace:

    az ml dataset list
    

    有关详细信息,请参阅 az ml dataset listFor more information, see az ml dataset list.

  • 获取数据集的详细信息:Get details of a dataset:

    az ml dataset show -n dataset-name
    

    有关详细信息,请参阅 az ml dataset showFor more information, see az ml dataset show.

  • 取消注册数据集:Unregister a dataset:

    az ml dataset unregister -n dataset-name
    

    有关详细信息,请参阅 az ml dataset unregisterFor more information, see az ml dataset unregister.

环境管理Environment management

以下命令演示如何为工作区创建、注册和列出 Azure 机器学习环境The following commands demonstrate how to create, register, and list Azure Machine Learning environments for your workspace:

环境配置架构Environment configuration schema

如果使用 az ml environment scaffold 命令,它将生成一个模板 azureml_environment.json 文件,可以通过 CLI 修改该文件并使用它来创建自定义环境配置。If you used the az ml environment scaffold command, it generates a template azureml_environment.json file that can be modified and used to create custom environment configurations with the CLI. 顶层对象松散地映射到 Python SDK 中的 Environment 类。The top level object loosely maps to the Environment class in the Python SDK.

{
    "name": "testenv",
    "version": null,
    "environmentVariables": {
        "EXAMPLE_ENV_VAR": "EXAMPLE_VALUE"
    },
    "python": {
        "userManagedDependencies": false,
        "interpreterPath": "python",
        "condaDependenciesFile": null,
        "baseCondaEnvironment": null
    },
    "docker": {
        "enabled": false,
        "baseImage": "mcr.microsoft.com/azureml/base:intelmpi2018.3-ubuntu16.04",
        "baseDockerfile": null,
        "sharedVolumes": true,
        "shmSize": "2g",
        "arguments": [],
        "baseImageRegistry": {
            "address": null,
            "username": null,
            "password": null
        }
    },
    "spark": {
        "repositories": [],
        "packages": [],
        "precachePackages": true
    },
    "databricks": {
        "mavenLibraries": [],
        "pypiLibraries": [],
        "rcranLibraries": [],
        "jarLibraries": [],
        "eggLibraries": []
    },
    "inferencingStackVersion": null
}

下表详细介绍了 JSON 文件中的每个顶层字段、其类型和说明。The following table details each top-level field in the JSON file, it's type, and a description. 如果某个对象类型已链接到 Python SDK 中的类,则在每个 JSON 字段与 Python 类中的公共变量名称之间存在松散的 1:1 匹配。If an object type is linked to a class from the Python SDK, there is a loose 1:1 match between each JSON field and the public variable name in the Python class. 在某些情况下,字段可能会映射到构造函数参数,而不是类变量。In some cases the field may map to a constructor argument rather than a class variable. 例如,environmentVariables 字段映射到 Environment 类中的 environment_variables 变量。For example, the environmentVariables field maps to the environment_variables variable in the Environment class.

JSON 字段JSON field 类型Type 说明Description
name string 环境的名称。Name of the environment. 名称不能以 MicrosoftAzureML 开头。Do not start name with Microsoft or AzureML.
version string 环境的版本。Version of the environment.
environmentVariables {string: string} 环境变量名称和值的哈希映射。A hash-map of environment variable names and values.
python PythonSection 此对象定义要在目标计算资源上使用的 Python 环境和解释器。Object that defines the Python environment and interpreter to use on target compute resource.
docker DockerSection 定义根据环境规范对生成的 Docker 映像进行自定义时使用的设置。Defines settings to customize the Docker image built to the environment's specifications.
spark SparkSection 此节配置 Spark 设置。The section configures Spark settings. 仅当 framework 设置为 PySpark 时才使用它。It is only used when framework is set to PySpark.
inferencingStackVersion string 指定添加到映像的推理堆栈版本。Specifies the inferencing stack version added to the image. 若要避免添加推理堆栈,请将此字段保留为 nullTo avoid adding an inferencing stack, leave this field null. 有效值:latest。Valid value: "latest".

ML 管道管理ML pipeline management

以下命令演示如何使用机器学习管道:The following commands demonstrate how to work with machine learning pipelines:

模型注册、分析和部署Model registration, profiling, deployment

以下命令演示如何注册已训练的模型,然后将其部署为生产服务:The following commands demonstrate how to register a trained model, and then deploy it as a production service:

  • 将模型注册到 Azure 机器学习:Register a model with Azure Machine Learning:

    az ml model register -n mymodel -p sklearn_regression_model.pkl
    

    有关详细信息,请参阅 az ml model registerFor more information, see az ml model register.

  • (可选) 分析模型以获取部署的最佳 CPU 和内存值。OPTIONAL Profile your model to get optimal CPU and memory values for deployment.

    az ml model profile -n myprofile -m mymodel:1 --ic inferenceconfig.json -d "{\"data\": [[1,2,3,4,5,6,7,8,9,10],[10,9,8,7,6,5,4,3,2,1]]}" -t myprofileresult.json
    

    有关详细信息,请参阅 az ml model profileFor more information, see az ml model profile.

  • 将模型部署到 AKSDeploy your model to AKS

    az ml model deploy -n myservice -m mymodel:1 --ic inferenceconfig.json --dc deploymentconfig.json --ct akscomputetarget
    

    有关推理配置文件架构的详细信息,请参阅推理配置架构For more information on the inference configuration file schema, see Inference configuration schema.

    有关部署配置文件架构的详细信息,请参阅部署配置架构For more information on the deployment configuration file schema, see Deployment configuration schema.

    有关详细信息,请参阅 az ml model deployFor more information, see az ml model deploy.

推理配置架构Inference configuration schema

inferenceconfig.json 文档中的条目映射到 InferenceConfig 类的参数。The entries in the inferenceconfig.json document map to the parameters for the InferenceConfig class. 下表描述了 JSON 文档中的实体与方法参数之间的映射:The following table describes the mapping between entities in the JSON document and the parameters for the method:

JSON 实体JSON entity 方法参数Method parameter 说明Description
entryScript entry_script 包含要为映像运行的代码的本地文件路径。Path to a local file that contains the code to run for the image.
runtime runtime 可选。Optional. 用于映像的运行时。Which runtime to use for the image. 支持的运行时为 spark-pypythonSupported runtimes are spark-py and python. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
condaFile conda_file 可选。Optional. 包含要用于映像的 Conda 环境定义的本地文件路径。Path to a local file that contains a Conda environment definition to use for the image. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
extraDockerFileSteps extra_docker_file_steps 可选。Optional. 包含设置映像时要运行的附加 Docker 步骤的本地文件路径。Path to a local file that contains additional Docker steps to run when setting up the image. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
sourceDirectory source_directory 可选。Optional. 包含用于创建映像的所有文件的文件夹路径,因此可以轻松地访问此文件夹或子文件夹中的任何文件。Path to folders that contain all files to create the image, which makes it easy to access any files within this folder or subfolder. 可以从本地计算机上传整个文件夹,作为 Webservice 的依赖项。You can upload an entire folder from your local machine as dependencies for the Webservice. 注意:entry_script、conda_file 和 extra_docker_file_steps 路径是 source_directory 路径的相对路径。Note: your entry_script, conda_file, and extra_docker_file_steps paths are relative paths to the source_directory path.
enableGpu enable_gpu 可选。Optional. 是否在映像中启用 GPU 支持。Whether to enable GPU support in the image. GPU 映像必须在 Azure 服务上运行,如 Azure 容器实例。The GPU image must be used on an Azure service, like Azure Container Instances. 例如,Azure 机器学习计算、Azure 虚拟机和 Azure Kubernetes 服务。For example, Azure Machine Learning Compute, Azure Virtual Machines, and Azure Kubernetes Service. 默认值为 False。The default is False. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
baseImage base_image 可选。Optional. 要用作基础映像的自定义映像。Custom image to be used as a base image. 如果未提供基础映像,则该映像将基于提供的运行时参数。If no base image is provided, the image will be based on the provided runtime parameter. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
baseImageRegistry base_image_registry 可选。Optional. 包含基础映像的映像注册表。Image registry that contains the base image. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
cudaVersion cuda_version 可选。Optional. 要为需要 GPU 支持的映像安装的 CUDA 版本。Version of CUDA to install for images that need GPU support. 必须在 Azure 服务上使用 GPU 映像。The GPU image must be used on an Azure service. 例如,Azure 容器实例、Azure 机器学习计算、Azure 虚拟机和 Azure Kubernetes 服务。For example, Azure Container Instances, Azure Machine Learning Compute, Azure Virtual Machines, and Azure Kubernetes Service. 支持的版本为 9.0、9.1 和 10.0。Supported versions are 9.0, 9.1, and 10.0. 如果设置 enable_gpu,则默认为 9.1。If enable_gpu is set, the default is 9.1. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
description description 映像的说明。A description for the image. 如果设置 environment,则会忽略此项。If environment is set, this entry is ignored.
environment environment 可选。Optional. Azure 机器学习环境Azure Machine Learning environment.

以下 JSON 是用于 CLI 的推理配置示例:The following JSON is an example inference configuration for use with the CLI:

{
    "entryScript": "score.py",
    "runtime": "python",
    "condaFile": "myenv.yml",
    "extraDockerfileSteps": null,
    "sourceDirectory": null,
    "enableGpu": false,
    "baseImage": null,
    "baseImageRegistry": null
}

可以在推理配置文件中包含 Azure 机器学习环境的完整规范。You can include full specifications of an Azure Machine Learning environment in the inference configuration file. 如果工作区中不存在此环境,Azure 机器学习会创建它。If this environment doesn't exist in your workspace, Azure Machine Learning will create it. 否则,Azure 机器学习会更新环境(如有必要)。Otherwise, Azure Machine Learning will update the environment if necessary. 以下 JSON 是一个示例:The following JSON is an example:

{
    "entryScript": "score.py",
    "environment": {
        "docker": {
            "arguments": [],
            "baseDockerfile": null,
            "baseImage": "mcr.microsoft.com/azureml/base:intelmpi2018.3-ubuntu16.04",
            "enabled": false,
            "sharedVolumes": true,
            "shmSize": null
        },
        "environmentVariables": {
            "EXAMPLE_ENV_VAR": "EXAMPLE_VALUE"
        },
        "name": "my-deploy-env",
        "python": {
            "baseCondaEnvironment": null,
            "condaDependencies": {
                "channels": [
                    "conda-forge"
                ],
                "dependencies": [
                    "python=3.6.2",
                    {
                        "pip": [
                            "azureml-defaults",
                            "azureml-telemetry",
                            "scikit-learn",
                            "inference-schema[numpy-support]"
                        ]
                    }
                ],
                "name": "project_environment"
            },
            "condaDependenciesFile": null,
            "interpreterPath": "python",
            "userManagedDependencies": false
        },
        "version": "1"
    }
}

还可以在单独的 CLI 参数中使用现有 Azure 机器学习环境,并从推理配置文件中删除“环境”密钥。You can also use an existing Azure Machine Learning environment in separated CLI parameters and remove the "environment" key from the inference configuration file. 将 -e 用于环境名称,将 --ev 用于环境版本。Use -e for the environment name, and --ev for the environment version. 如果未指定 --ev,则会使用最新版本。If you don't specify --ev, the latest version will be used. 下面是推理配置文件的示例:Here is an example of an inference configuration file:

{
    "entryScript": "score.py",
    "sourceDirectory": null
}

以下命令演示如何使用以前的推理配置文件(名为 myInferenceConfig.json)来部署模型。The following command demonstrates how to deploy a model using the previous inference configuration file (named myInferenceConfig.json).

它还使用最新版本的现有 Azure 机器学习环境(名为 AzureML-Minimal)。It also uses the latest version of an existing Azure Machine Learning environment (named AzureML-Minimal).

az ml model deploy -m mymodel:1 --ic myInferenceConfig.json -e AzureML-Minimal --dc deploymentconfig.json

部署配置架构Deployment configuration schema

本地部署配置架构Local deployment configuration schema

deploymentconfig.json 文档中的条目对应于 LocalWebservice.deploy_configuration 的参数。The entries in the deploymentconfig.json document map to the parameters for LocalWebservice.deploy_configuration. 下表描述了 JSON 文档中的实体与方法参数之间的映射:The following table describes the mapping between the entities in the JSON document and the parameters for the method:

JSON 实体JSON entity 方法参数Method parameter 说明Description
computeType NANA 计算目标。The compute target. 对于本地目标,值必须是 localFor local targets, the value must be local.
port port 用于公开服务的 HTTP 终结点的本地端口。The local port on which to expose the service's HTTP endpoint.

此 JSON 是用于 CLI 的部署配置示例:This JSON is an example deployment configuration for use with the CLI:

{
    "computeType": "local",
    "port": 32267
}

Azure 容器实例部署配置架构Azure Container Instance deployment configuration schema

deploymentconfig.json 文档中的条目映射到 AciWebservice.deploy_configuration 的参数。The entries in the deploymentconfig.json document map to the parameters for AciWebservice.deploy_configuration. 下表描述了 JSON 文档中的实体与方法参数之间的映射:The following table describes the mapping between the entities in the JSON document and the parameters for the method:

JSON 实体JSON entity 方法参数Method parameter 说明Description
computeType 不可用NA 计算目标。The compute target. 对于 ACI,此值必须为 ACIFor ACI, the value must be ACI.
containerResourceRequirements 不可用NA CPU 和内存实体的容器。Container for the CPU and memory entities.
  cpu cpu_cores 要分配的 CPU 核心数。The number of CPU cores to allocate. 默认值为 0.1Defaults, 0.1
  memoryInGB memory_gb 为此 Web 服务分配的内存量 (GB)。The amount of memory (in GB) to allocate for this web service. 默认值为 0.5Default, 0.5
location location 要将此 Webservice 部署到的 Azure 区域。The Azure region to deploy this Webservice to. 如果未指定,将使用工作区位置。If not specified the Workspace location will be used. 有关可用区域的详细信息,请参阅:ACI 区域More details on available regions can be found here: ACI Regions
authEnabled auth_enabled 是否为此 Webservice 启用身份验证。Whether to enable auth for this Webservice. 默认为 FalseDefaults to False
sslEnabled ssl_enabled 是否为此 Webservice 启用 SSL。Whether to enable SSL for this Webservice. 默认为 False。Defaults to False.
appInsightsEnabled enable_app_insights 是否为此 Webservice 启用 AppInsights。Whether to enable AppInsights for this Webservice. 默认为 FalseDefaults to False
sslCertificate ssl_cert_pem_file 如果启用 SSL,则需要证书文件The cert file needed if SSL is enabled
sslKey ssl_key_pem_file 如果启用 SSL,则需要密钥文件The key file needed if SSL is enabled
cname ssl_cname 如果启用 SSL,则需要 CNAMEThe cname for if SSL is enabled
dnsNameLabel dns_name_label 评分终结点的 DNS 名称标签。The dns name label for the scoring endpoint. 如果未指定,则将为评分终结点生成唯一的 DNS 名称标签。If not specified a unique dns name label will be generated for the scoring endpoint.

以下 JSON 是用于 CLI 的部署配置示例:The following JSON is an example deployment configuration for use with the CLI:

{
    "computeType": "aci",
    "containerResourceRequirements":
    {
        "cpu": 0.5,
        "memoryInGB": 1.0
    },
    "authEnabled": true,
    "sslEnabled": false,
    "appInsightsEnabled": false
}

Azure Kubernetes 服务部署配置架构Azure Kubernetes Service deployment configuration schema

deploymentconfig.json 文档中的条目对应于 AksWebservice.deploy_configuration 的参数。The entries in the deploymentconfig.json document map to the parameters for AksWebservice.deploy_configuration. 下表描述了 JSON 文档中的实体与方法参数之间的映射:The following table describes the mapping between the entities in the JSON document and the parameters for the method:

JSON 实体JSON entity 方法参数Method parameter 说明Description
computeType 不可用NA 计算目标。The compute target. 对于 AKS,此值必须为 aksFor AKS, the value must be aks.
autoScaler 不可用NA 包含自动缩放的配置元素。Contains configuration elements for autoscale. 请参阅自动缩放程序表。See the autoscaler table.
  autoscaleEnabled autoscale_enabled 是否为 Web 服务启用自动缩放。Whether to enable autoscaling for the web service. 如果 numReplicas = 0,则为 True;否则为 FalseIf numReplicas = 0, True; otherwise, False.
  minReplicas autoscale_min_replicas 自动缩放此 Web 服务时可使用的容器的最小数目。The minimum number of containers to use when autoscaling this web service. 默认值为 1Default, 1.
  maxReplicas autoscale_max_replicas 自动缩放此 Web 服务时可使用的容器的最大数目。The maximum number of containers to use when autoscaling this web service. 默认值为 10Default, 10.
  refreshPeriodInSeconds autoscale_refresh_seconds 自动缩放程序尝试缩放此 Web 服务的频率。How often the autoscaler attempts to scale this web service. 默认值为 1Default, 1.
  targetUtilization autoscale_target_utilization 自动缩放程序应尝试维持的此 Web 服务的目标利用率(以低于 100 的百分比表示)。The target utilization (in percent out of 100) that the autoscaler should attempt to maintain for this web service. 默认值为 70Default, 70.
dataCollection 不可用NA 包含数据集合的配置元素。Contains configuration elements for data collection.
  storageEnabled collect_model_data 是否为 Web 服务启用模型数据收集。Whether to enable model data collection for the web service. 默认值为 FalseDefault, False.
authEnabled auth_enabled 是否为 Web 服务启用密钥身份验证。Whether or not to enable key authentication for the web service. tokenAuthEnabledauthEnabled 均不能为 TrueBoth tokenAuthEnabled and authEnabled cannot be True. 默认值为 TrueDefault, True.
tokenAuthEnabled token_auth_enabled 是否为 Web 服务启用令牌身份验证。Whether or not to enable token authentication for the web service. tokenAuthEnabledauthEnabled 均不能为 TrueBoth tokenAuthEnabled and authEnabled cannot be True. 默认值为 FalseDefault, False.
containerResourceRequirements 不可用NA CPU 和内存实体的容器。Container for the CPU and memory entities.
  cpu cpu_cores 要分配给此 Web 服务的 CPU 核心数。The number of CPU cores to allocate for this web service. 默认值为 0.1Defaults, 0.1
  memoryInGB memory_gb 为此 Web 服务分配的内存量 (GB)。The amount of memory (in GB) to allocate for this web service. 默认值为 0.5Default, 0.5
appInsightsEnabled enable_app_insights 是否为 Web 服务启用 Application Insights 日志记录。Whether to enable Application Insights logging for the web service. 默认值为 FalseDefault, False.
scoringTimeoutMs scoring_timeout_ms 对 Web 服务调用的评分强制执行的超时时间。A timeout to enforce for scoring calls to the web service. 默认值为 60000Default, 60000.
maxConcurrentRequestsPerContainer replica_max_concurrent_requests 此 Web 服务每个节点的最大并发请求数。The maximum concurrent requests per node for this web service. 默认值为 1Default, 1.
maxQueueWaitMs max_request_wait_time 在返回 503 错误之前,请求在队列中停留的最长时间(毫秒)。The maximum time a request will stay in thee queue (in milliseconds) before a 503 error is returned. 默认值为 500Default, 500.
numReplicas num_replicas 要分配给此 Web 服务的容器数量。The number of containers to allocate for this web service. 没有默认值。No default value. 如果未设置此参数,则默认启用自动缩放程序。If this parameter is not set, the autoscaler is enabled by default.
keys 不可用NA 包含密钥的配置元素。Contains configuration elements for keys.
  primaryKey primary_key 要用于此 Web 服务的主要身份验证密钥A primary auth key to use for this Webservice
  secondaryKey secondary_key 要用于此 Web 服务的辅助身份验证密钥A secondary auth key to use for this Webservice
gpuCores gpu_cores 要分配给此 Web 服务的 GPU 核心数。The number of GPU cores to allocate for this Webservice. 默认值为 1。Default is 1. 仅支持整数值。Only supports whole number values.
livenessProbeRequirements 不可用NA 包含运行情况探测要求的配置元素。Contains configuration elements for liveness probe requirements.
  periodSeconds period_seconds 执行运行情况探测的频率(秒)。How often (in seconds) to perform the liveness probe. 默认值为 10 秒。Default to 10 seconds. 最小值为 1。Minimum value is 1.
  initialDelaySeconds initial_delay_seconds 启动容器后,启动运行情况探测前的秒数。Number of seconds after the container has started before liveness probes are initiated. 默认值为 310Defaults to 310
  timeoutSeconds timeout_seconds 运行情况探测超时前等待的秒数。默认值为 2 秒。Number of seconds after which the liveness probe times out. Defaults to 2 seconds. 最小值为 1Minimum value is 1
  successThreshold success_threshold 运行情况探测失败后,将其视为成功所需的最小连续成功次数。Minimum consecutive successes for the liveness probe to be considered successful after having failed. 默认值为 1。Defaults to 1. 最小值为 1。Minimum value is 1.
  failureThreshold failure_threshold 当 Pod 启动而运行情况探测失败时,Kubernetes 将尝试 failureThreshold 次才会放弃。When a Pod starts and the liveness probe fails, Kubernetes will try failureThreshold times before giving up. 默认值为 3。Defaults to 3. 最小值为 1。Minimum value is 1.
namespace namespace 将 Web 服务部署到的 Kubernetes 命名空间。The Kubernetes namespace that the webservice is deployed into. 最多 63 个字符,可使用小写字母数字字符(“a”-“z”,“0”-“9”)和连字符(“-”)。Up to 63 lowercase alphanumeric ('a'-'z', '0'-'9') and hyphen ('-') characters. 第一个和最后一个字符不能为连字符。The first and last characters can't be hyphens.

以下 JSON 是用于 CLI 的部署配置示例:The following JSON is an example deployment configuration for use with the CLI:

{
    "computeType": "aks",
    "autoScaler":
    {
        "autoscaleEnabled": true,
        "minReplicas": 1,
        "maxReplicas": 3,
        "refreshPeriodInSeconds": 1,
        "targetUtilization": 70
    },
    "dataCollection":
    {
        "storageEnabled": true
    },
    "authEnabled": true,
    "containerResourceRequirements":
    {
        "cpu": 0.5,
        "memoryInGB": 1.0
    }
}

后续步骤Next steps