在工作区中运行 Jupyter 笔记本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.

有关如何创建和管理文件(包括笔记本)的信息,请参阅在工作区中创建和管理文件For information on how to create and manage files, including notebooks, see Create and manage files in your workspace.

先决条件Prerequisites

编辑笔记本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. 如果本部分中没有任何笔记本,请参阅在工作区中创建和管理文件If you don't have any notebooks in this section, see Create and manage files in your workspace.

你可以在不连接到计算实例的情况下编辑笔记本。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.

导出笔记本Export a notebook

在笔记本工具栏中选择菜单,然后选择“导出为”,以任一受支持类型导出笔记本:In the notebook toolbar, select the menu and then Export As to export the notebook as any of the supported types:

  • 笔记本Notebook
  • PythonPython
  • HTMLHTML
  • LaTeXLaTeX

将笔记本导出到计算机

导出的文件保存在计算机上。The exported file is saved on your computer.

运行笔记本或 Python 脚本Run a notebook or Python script

若要运行笔记本或 Python 脚本,请先连接到正在运行的计算实例To run a notebook or a Python script, you first connect to a running compute instance.

  • 如果没有计算实例,请使用以下步骤创建一个计算实例:If you don't have a compute instance, use these steps to create one:

    1. 在笔记本或脚本工具栏中,选择“计算”下拉列表右侧的“+ 新建计算”。In the notebook or script toolbar, to the right of the Compute dropdown, select + New Compute. 它可能位于“...”菜单下,具体取决于屏幕大小。Depending on your screen size, this may be located under a ... menu. 创建新计算
    2. 为计算命名,并在“虚拟机大小”中选择一个大小。Name the Compute and choose a Virtual Machine Size.
    3. 选择“创建”。Select Create.
    4. 计算实例自动连接到文件。The compute instance is connected to the file automatically. 现在可以使用计算实例左侧的工具来运行笔记本单元格或 Python 脚本。You can now run the notebook cells or the Python script using the tool to the left of the compute instance.
  • 如果有已停止的计算实例,请选择“计算”下拉列表右侧的“开始计算”。If you have a stopped compute instance, select Start compute to the right of the Compute dropdown. 它可能位于“...”菜单下,具体取决于屏幕大小。Depending on your screen size, this may be located under a ... menu.

    启动计算实例

只有本人可以查看和使用自己创建的计算实例。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. 屏幕截图:Jupyter 笔记本小组件

探究笔记本中的变量Explore variables in the notebook

在笔记本工具栏上,使用“变量资源管理器”工具显示已在笔记本中创建的所有变量的名称、类型、长度和示例值。On the notebook toolbar, use the Variable explorer tool to show the name, type, length, and sample values for all variables that have been created in your notebook.

屏幕截图:“变量资源管理器”工具

选择此工具可显示“变量资源管理器”窗口。Select the tool to show the variable explorer window.

屏幕截图:“变量资源管理器”窗口

在笔记本工具栏上,使用“目录”工具显示或隐藏目录。On the notebook toolbar, use the Table of contents tool to display or hide the table of contents. 启动带标题的 Markdown 单元格,以将其添加到目录中。Start a markdown cell with a heading to add it to the table of contents. 单击表中的某个条目可滚动到笔记本中的相应单元格。Click on an entry in the table to scroll to that cell in the notebook.

屏幕截图:笔记本中的目录

更改笔记本环境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

使用终端创建新内核,并将其添加到计算实例。Use the terminal to create and add new kernels to your compute instance. 笔记本会自动查找连接的计算实例上安装的所有 Jupyter 内核。The notebook will automatically find all Jupyter kernels installed on the connected compute instance.

使用右侧的“内核”下拉列表更改为任何已安装的内核。Use the kernel dropdown on the right to change to any of the installed kernels.

状态指示器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.

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

