教程:运行“Hello world!”Tutorial: Run a "Hello world!" Python 脚本(第 2 部分,共 4 部分)Python script (part 2 of 4)

在本教程中,你将了解如何使用适用于 Python 的 Azure 机器学习 SDK 提交并运行 Python“Hello world!”In this tutorial, you learn how to use the Azure Machine Learning SDK for Python to submit and run a Python "Hello world!" 脚本。script.

本教程是由四部分组成的系列教程的第 2 部分,你可以在其中了解 Azure 机器学习基础知识,并在 Azure 中完成基于作业的机器学习任务。This tutorial is part 2 of a four-part tutorial series in which you learn the fundamentals of Azure Machine Learning and complete jobs-based machine learning tasks in Azure. 本教程的基础是你在第 1 部分:为 Azure 机器学习设置本地计算机中完成的工作。This tutorial builds on the work you completed in Part 1: Set up your local machine for Azure Machine Learning.

在本教程中,将:In this tutorial, you will:

  • 在本地创建并运行“Hello world!”Create and run a "Hello world!" Python 脚本。Python script locally.
  • 创建一个 Python 控制脚本,以将“Hello world!”提交Create a Python control script to submit "Hello world!" 到 Azure 机器学习。to Azure Machine Learning.
  • 了解控制脚本中的 Azure 机器学习概念。Understand the Azure Machine Learning concepts in the control script.
  • 提交并运行“Hello world!”Submit and run the "Hello world!" 脚本。script.
  • 在云中查看代码输出。View your code output in the cloud.

先决条件Prerequisites

  • 如果你还没有 Azure 机器学习工作区,请先完成第 1 部分Completion of part 1 if you don't already have an Azure Machine Learning workspace.

在本地创建并运行一个 Python 脚本Create and run a Python script locally

tutorial 目录下创建一个新的名为 src 的子目录,用于存储要在 Azure 机器学习计算群集上运行的代码。Create a new subdirectory called src under the tutorial directory to store code that you want to run on an Azure Machine Learning compute cluster. src 子目录中,创建 hello.py Python 脚本:In the src subdirectory, create the hello.py Python script:

# src/hello.py
print("Hello world!")

你的项目目录结构现在将如下所示:Your project directory structure will now look like:

目录结构显示 src 子目录中的 hello.py

在本地测试你的脚本Test your script locally

你可以通过使用你常用的 IDE 或终端在本地运行代码。You can run your code locally, by using your favorite IDE or a terminal. 在本地运行代码具有可对代码进行交互式调试的好处。Running code locally has the benefit of interactive debugging of code. 在已激活 tutorial1 conda 环境的窗口中,运行 Python 文件:In the window that has the activated tutorial1 conda environment, run the Python file:

cd <path/to/tutorial>
python ./src/hello.py

创建控制脚本Create a control script

使用控制脚本,你可以在云中运行 hello.py 脚本。A control script allows you to run your hello.py script in the cloud. 可以使用控制脚本来控制如何运行以及在何处运行机器学习代码。You use the control script to control how and where your machine learning code is run.

在你的教程目录中,创建名为 03-run-hello.py 的新 Python 文件,然后将以下代码复制并粘贴到该文件中:In your tutorial directory, create a new Python file called 03-run-hello.py and copy/paste the following code into that file:

# tutorial/03-run-hello.py
from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig

ws = Workspace.from_config()
experiment = Experiment(workspace=ws, name='day1-experiment-hello')

config = ScriptRunConfig(source_directory='./src', script='hello.py', compute_target='cpu-cluster')

run = experiment.submit(config)
aml_url = run.get_portal_url()
print(aml_url)

了解代码Understand the code

下面是有关控制脚本工作方式的说明:Here's a description of how the control script works:

ws = Workspace.from_config()

工作区连接到你的 Azure 机器学习工作区,以便你可以与 Azure 机器学习资源通信。Workspace connects to your Azure Machine Learning workspace, so that you can communicate with your Azure Machine Learning resources.

experiment = Experiment( ... )

可以通过试验轻松地将多个运行组织到单个名称下。Experiment provides a simple way to organize multiple runs under a single name. 稍后,你可以了解如何使用试验轻松地在数十个运行之间比较指标。Later you can see how experiments make it easy to compare metrics between dozens of runs.

config = ScriptRunConfig( ... )

ScriptRunConfig 包装你的 hello.py 代码并将其传递到你的工作区。ScriptRunConfig wraps your hello.py code and passes it to your workspace. 顾名思义,你可以使用此类来配置你希望脚本如何在 Azure 机器学习中运行。 As the name suggests, you can use this class to configure how you want your script to run in Azure Machine Learning. 此类还会指定该脚本将会在哪个计算目标上运行。It also specifies what compute target the script will run on. 在此代码中,目标是在设置教程中创建的计算群集。In this code, the target is the compute cluster that you created in the setup tutorial.

run = experiment.submit(config)

