教程:在 Jupyter Notebook 中开始使用 Azure 机器学习Tutorial: Get started with Azure Machine Learning in Jupyter Notebooks

在本教程中,你将通过使用托管的基于云的工作站(计算实例)上的 Jupyter Notebook 来完成 Azure 机器学习的入门步骤。In this tutorial, you complete the steps to get started with Azure Machine Learning by using Jupyter Notebooks on a managed cloud-based workstation (compute instance). 本教程是所有其他 Jupyter Notebook 教程的前提。This tutorial is a precursor to all other Jupyter Notebook tutorials.

本教程介绍以下操作:In this tutorial, you:

  • 创建要在其他 Jupyter Notebook 教程中使用的 Azure 机器学习工作区Create an Azure Machine Learning Workspace to use in other Jupyter Notebook tutorials.
  • 将教程笔记本克隆到工作区中的文件夹。Clone the tutorials notebook to your folder in the workspace.
  • 创建一个基于云的计算实例,其中已安装并预配置了 Azure 机器学习 Python SDK。Create a cloud-based compute instance with the Azure Machine Learning Python SDK installed and preconfigured.

如果没有 Azure 订阅,请在开始前创建一个试用帐户。If you don’t have an Azure subscription, create a trial account before you begin. 立即试用试用版 Azure 机器学习Try the trial version of Azure Machine Learning today.

创建工作区Create a workspace

Azure 机器学习工作区是云中的基础资源,用于试验、训练和部署机器学习模型。An Azure Machine Learning workspace is a foundational resource in the cloud that you use to experiment, train, and deploy machine learning models. 它将 Azure 订阅和资源组关联到服务中一个易于使用的对象。It ties your Azure subscription and resource group to an easily consumed object in the service.

如果你已有 Azure 机器学习工作区,请跳转到克隆笔记本文件夹Skip to Clone a notebook folder if you already have an Azure Machine Learning workspace.

可以通过许多方法来创建工作区There are many ways to create a workspace. 本教程将通过 Azure 门户创建工作区,该门户是用于管理 Azure 资源的基于 Web 的控制台。In this tutorial, you create a workspace via the Azure portal, a web-based console for managing your Azure resources.

  1. 使用 Azure 订阅的凭据登录到 Azure 门户Sign in to Azure portal by using the credentials for your Azure subscription.

  2. 在 Azure 门户的左上角,选择“+ 创建资源” 。In the upper-left corner of Azure portal, select + Create a resource.

    创建新资源

  3. 使用搜索栏查找“机器学习” 。Use the search bar to find Machine Learning.

  4. 选择“机器学习” 。Select Machine Learning.

  5. 在“机器学习”窗格中,选择“创建”以开始 。In the Machine Learning pane, select Create to begin.

  6. 提供以下信息来配置新工作区:Provide the following information to configure your new workspace:

    字段Field 说明Description
    工作区名称Workspace name 输入用于标识工作区的唯一名称。Enter a unique name that identifies your workspace. 本示例使用 docs-ws 。In this example, we use docs-ws. 名称在整个资源组中必须唯一。Names must be unique across the resource group. 使用易于记忆且区别于其他人所创建工作区的名称。Use a name that's easy to recall and to differentiate from workspaces created by others.
    订阅Subscription 选择要使用的 Azure 订阅。Select the Azure subscription that you want to use.
    资源组Resource group 使用订阅中的现有资源组,或者输入一个名称以创建新的资源组。Use an existing resource group in your subscription or enter a name to create a new resource group. 资源组保存 Azure 解决方案的相关资源。A resource group holds related resources for an Azure solution. 本示例使用 docs-aml 。In this example, we use docs-aml.
    位置Location 选择离你的用户和数据资源最近的位置来创建工作区。Select the location closest to your users and the data resources to create your workspace.
    工作区版本Workspace edition 选择“基本” 作为本教程的工作区类型。Select Basic as the workspace type for this tutorial. 工作区类型(基本和企业)确定要访问的功能和定价。The workspace type (Basic & Enterprise) determines the features to which you’ll have access and pricing. 本教程中的所有内容均可使用基本或企业工作区来执行。Everything in this tutorial can be performed with either a Basic or Enterprise workspace.
  7. 完成工作区配置后,选择“查看 + 创建” 。After you are finished configuring the workspace, select Review + Create.

    警告

    在云中创建工作区可能需要几分钟时间。It can take several minutes to create your workspace in the cloud.

    完成创建后,会显示部署成功消息。When the process is finished, a deployment success message appears.

  8. 若要查看新工作区,请选择“转到资源” 。To view the new workspace, select Go to resource.

重要

记下你的工作区和订阅 。Take note of your workspace and subscription. 你将需要此信息以确保在正确的位置创建试验。You'll need this information to ensure you create your experiment in the right place.

在工作区中运行笔记本Run a notebook in your workspace

Azure 机器学习在你的工作区中提供了一个云笔记本服务器,实现了免安装的预配置体验。Azure Machine Learning includes a cloud notebook server in your workspace for an install-free and preconfigured experience. 如果你希望控制环境、包和依赖项,请使用自己的环境Use your own environment if you prefer to have control over your environment, packages, and dependencies.

克隆笔记本文件夹Clone a notebook folder