与 Jupyter Notebook 类似,Azure 机器学习工作室笔记本有一个模式化用户界面。Similar to Jupyter Notebooks, Azure Machine Learning Studio notebooks have a modal user interface. 键盘根据笔记本单元格的具体模式执行不同的任务。The keyboard does different things depending on which mode the notebook cell is in. 对于给定的代码单元格,Azure 机器学习工作室笔记本支持以下两种模式:命令模式和编辑模式。Azure Machine Learning Studio notebooks support the following two modes for a given code cell: command mode and edit mode.

命令模式快捷方式Command mode shortcuts

当没有文本光标提示你键入时,单元格处于命令模式。A cell is in command mode when there is no text cursor prompting you to type. 当单元格处于命令模式时,可以将笔记本作为一个整体进行编辑,但不能键入单独的单元格。When a cell is in Command mode, you can edit the notebook as a whole but not type into individual cells. ESC 或使用鼠标选择单元格编辑器区域外部,进入命令模式。Enter command mode by pressing ESC or using the mouse to select outside of a cell's editor area. 活动单元格的左边框为蓝色且为实线,其“运行”按钮为蓝色。The left border of the active cell is blue and solid, and its Run button is blue.

命令模式下的笔记本单元格

快捷键Shortcut 描述Description
EnterEnter 进入编辑模式Enter edit mode
Shift+EnterShift + Enter 运行单元格,选择下方Run cell, select below
Control/Command + EnterControl/Command + Enter 运行单元格Run cell
Alt + EnterAlt + Enter 运行单元格,在下方插入代码单元格Run cell, insert code cell below
Control/Command + Alt + EnterControl/Command + Alt + Enter 运行单元格,在下方插入 Markdown 单元格Run cell, insert markdown cell below
Alt + RAlt + R 全部运行Run all
YY 将单元格转换为代码Convert cell to code
MM 将单元格转换为 MarkdownConvert cell to markdown
Up/KUp/K 选择上方的单元格Select cell above
Down/JDown/J 选择下方的单元格Select cell below
AA 在上方插入代码单元格Insert code cell above
BB 在下方插入代码单元格Insert code cell below
Control/Command + Shift + AControl/Command + Shift + A 在上方插入 Markdown 单元格Insert markdown cell above
Control/Command + Shift + BControl/Command + Shift + B 在下方插入 Markdown 单元格Insert markdown cell below
XX 剪切所选单元格Cut selected cell
CC 复制所选单元格Copy selected cell
Shift + VShift + V 在上方粘贴所选单元格Paste selected cell above
VV 在下方粘贴所选单元格Paste selected cell below
D DD D 删除所选单元格Delete selected cell
OO 切换输出Toggle output
Shift + OShift + O 打开/关闭输出滚动Toggle output scrolling
I II I 中断内核Interrupt kernel
0 00 0 重启内核Restart kernel
Shift + 空格键Shift + Space 向上滚动Scroll up
SpaceSpace 向下滚动Scroll down
选项卡Tab 将焦点更改到下一个可聚焦的项(当 Tab 陷阱被禁用时)Change focus to next focusable item (when tab trap disabled)
Control/Command + SControl/Command + S 保存笔记本Save notebook
11 更改到 h1Change to h1
22 更改到 h2Change to h2
33 更改到 h3Change to h3
44 更改到 h4Change to h4
55 更改到 h5Change to h5
66 更改到 h6Change to h6

编辑模式快捷方式Edit mode shortcuts

编辑模式由文本光标指示,提示你在编辑器区域中键入内容。Edit mode is indicated by a text cursor prompting you to type in the editor area. 当单元格处于编辑模式时,可以在单元格中键入。When a cell is in edit mode, you can type into the cell. Enter 或使用鼠标选择单元格的编辑器区域,进入编辑模式。Enter edit mode by pressing Enter or using the mouse to select on a cell's editor area. 活动单元格的左边框为绿色且为交错线,其“运行”按钮为绿色。The left border of the active cell is green and hatched, and its Run button is green. 在“编辑”模式下,还会在单元格中看到光标提示。You also see the cursor prompt in the cell in Edit mode.

