如何在工作区中运行 Jupyter 笔记本How to run Jupyter Notebooks in your workspace

了解如何直接在 Azure 机器学习工作室中的工作区中运行 Jupyter 笔记本。Learn how to run your Jupyter Notebooks directly in your workspace in Azure Machine Learning studio. 用户不但可以在工作区中启动 JupyterJupyterLab,还可以直接编辑和运行笔记本。While you can launch Jupyter or JupyterLab, you can also edit and run your notebooks without leaving the workspace.

本文介绍如何执行以下操作:See how you can:

  • 在工作区中创建 Jupyter 笔记本Create Jupyter Notebooks in your workspace
  • 在笔记本中运行试验Run an experiment from a notebook
  • 更改笔记本环境Change the notebook environment
  • 查找用于运行笔记本的计算实例的详细信息Find details of the compute instances used to run your notebooks

先决条件Prerequisites

创建笔记本Create notebooks

在 Azure 机器学习工作区中,创建一个新的 Jupyter 笔记本并开始运行。In your Azure Machine Learning workspace, create a new Jupyter notebook and start working. 新创建的笔记本存储在默认的工作区存储中。The newly created notebook is stored in the default workspace storage. 有权访问此工作区的任何人都可以共享此笔记本。This notebook can be shared with anyone with access to the workspace.

创建新笔记本的步骤:To create a new notebook:

  1. Azure 机器学习工作室中打开工作区。Open your workspace in Azure Machine Learning studio.

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

  3. 在“我的文件”部分的“用户文件”列表上方,选择“新建文件”图标。Select the Create new file icon above the list User files in the My files section.

    新建文件

  4. 为文件命名。Name the file.

  5. 对于 Jupyter Notebook 文件,请选择“Notebook”作为文件类型。For Jupyter Notebook Files, select Notebook as the file type.

  6. 选择文件目录。Select a file directory.

  7. 选择“创建” 。Select Create.

提示

也可以创建文本文件。You can create text files as well. 选择“文本”作为文件类型,并向名称添加扩展名(例如 myfile.py 或 myfile.txt)Select Text as the file type and add the extension to the name (for example, myfile.py or myfile.txt)

另外,还可以通过“笔记本”页顶部的工具上传文件夹和文件,包括笔记本。You can also upload folders and files, including notebooks, with the tools at the top of the Notebooks page. 笔记本和大多数文本文件类型可在“预览”部分显示。Notebooks and most text file types display in the preview section. 其他大多数文件类型没有预览功能。No preview is available for most other file types.

重要

笔记本和脚本中的内容可能会从会话中读取数据,并在组织不在 Azure 中的情况下访问数据。Content in notebooks and scripts can potentially read data from your sessions and access data without your organization in Azure. 仅从受信任的源加载文件。Only load files from trusted sources. 有关详细信息,请参阅安全代码最佳做法For more information, see Secure code best practices.

克隆示例Clone samples

你的工作区包含一个“示例”文件夹,其中的笔记本旨在帮助你探索 SDK,并用作你自己的机器学习项目的示例。Your workspace contains a Samples folder with notebooks designed to help you explore the SDK and serve as examples for your own machine learning projects. 可以将这些笔记本克隆到你自己工作区存储容器上的文件夹中。You can clone these notebooks into your own folder on your workspace storage container.

有关示例,请参阅教程:创建第一个 ML 试验For an example, see Tutorial: Create your first ML experiment.

使用 Git 中的文件并对我的文件执行版本控制Use files from Git and version my files

可以使用终端窗口访问所有 Git 操作。You can access all Git operations by using a terminal window. 所有 Git 文件和文件夹都将存储在你的工作区文件系统中。All Git files and folders will be stored in your workspace file system.

备注

将文件和文件夹添加到 ~/cloudfiles/code/Users 文件夹下的任意位置,使其在所有 Jupyter 环境中都可见。Add your files and folders anywhere under the ~/cloudfiles/code/Users folder so they will be visible in all your Jupyter environments.

访问终端的步骤:To access the terminal:

  1. Azure 机器学习工作室中打开工作区。Open your workspace in Azure Machine Learning studio.

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

  3. 选择左侧“用户文件”部分中的任何笔记本。Select any notebook located in the User files section on the left-hand side. 如果没有笔记本,请先创建笔记本If you don't have any notebooks there, first create a notebook

  4. 选择“计算”目标或新建一个目标,然后等待目标运行。Select a Compute target or create a new one and wait until it's running.

  5. 选择“打开终端”图标。Select the Open terminal icon.

    打开终端

  6. 如果看不到该图标,请选择计算目标右侧的“...”,然后选择“打开终端”。If you don't see the icon, select the ... to the right of the compute target and then select Open terminal .

    从“...”打开终端