在 Azure 机器学习工作室中完成以下试验设置并运行步骤。You complete the following experiment setup and run steps in Azure Machine Learning studio. 此合并接口包括机器学习工具,所有技能级别的数据科学专业人员均可利用这些工具实现数据科学方案。This consolidated interface includes machine learning tools to perform data science scenarios for data science practitioners of all skill levels.

  1. 登录到 Azure 机器学习工作室Sign in to Azure Machine Learning studio.

  2. 选择创建的订阅和工作区。Select your subscription and the workspace you created.

  3. 在左侧选择“笔记本”。On the left, select Notebooks.

  4. 在顶部选择“示例”选项卡。At the top, select the Samples tab.

  5. 打开“Python”文件夹。Open the Python folder.

  6. 打开包含版本号的文件夹。Open the folder with a version number on it. 此数字表示 Python SDK 的当前版本。This number represents the current release for the Python SDK.

  7. 选择“教程”文件夹右侧的“…”按钮,然后选择“克隆” 。Select the ... button at the right of the tutorials folder, and then select Clone.

    显示“克隆”教程文件夹的屏幕截图。

  8. 文件夹列表显示访问该工作区的每个用户。A list of folders shows each user who accesses the workspace. 选择要将“tutorials”文件夹克隆到其中的文件夹。Select your folder to clone the tutorials folder there.

打开克隆的笔记本Open the cloned notebook

  1. 打开“用户文件”部分中关闭的“教程”文件夹 。Open the tutorials folder that was closed into your User files section.

    重要

    可以查看“示例”文件夹中的笔记本,但无法从此文件夹运行笔记本。You can view notebooks in the samples folder but you can't run a notebook from there. 若要运行笔记本,请确保在“用户文件”部分打开笔记本的克隆版本。To run a notebook, make sure you open the cloned version of the notebook in the User Files section.

  2. 选择 tutorials/image-classification-mnist-data 文件夹中的 img-classification-part1-training.ipynb 文件。 Select the img-classification-part1-training.ipynb file in your tutorials/image-classification-mnist-data folder.

    显示“打开”教程文件夹的屏幕截图。

  3. 在顶部栏上,选择用来运行笔记本的计算实例。On the top bar, select a compute instance to use to run the notebook. 这些虚拟机 (VM) 中已预先配置了运行 Azure 机器学习所需的一切设置These virtual machines (VMs) are preconfigured with everything you need to run Azure Machine Learning.

  4. 如果未找到任何 VM,请选择“+ 添加”来创建计算实例 VM。If no VMs are found, select + Add to create the compute instance VM.

    1. 创建 VM 时,请遵循以下规则:When you create a VM, follow these rules:

      • 名称是必填项,该字段不能为空。A name is required, and the field can't be empty.
      • 此名称在工作区或计算实例的 Azure 区域中的所有现有计算实例中必须唯一(不区分大小写)。The name must be unique (in a case-insensitive fashion) across all existing compute instances in the Azure region of the workspace or compute instance. 如果选择的名称不是唯一的,会收到警报。You'll get an alert if the name you choose isn't unique.
      • 有效字符包括大小写字母、数字 0 到 9 和短划线字符 (-)。Valid characters are uppercase and lowercase letters, numbers 0 to 9, and the dash character (-).
      • 名称的长度必须介于 3 到 24 个字符之间。The name must be between 3 and 24 characters long.
      • 名称应以字母(而非数字或短划线字符)开头。The name should start with a letter, not a number or a dash character.
      • 如果使用短划线字符,短划线后需要跟至少一个字母。If a dash character is used, it must be followed by at least one letter after the dash. 例如,“Test-”、“test-0”、“test-01”无效,而“test-a0”、“test-0a”为有效实例。For example, Test-, test-0, test-01 are invalid, while test-a0, test-0a are valid instances.
    2. 从可用选项中选择 VM 大小。Select the VM size from the available choices. 对于本教程,默认 VM 是不错的选择。For the tutorials, the default VM is a good choice.

    3. 然后选择“创建”。Then select Create. 设置 VM 可能需要大约五分钟时间。It can take approximately five minutes to set up your VM.

  5. 当 VM 可用时,它将显示在顶部工具栏中。When the VM is available, it appears in the top toolbar. 现在,可以使用工具栏中的“全部运行”,或者在笔记本的代码单元中按 Shift+Enter 来运行笔记本 。You can now run the notebook by using either Run all in the toolbar or Shift+Enter in the code cells of the notebook.

如果你有自定义小组件或喜欢使用 Jupyter 或 JupyterLab,请在最右侧选择“Jupyter”下拉列表。If you have custom widgets or prefer to use Jupyter or JupyterLab, select the Jupyter drop-down list on the far right. 然后选择“Jupyter”或“JupyterLab” 。Then select Jupyter or JupyterLab. 此时将打开一个新的浏览器窗口。The new browser window opens.

后续步骤Next steps

现在,你已设置了一个开发环境,请继续在 Jupyter Notebook 中训练模型。Now that you have a development environment set up, continue on to train a model in a Jupyter Notebook.

如果你现在不打算继续学习任何其他教程,请在不使用云笔记本服务器 VM 时停止它,以降低成本。If you don't plan on following any other tutorials now, stop the cloud notebook server VM when you aren't using it to reduce cost.

如果使用了计算实例或笔记本 VM,请停止未使用的 VM,以降低成本。If you used a compute instance or Notebook VM, stop the VM when you are not using it to reduce cost.

  1. 在工作区中选择“计算”。 In your workspace, select Compute.

  2. 从列表中选择 VM。From the list, select the VM.

  3. 选择“停止” 。Select Stop.

  4. 准备好再次使用服务器时,选择“启动” 。When you're ready to use the server again, select Start.