设置 Azure 机器学习 Visual Studio Code 扩展Set up Azure Machine Learning Visual Studio Code extension

了解如何使用 Azure 机器学习 Visual Studio Code 扩展安装和运行脚本。Learn how to install and run scripts using the Azure Machine Learning Visual Studio Code extension.

本教程将介绍以下任务:In this tutorial, you learn the following tasks:

  • 安装 Azure 机器学习 Visual Studio Code 扩展Install the Azure Machine Learning Visual Studio Code extension
  • 从 Visual Studio Code 登录到 Azure 帐户Sign into your Azure account from Visual Studio Code
  • 使用 Azure 机器学习扩展来运行示例脚本Use the Azure Machine Learning extension to run a sample script

先决条件Prerequisites

安装扩展Install the extension

  1. 打开 Visual Studio Code。Open Visual Studio Code.

  2. 活动栏选择“扩展” 图标,打开“扩展”视图。Select Extensions icon from the Activity Bar to open the Extensions view.

  3. 在“扩展”视图中,搜索“Azure 机器学习”。In the Extensions view, search for "Azure Machine Learning".

  4. 选择“安装” 。Select Install.

    安装 Azure 机器学习 VS Code 扩展Install Azure Machine Learning VS Code Extension

备注

也可直接下载安装程序,以便通过 Visual Studio Marketplace 安装 Azure 机器学习扩展。Alternatively, you can install the Azure Machine Learning extension via the Visual Studio Marketplace by downloading the installer directly.

本教程中的其余步骤已使用扩展的版本 0.6.8 进行了测试。The rest of the steps in this tutorial have been tested with version 0.6.8 of the extension.

登录 Azure 帐户Sign in to your Azure Account

若要在 Azure 上预配资源并运行工作负载,必须使用 Azure 帐户凭据登录。In order to provision resources and run workloads on Azure, you have to sign in with your Azure account credentials. Azure 机器学习会自动安装 Azure 帐户扩展,帮助你进行帐户管理。To assist with account management, Azure Machine Learning automatically installs the Azure Account extension. 请访问以下站点,详细了解 Azure 帐户扩展Visit the following site to learn more about the Azure Account extension.

  1. 从菜单栏选择“视图”>“命令面板”,打开命令面板。 Open the command palette by selecting View > Command Palette from the menu bar.
  2. 将命令“Azure:登录”输入命令面板,启动登录过程。Enter the command "Azure: Sign In" into the command palette to start the sign in process.

在 Azure 中运行机器学习模型训练脚本Run a machine learning model training script in Azure