详细了解如何将 Git 存储库克隆到工作区文件系统Learn more about cloning Git repositories into your workspace file system.

在终端中复制和粘贴Copy and Paste in Terminal

  • Windows:使用 Ctrl-Insert 复制,使用 Ctrl-Shift-vShift-Insert 粘贴。Windows: Ctrl-Insert to copy and use Ctrl-Shift-v or Shift-Insert to paste.
  • Mac OS:使用 Cmd-c 复制,使用 Cmd-v 粘贴。Mac OS: Cmd-c to copy and Cmd-v to paste.
  • FireFox/IE 可能不会正确支持剪贴板权限。FireFox/IE may not support clipboard permissions properly.

共享笔记本和其他文件Share notebooks and other files

复制并粘贴 URL 可以共享笔记本或文件。Copy and paste the URL to share a notebook or file. 只有工作区的其他用户才能访问此 URL。Only other users of the workspace can access this URL. 详细了解如何授权访问工作区Learn more about granting access to your workspace.

编辑笔记本Edit a notebook

若要编辑笔记本,请打开位于工作区“用户文件”部分的任何笔记本。To edit a notebook, open any notebook located in the User files section of your workspace. 单击要编辑的单元格。Click on the cell you wish to edit.

你可以在不连接到计算实例的情况下编辑笔记本。You can edit the notebook without connecting to a compute instance. 要运行笔记本中的单元格时,请选择或创建一个计算实例。When you want to run the cells in the notebook, select or create a compute instance. 如果选择一个已停止的计算实例,则在你运行第一个单元时,该计算实例会自动启动。If you select a stopped compute instance, it will automatically start when you run the first cell.

如果计算实例正在运行,还可以在任何 Python Notebook 中使用由 Intellisense 提供支持的代码完成功能。When a compute instance is running, you can also use code completion, powered by Intellisense, in any Python Notebook.

另外,还可以从笔记本工具栏中启动 Jupyter 或 JupyterLab。You can also launch Jupyter or JupyterLab from the Notebook toolbar. Azure 机器学习不提供来自 Jupyter 或 JupyterLab 的更新,也不修复其中的 bug,因为这些开源产品不在 Microsoft 支持的范围内。Azure Machine Learning does not provide updates and fix bugs from Jupyter or JupyterLab as they are Open Source products outside of the boundary of Microsoft Support.

焦点模式Focus mode

使用焦点模式展开当前视图,以便将焦点放在活动的选项卡上。Use focus mode to expand your current view so you can focus on your active tabs. 焦点模式将隐藏笔记本文件资源管理器。Focus mode hides the Notebooks file explorer.

  1. 在终端窗口工具栏中,选择“焦点模式”以打开焦点模式。In the terminal window toolbar, select Focus mode to turn on focus mode. 根据窗口宽度的不同,它可能位于工具栏的“…”菜单项下。Depending on your window width, this may be located under the ... menu item in your toolbar.

  2. 在焦点模式下,通过选择“标准视图”返回到标准视图。While in focus mode, return to the standard view by selecting Standard view.

    切换焦点模式/标准视图

使用 IntelliSenseUse IntelliSense

IntelliSense 是一个代码完成辅助插件,其中包括许多功能:“列出成员”、“参数信息”、“快速信息”和“完成单词”。IntelliSense is a code-completion aid that includes a number of features: List Members, Parameter Info, Quick Info, and Complete Word. 这些功能可帮助你详细了解所使用的代码、跟踪正在键入的参数,只需几次击键即可添加对属性和方法的调用。These features help you to learn more about the code you're using, keep track of the parameters you're typing, and add calls to properties and methods with only a few keystrokes.

键入代码时,请使用“Ctrl+空格键”来触发 IntelliSense。When typing code, use Ctrl+Space to trigger IntelliSense.

清理笔记本(预览版)Clean your notebook (preview)

重要

收集功能目前为公共预览版。The gather feature is currently in public preview. 该预览版在提供时没有附带服务级别协议,建议不要将其用于生产工作负载。The preview version is provided without a service level agreement, and it's not recommended for production workloads. 某些功能可能不受支持或者受限。Certain features might not be supported or might have constrained capabilities.