编辑模式下的笔记本单元格

使用以下击键快捷方式,可以在“编辑”模式下更轻松地在 Azure 机器学习笔记本中导航并运行代码。Using the following keystroke shortcuts, you can more easily navigate and run code in Azure Machine Learning notebooks when in Edit mode.

快捷键Shortcut 描述Description
EscapeEscape 进入命令模式Enter command mode
Control/Command + 空格键Control/Command + Space 激活 IntelliSenseActivate IntelliSense
Shift+EnterShift + Enter 运行单元格,选择下方Run cell, select below
Control/Command + EnterControl/Command + Enter 运行单元格Run cell
Alt + EnterAlt + Enter 运行单元格,在下方插入代码单元格Run cell, insert code cell below
Control/Command + Alt + EnterControl/Command + Alt + Enter 运行单元格,在下方插入 Markdown 单元格Run cell, insert markdown cell below
Alt + RAlt + R 运行所有单元格Run all cells
向上Up 将光标上移或移到上一个单元格Move cursor up or previous cell
向下Down 将光标下移或移到下一个单元格Move cursor down or next cell
Control/Command + SControl/Command + S 保存笔记本Save notebook
Control/Command + 向上箭头键Control/Command + Up 转到单元格开头Go to cell start
Control/Command + 向下箭头键Control/Command + Down 转到单元格末尾Go to cell end
选项卡Tab 代码完成或缩进(如果启用了 Tab 陷阱)Code completion or indent (if tab trap enabled)
Control/Command + MControl/Command + M 启用/禁用 Tab 陷阱Enable/disable tab trap
Control/Command + ]Control/Command + ] 缩进Indent
Control/Command + [Control/Command + [ 取消缩进Dedent
Control/Command + AControl/Command + A 全选Select all
Control/Command + ZControl/Command + Z 撤消Undo
Control/Command + Shift + ZControl/Command + Shift + Z 重做Redo
Control/Command + YControl/Command + Y 重做Redo
Control/Command + HomeControl/Command + Home 转到单元格开头Go to cell start
Control/Command + EndControl/Command + End 转到单元格末尾Go to cell end
Control/Command + 向左箭头键Control/Command + Left 左移一个字Go one word left
Control/Command + 向右箭头键Control/Command + Right 右移一个字Go one word right
Control/Command + BackspaceControl/Command + Backspace 删除插入提示之前的字Delete word before
Control/Command + DeleteControl/Command + Delete 删除插入提示之后的字Delete word after
Control/Command + /Control/Command + / 打开/关闭单元注释Toggle comment on cell

疑难解答Troubleshooting

  • 如果无法连接到笔记本,请确保未禁用 Web 套接字通信。If you can't connect to a notebook, ensure that web socket communication is not disabled. 为了让计算实例 Jupyter 功能可以正常运行,必须启用 Web 套接字通信。For compute instance Jupyter functionality to work, web socket communication must be enabled. 请确保网络允许到 *.instances.azureml.net 和 *.instances.azureml.ms 的 websocket 连接。Please ensure your network allows websocket connections to *.instances.azureml.net and *.instances.azureml.ms.

  • 在专用链接工作区中部署计算实例时,只能从虚拟网络内部访问When compute instance is deployed in a private link workspace it can be only be accessed from within virtual network. 如果使用自定义 DNS 或 hosts 文件,请为 < instance-name >.< region >.instances.azureml.ms 添加一个条目,让该条目包含工作区专用终结点的专用 IP 地址。If you are using custom DNS or hosts file please add an entry for < instance-name >.< region >.instances.azureml.ms with private IP address of workspace private endpoint. 有关详细信息,请参阅自定义 DNS 一文。For more information see the custom DNS article.

后续步骤Next steps