使用帐户凭据登录到 Azure 后,即可执行此部分中的步骤,了解如何使用该扩展训练机器学习模型。Now that you have signed into Azure with your account credentials, Use the steps in this section to learn how to use the extension to train a machine learning model.

  1. 在计算机上的任何位置下载并解压缩 VS Code Tools for AI 存储库Download and unzip the VS Code Tools for AI repository anywhere on your computer.

  2. 在 Visual Studio Code 中打开 mnist-vscode-docs-sample 目录。Open the mnist-vscode-docs-sample directory in Visual Studio Code.

  3. 在活动栏中选择“Azure”图标。 Select the Azure icon in the Activity Bar.

  4. 选择“Azure 机器学习”视图顶部的“运行试验” 图标。Select the Run Experiment icon at the top of the Azure Machine Learning View.

    运行试验Run Experiment

  5. 命令面板展开后,请按提示操作。When the command palette expands, follow the prompts.

    备注

    如果已预配现有的 Azure 机器学习资源,请参阅“如何在 VS Code 中运行试验”指南If you already have existing Azure Machine Learning resources provisioned, see how to run experiments in VS Code guide.

    1. 选择 Azure 订阅。Select your Azure subscription.
    2. 从环境列表中选择“Conda 依赖项文件”。From the list of environments, select Conda dependencies file.
    3. 按“Enter”以浏览 Conda 依赖项文件。Press Enter to browse the Conda dependencies file. 此文件包含运行脚本所需的依赖项。This file contains the dependencies required to run your script. 在本例中,依赖项文件是 mnist-vscode-docs-sample 目录中的 env.yml 文件。In this case, the dependencies file is the env.yml file inside the mnist-vscode-docs-sample directory.
    4. 按“Enter”以浏览训练脚本文件。Press Enter to browse the training script file. 这是一个文件,其中包含机器学习模型的代码,用于对手写数字的图像分类。This is the file that contains code to a machine learning model that categorize images of handwritten digits. 在此示例中,用于训练模型的脚本是 mnist-vscode-docs-sample 目录内的 train.py 文件。In this case, the script to train the model is the train.py file inside the mnist-vscode-docs-sample directory.
  6. 此时会在文本编辑器中显示如下所示的配置文件。At this point, a configuration file similar to the one below appears in the text editor. 此配置包含运行训练作业所需的信息,例如,包含训练模型所需的代码以及在上一步指定的任何 Python 依赖项的文件。The configuration contains the information required to run the training job like the file that contains the code to train the model and any Python dependencies specified in the previous step.

    {
        "workspace": "WS06271500",
        "resourceGroup": "WS06271500-rg2",
        "location": "China East",
        "experiment": "WS06271500-exp1",
        "compute": {
            "name": "WS06271500-com1",
            "vmSize": "Standard_D1_v2, Cores: 1; RAM: 3.5GB;"
        },
        "runConfiguration": {
            "filename": "WS06271500-com1-rc1",
            "environment": {
                "name": "WS06271500-env1",
                "conda_dependencies": [
                    "python=3.6.2",
                    "tensorflow=1.15.0",
                    "pip"
                ],
                "pip_dependencies": [
                    "azureml-defaults"
                ],
                "environment_variables": {}
            }
        }
    }
    
  7. 对配置满意以后,即可提交试验,方法是:打开命令面板并输入以下命令:Once you're satisfied with your configuration, submit your experiment by opening the command palette and entering the following command:

    Azure ML: Submit Experiment
    

    这样就会将 train.py 和配置文件发送到 Azure 机器学习工作区。This sends the train.py and configuration file to your Azure Machine Learning workspace. 然后就会在 Azure 中的计算资源上启动训练作业。The training job is then started on a compute resource in Azure.

跟踪训练脚本的进度Track the progress of the training script

运行脚本可能需要几分钟时间。Running your script can take several minutes. 若要跟踪其进度,请执行以下操作:To track its progress:

  1. 在活动栏中选择“Azure”图标。Select the Azure icon from the activity bar.

  2. 展开订阅节点。Expand your subscription node.

  3. 展开当前正在运行的试验的节点。Expand your currently running experiment's node. 它位于 {workspace}/Experiments/{experiment} 节点中,其中的工作区和试验的值与配置文件中定义的属性相同。This is located inside the {workspace}/Experiments/{experiment} node where the values for your workspace and experiment are the same as the properties defined in the configuration file.

  4. 将列出试验的所有运行及其状态。All of the runs for the experiment are listed, as well as their status. 若要获取最新状态,请单击“Azure 机器学习”视图顶部的刷新图标。To get the most recent status, click the refresh icon at the top of the Azure Machine Learning View.

    跟踪试验进度Track Experiment Progress

下载训练的模型Download the trained model

试验运行完成后,输出是训练的模型。When the experiment run is complete, the output is a trained model. 若要在本地下载输出,请执行以下操作:To download the outputs locally:

  1. 右键单击最近的运行,然后选择“下载输出”。Right-click the most recent run and select Download Outputs.

    下载训练的模型Download Trained Model

  2. 选择要将输出保存到其中的位置。Select a location where to save the outputs to.

  3. 带有运行名称的文件夹下载到本地。A folder with the name of your run is downloaded locally. 导航到此页。Navigate to it.

  4. 模型文件位于 outputs/outputs/model 目录中。The model files are inside the outputs/outputs/model directory.

后续步骤Next steps