在创建笔记本的过程中,通常最终会得到用于数据浏览或调试的单元格。Over the course of creating a notebook, you typically end up with cells you used for data exploration or debugging. “收集”功能会帮助你生成一个没有这些无关单元格的纯净笔记本。The gather feature will help you produce a clean notebook without these extraneous cells.

  1. 运行所有笔记本单元格。Run all of your notebook cells.
  2. 选择特定的单元格,其中包含的代码是你希望新笔记本运行的。Select the cell containing the code you wish the new notebook to run. 例如,用于提交试验的代码,或者用于注册模型的代码。For example, the code that submits an experiment, or perhaps the code that registers a model.
  3. 选择单元格工具栏上出现的“收集”图标。Select the Gather icon that appears on the cell toolbar. 屏幕截图:选择“收集”图标
  4. 输入新的“已收集”笔记本的名称。Enter the name for your new "gathered" notebook.

新笔记本只包含代码单元格,所有单元格都需要生成与你选择用于收集的单元格相同的结果。The new notebook contains only code cells, with all cells required to produce the same results as the cell you selected for gathering.

将笔记本保存并设置检查点Save and checkpoint a notebook

当你创建一个 ipynb 文件时,Azure 机器学习会创建一个检查点文件。Azure Machine Learning creates a checkpoint file when you create an ipynb file.

在笔记本工具栏中,选择“菜单”,然后选择“文件”>“保存并设置检查点”以手动保存笔记本,它会添加与笔记本关联的检查点文件。In the notebook toolbar, select the menu and then File>Save and checkpoint to manually save the notebook and it will add a checkpoint file associated with the notebook.

笔记本工具栏中保存工具的屏幕截图

每个笔记本每隔 30 秒会自动保存一次。Every notebook is autosaved every 30 seconds. 自动保存仅更新初始 ipynb 文件,而不更新检查点文件。Autosave updates only the initial ipynb file, not the checkpoint file.

在“笔记本”菜单中选择“检查点”以创建命名检查点,并将笔记本还原为已保存的检查点。Select Checkpoints in the notebook menu to create a named checkpoint and to revert the notebook to a saved checkpoint.

有用的键盘快捷方式Useful keyboard shortcuts

键盘Keyboard 操作Action
Shift+EnterShift+Enter 运行单元格Run a cell
Ctrl+空格键Ctrl+Space 激活 IntelliSenseActivate IntelliSense
Ctrl+M(Windows)Ctrl+M(Windows) 启用/禁用笔记本中的 Tab 键捕获。Enable/disable tab trapping in notebook.
Ctrl+Shift+M(Mac 和 Linux)Ctrl+Shift+M(Mac & Linux) 启用/禁用笔记本中的 Tab 键捕获。Enable/disable tab trapping in notebook.
Tab(Tab 键捕获处于启用状态)Tab (when tab trap enabled) 添加“\t”字符(缩进)Add a '\t' character (indent)
Tab(Tab 键捕获处于禁用状态)Tab (when tab trap disabled) 将焦点更改为下一个可聚焦项(“删除单元格”按钮、“运行”按钮等)Change focus to next focusable item (delete cell button, run button, etc.)

删除笔记本Delete a notebook

不能删除“示例”笔记本。You can't delete the Samples notebooks. 这些笔记本是工作室的一部分,在每次发布新的 SDK 时,它们都会相应地获得更新。These notebooks are part of the studio and are updated each time a new SDK is published.

可以通过以下任一方式删除“用户文件”笔记本:You can delete User files notebooks in any of these ways:

  • 在工作室中,选择文件夹或文件末尾的“...”。In the studio, select the ... at the end of a folder or file. 请确保使用支持的浏览器(Microsoft Edge、Chrome 或 Firefox)。Make sure to use a supported browser (Microsoft Edge, Chrome, or Firefox).
  • 在任何笔记本工具栏中,选择”打开终端”,访问计算实例的终端窗口。From any Notebook toolbar, select Open terminal to access the terminal window for the compute instance.
  • 在 Jupyter 或 JupyterLab 中使用自带工具删除。In either Jupyter or JupyterLab with their tools.

运行试验Run an experiment

若要从笔记本运行试验,请先连接到正在运行的计算实例To run an experiment from a Notebook, you first connect to a running compute instance. 如果没有计算实例,请使用以下步骤创建一个计算实例:If you don't have a compute instance, use these steps to create one:

  1. 选择笔记本工具栏中的“+”。Select + in the Notebook toolbar.
  2. 为计算命名,并在“虚拟机大小”中选择一个大小。Name the Compute and choose a Virtual Machine Size.
  3. 选择“创建” 。Select Create.
  4. 计算实例会自动连接到笔记本,现在就可以运行单元格了。The compute instance is connected to the Notebook automatically and you can now run your cells.

