连接到 Visual Studio Code 中的 Azure 机器学习计算实例(预览)Connect to an Azure Machine Learning compute instance in Visual Studio Code (preview)

在本文中,你将学习如何使用 Visual Studio Code 连接到 Azure 机器学习计算实例。In this article, you'll learn how to connect to an Azure Machine Learning compute instance using Visual Studio Code.

Azure 机器学习计算实例是基于云的完全托管数据科学工作站,并为 IT 管理员提供管理功能和企业就绪功能。An Azure Machine Learning Compute Instance is a fully managed cloud-based workstation for data scientists and provides management and enterprise readiness capabilities for IT administrators.

有两种方法可从 Visual Studio Code 连接到计算实例:There are two ways you can connect to a compute instance from Visual Studio Code:

  • 远程 Jupyter Notebook 服务器。Remote Jupyter Notebook server. 通过此选项,可将计算实例设置为远程 Jupyter Notebook 服务器。This option allows you to set a compute instance as a remote Jupyter Notebook server.
  • Visual Studio Code 远程开发Visual Studio Code remote development. 通过 Visual Studio Code 远程开发,可将容器、远程计算机或适用于 Linux 的 Windows 子系统 (WSL) 作为功能完备的开发环境使用。Visual Studio Code remote development allows you to use a container, remote machine, or the Windows Subsystem for Linux (WSL) as a full-featured development environment.

将计算实例配置为远程笔记本服务器Configure compute instance as remote notebook server

若要将计算实例配置为远程 Jupyter Notebook 服务器,需要满足以下先决条件:In order to configure a compute instance as a remote Jupyter Notebook server you'll need a few prerequisites:

若要连接到计算实例:To connect to a compute instance:

  1. 在 Visual Studio Code 中打开 Jupyter Notebook。Open a Jupyter Notebook in Visual Studio Code.

  2. 加载集成笔记本体验时,选择“Jupyter 服务器”。When the integrated notebook experience loads, select Jupyter Server .

    启动 Azure 机器学习远程 Jupyter 笔记本服务器下拉列表Launch Azure Machine Learning remote Jupyter notebook server dropdown

    此外,还可使用命令面板:Alternatively, you also use the command palette:

    1. 从菜单栏选择“视图”>“命令面板”,打开命令面板。Open the command palette by selecting View > Command Palette from the menu bar.
    2. 在文本框中输入 Azure ML: Connect to Compute instance Jupyter serverEnter into the text box Azure ML: Connect to Compute instance Jupyter server.
  3. 从 Jupyter 服务器选项列表中选择 Azure ML Compute InstancesChoose Azure ML Compute Instances from the list of Jupyter server options.

  4. 从订阅列表中选择你的订阅。Select your subscription from the list of subscriptions. 如果之前配置过默认 Azure 机器学习工作区,则跳过此步骤。If you have have previously configured your default Azure Machine Learning workspace, this step is skipped.

  5. 选择工作区。Select your workspace.

  6. 从列表中选择你的计算实例。Select your compute instance from the list. 如果没有计算实例,请选择“创建新的 Azure ML 计算实例”,并按照提示进行创建。If you don't have one, select Create new Azure ML Compute Instance and follow the prompts to create one.

  7. 要使更改生效,必须重新加载 Visual Studio Code。For the changes to take effect, you have to reload Visual Studio Code.

  8. 打开 Jupyter Notebook 并运行一个单元。Open a Jupyter Notebook and run a cell.

重要

必须运行一个单元才能建立连接。You MUST run a cell in order to establish the connection.

此时,你可继续在 Jupyter 笔记本中运行单元。At this point, you can continue to run cells in your Jupyter notebook.

提示

也可使用包含类似 Jupyter 的代码单元的 Python 脚本文件 (.py)。You can also work with Python script files (.py) containing Jupyter-like code cells. 有关详细信息,请参阅 Visual Studio Code Python 交互文档For more information, see the Visual Studio Code Python interactive documentation.

配置计算实例远程开发Configure compute instance remote development

若要获得功能完备的远程开发体验,需要满足下面几项先决条件:For a full-featured remote development experience, you'll need a few prerequisites:

备注

在 Windows 平台上,必须安装 OpenSSH 兼容的 SSH 客户端(如果还没有)。On Windows platforms, you must install an OpenSSH compatible SSH client if one is not already present. Windows 上不支持 PuTTY,因为 ssh 命令必须在路径中。PuTTY is not supported on Windows since the ssh command must be in the path.