提交脚本。Submits your script. 此提交被称为运行This submission is called a run. 运行会封装你的代码的单次执行。A run encapsulates a single execution of your code. 可以使用运行来监视脚本进度、捕获输出、分析结果、将指标可视化,等等。Use a run to monitor the script progress, capture the output, analyze the results, visualize metrics, and more.

aml_url = run.get_portal_url()

run 对象提供了一个便于执行你的代码的句柄。The run object provides a handle on the execution of your code. 可以使用从 Python 脚本输出的 URL 从 Azure 机器学习工作室中监视代码执行进度。Monitor its progress from the Azure Machine Learning studio with the URL that's printed from the Python script.

在云中提交并运行代码Submit and run your code in the cloud

运行你的控制脚本,该脚本继而会在设置教程中创建的计算群集上运行 hello.pyRun your control script, which in turn runs hello.py on the compute cluster that you created in the setup tutorial.

python 03-run-hello.py

提示

如果运行此代码返回错误“你没有访问订阅的权限”,请参阅连接到工作区,以获取有关身份验证选项的信息。If running this code gives you an error that you do not have access to the subscription, see Connect to a workspace for information on authentication options.

通过使用工作室在云中监视代码Monitor your code in the cloud by using the studio

来自脚本的输出中将包含一个指向工作室的链接,该链接类似于:https://studio.ml.azure.cn/experiments/hello-world/runs/<run-id>?wsid=/subscriptions/<subscription-id>/resourcegroups/<resource-group>/workspaces/<workspace-name>The output from your script will contain a link to the studio that looks something like this: https://studio.ml.azure.cn/experiments/hello-world/runs/<run-id>?wsid=/subscriptions/<subscription-id>/resourcegroups/<resource-group>/workspaces/<workspace-name>.

访问该链接。Follow the link. 首先,你会看到状态为“正在准备”。At first, you'll see a status of Preparing. 首次运行将需要 5 - 10 分钟才能完成。The very first run will take 5-10 minutes to complete. 这是因为发生了以下情况:This is because the following occurs:

  • docker 映像在云中构建A docker image is built in the cloud
  • 计算群集的大小从 0 个节点调整到 1 个节点The compute cluster is resized from 0 to 1 node
  • docker 映像被下载到该计算。The docker image is downloaded to the compute.

由于 docker 映像已缓存在计算中,因此后续运行要快得多(约 15 秒)。Subsequent runs are much quicker (~15 seconds) as the docker image is cached on the compute. 可以通过在第一次运行完成后重新提交以下代码来进行测试。You can test this by resubmitting the code below after the first run has completed.

作业完成后,转到“输出 + 日志”选项卡。在那里可以看到 70_driver_log.txt 文件,该文件的内容类似于:Once the job completes, go to the Outputs + logs tab. There you can see a 70_driver_log.txt file that looks like this:

 1: [2020-08-04T22:15:44.407305] Entering context manager injector.
 2: [context_manager_injector.py] Command line Options: Namespace(inject=['ProjectPythonPath:context_managers.ProjectPythonPath', 'RunHistory:context_managers.RunHistory', 'TrackUserError:context_managers.TrackUserError', 'UserExceptions:context_managers.UserExceptions'], invocation=['hello.py'])
 3: Starting the daemon thread to refresh tokens in background for process with pid = 31263
 4: Entering Run History Context Manager.
 5: Preparing to call script [ hello.py ] with arguments: []
 6: After variable expansion, calling script [ hello.py ] with arguments: []
 7:
 8: Hello world!
 9: Starting the daemon thread to refresh tokens in background for process with pid = 31263
10:
11:
12: The experiment completed successfully. Finalizing run...
13: Logging experiment finalizing status in history service.
14: [2020-08-04T22:15:46.541334] TimeoutHandler __init__
15: [2020-08-04T22:15:46.541396] TimeoutHandler __enter__
16: Cleaning up all outstanding Run operations, waiting 300.0 seconds
17: 1 items cleaning up...
18: Cleanup took 0.1812913417816162 seconds
19: [2020-08-04T22:15:47.040203] TimeoutHandler __exit__

在第 8 行,你会看到“Hello world!”On line 8, you see the "Hello world!" 输出。output.

70_driver_log.txt 文件包含来自运行的标准输出。The 70_driver_log.txt file contains the standard output from a run. 当你在云中调试远程运行时,此文件会很有用。This file can be useful when you're debugging remote runs in the cloud.

后续步骤Next steps

在本教程中,你使用了一个简单的“hello world!”In this tutorial, you took a simple "Hello world!" 脚本,并在 Azure 上运行了该脚本。script and ran it on Azure. 你了解了如何连接到 Azure 机器学习工作区、如何创建试验,以及如何将 hello.py 代码提交到云中。You saw how to connect to your Azure Machine Learning workspace, create an experiment, and submit your hello.py code to the cloud.

在接下来的教程中,你将在这些知识的基础上通过运行比 print("Hello world!") 更有趣的脚本来进一步学习。In the next tutorial, you build on these learnings by running something more interesting than print("Hello world!").

备注

如果你想就此完成本教程系列,不再继续进行下一步,请记得清理你的资源If you want to finish the tutorial series here and not progress to the next step, remember to clean up your resources.