只有本人可以查看和使用自己创建的计算实例。Only you can see and use the compute instances you create. 用户文件与 VM 分开存储,并在工作区中的所有计算实例之间共享。Your User files are stored separately from the VM and are shared among all compute instances in the workspace.

查看日志和输出View logs and output

使用笔记本小组件可以查看运行进度和日志。Use Notebook widgets to view the progress of the run and logs. 小组件具有异步性,在训练结束之前,它会一直提供更新。A widget is asynchronous and provides updates until training finishes. Jupyter 和 JupterLab 也支持 Azure 机器学习小组件。Azure Machine Learning widgets are also supported in Jupyter and JupterLab.

更改笔记本环境Change the notebook environment

使用笔记本工具栏可以更改笔记本的运行环境。The Notebook toolbar allows you to change the environment on which your Notebook runs.

以下操作不会更改笔记本状态或笔记本中任何变量的值:These actions will not change the notebook state or the values of any variables in the notebook:

操作Action 结果Result
停止内核Stop the kernel 停止任何正在运行的单元格。Stops any running cell. 运行单元格会自动重新启动内核。Running a cell will automatically restart the kernel.
导航到另一个工作区部分Navigate to another workspace section 停止运行单元格。Running cells are stopped.

以下操作将会重置笔记本状态,并重置笔记本中的所有变量。These actions will reset the notebook state and will reset all variables in the notebook.

操作Action 结果Result
更改内核Change the kernel 笔记本使用新内核Notebook uses new kernel
切换计算Switch compute 笔记本自动使用新的计算。Notebook automatically uses the new compute.
重置计算Reset compute 在你尝试运行单元格时再次启动Starts again when you try to run a cell
停止计算Stop compute 不会运行任何单元格No cells will run
在 Jupyter 或 JupyterLab 中打开笔记本Open notebook in Jupyter or JupyterLab 笔记本在新选项卡中打开。Notebook opened in a new tab.

添加新内核Add new kernels

笔记本会自动查找连接的计算实例上安装的所有 Jupyter 内核。The Notebook will automatically find all Jupyter kernels installed on the connected compute instance. 若要向计算实例添加内核,请执行以下步骤:To add a kernel to the compute instance:

  1. 选择笔记本工具栏中的“打开终端”。Select Open terminal in the Notebook toolbar.

  2. 使用终端窗口创建新环境。Use the terminal window to create a new environment. 例如,以下代码会创建 newenvFor example, the code below creates newenv:

    conda create -y --name newenv
    
  3. 激活该环境。Activate the environment. 例如,创建 newenv 的结果如下:For example, after creating newenv:

    conda activate newenv
    
  4. 在新环境中安装 pip 和 ipykernel 包,并为该 conda 环境创建内核Install pip and ipykernel package to the new environment and create a kernel for that conda env

    conda install -y pip
    conda install -y ipykernel
    python -m ipykernel install --user --name newenv --display-name "Python (newenv)"
    

备注

对于笔记本中的包管理,使用 %pip 或 %conda magic 函数将包自动安装到当前运行中的内核,而不是安装表示所有包(包括当前运行中的内核之外的包)的 !pip 或 !conda For package management within a notebook, use %pip or %conda magic functions to automatically install packages into the currently-running kernel, rather than !pip or !conda which refers to all packages (including packages outside the currently-running kernel)

可以安装任何可用的 Jupyter 内核Any of the available Jupyter Kernels can be installed.

状态指示器Status indicators

“计算”下拉列表旁的指示器显示计算的状态。An indicator next to the Compute dropdown shows its status. 在计算的下拉列表中也会显示状态。The status is also shown in the dropdown itself.

颜色Color 计算状态Compute status
绿色Green 正在运行计算Compute running
RedRed 计算失败Compute failed
黑色Black 已停止计算Compute stopped
浅蓝色Light Blue 正在创建、正在启动、正在重新启动、正在设置计算Compute creating, starting, restarting, setting Up
灰色Gray 正在删除、正在停止计算Compute deleting, stopping

“内核”下拉列表旁的指示器显示内核的状态。An indicator next to the Kernel dropdown shows its status.

颜色Color 内核状态Kernel status
绿色Green 内核已连接、空闲、繁忙Kernel connected, idle, busy
灰色Gray 内核未连接Kernel not connected

查找计算详细信息Find compute details

有关计算实例的详细信息,可以访问工作室的“计算”页。Find details about your compute instances on the Compute page in studio.

后续步骤Next steps