获取计算实例的 IP 和 SSH 端口Get the IP and SSH port for your compute instance

  1. 前往 https://studio.ml.azure.cn/,转到 Azure 机器学习工作室。Go to the Azure Machine Learning studio at https://studio.ml.azure.cn/.

  2. 选择工作区Select your workspace.

  3. 单击“计算实例”选项卡 。Click the Compute Instances tab.

  4. 在“应用程序 URI”列中,单击要用作远程计算的计算实例的 SSH 链接 。In the Application URI column, click the SSH link of the compute instance you want to use as a remote compute.

  5. 请记下对话框中的 IP 地址和 SSH 端口。In the dialog, take note of the IP Address and SSH port.

  6. 将私钥保存到本地计算机上的 ~/.ssh/ 目录中。例如,打开新文件的编辑器,然后将密钥粘贴进来:Save your private key to the ~/.ssh/ directory on your local computer; for instance, open an editor for a new file and paste the key in:

    Linux:Linux :

    vi ~/.ssh/id_azmlcitest_rsa  
    

    WindowsWindows :

    notepad C:\Users\<username>\.ssh\id_azmlcitest_rsa
    

    私钥如下所示:The private key will look somewhat like this:

    -----BEGIN RSA PRIVATE KEY-----
    
    MIIEpAIBAAKCAQEAr99EPm0P4CaTPT2KtBt+kpN3rmsNNE5dS0vmGWxIXq4vAWXD 
    ..... 
    ewMtLnDgXWYJo0IyQ91ynOdxbFoVOuuGNdDoBykUZPQfeHDONy2Raw== 
    
    -----END RSA PRIVATE KEY----- 
    
  7. 更改文件的权限,确保只有你才能读取该文件。Change permissions on file to make sure only you can read the file.

    chmod 600 ~/.ssh/id_azmlcitest_rsa   
    

将实例添加为主机Add instance as a host

在编辑器中打开文件 ~/.ssh/config (Linux) 或 C:\Users<username>.ssh\config (Windows),并添加类似以下内容的新条目:Open the file ~/.ssh/config (Linux) or C:\Users<username>.ssh\config (Windows) in an editor and add a new entry similar to the content below:

Host azmlci1 

    HostName 13.69.56.51 

    Port 50000 

    User azureuser 

    IdentityFile ~/.ssh/id_azmlcitest_rsa   

下面是有关这些字段的一些详细信息:Here some details on the fields:

字段Field 描述Description
主机Host 对计算实例应用所需的任何简写Use whatever shorthand you like for the compute instance
HostNameHostName 这是计算实例的 IP 地址This is the IP address of the compute instance
PortPort 这是上述 SSH 对话框中显示的端口This is the port shown on the SSH dialog above
用户User 这应为  azureuserThis needs to be azureuser
IdentityFileIdentityFile 应指向保存私钥的文件Should point to the file where you saved the private key

现在应该能够使用之前采用的简写 ssh azmlci1 通过 ssh 连接到计算实例。Now, you should be able to ssh to your compute instance using the shorthand you used above, ssh azmlci1.

将 VS Code 连接到实例Connect VS Code to the instance

  1. 单击 Visual Studio Code 活动栏中的远程 SSH 图标,调出 SSH 配置。Click the Remote-SSH icon from the Visual Studio Code activity bar to show your SSH configurations.

  2. 右键单击刚创建的 SSH 主机配置。Right-click the SSH host configuration you just created.

  3. 选择“连接到当前窗口中的主机” 。Select Connect to Host in Current Window .

从这里开始,你会完全在计算实例上操作,可以编辑、调试、使用 git、使用扩展等,就和使用本地 Visual Studio Code 时一样。From here on, you are entirely working on the compute instance and you can now edit, debug, use git, use extensions, etc. -- just like you can with your local Visual Studio Code.

后续步骤Next steps

现在,你已设置 Visual Studio Code 远程,可以将计算实例用作 Visual Studio Code 中的远程计算,从而对代码进行交互式调试Now that you've set up Visual Studio Code Remote, you can use a compute instance as remote compute from Visual Studio Code to interactively debug your code.

教程:训练自己的首个 ML 模型演示如何将计算实例与集成的笔记本配合使用。Tutorial: Train your first ML model shows how to use a compute instance with an integrated notebook.