使用 VS Code Remote 对 Azure 机器学习计算实例进行交互式调试Debug interactively on an Azure Machine Learning Compute Instance with VS Code Remote

本文介绍如何在 Azure 机器学习计算实例上设置 Visual Studio Code Remote 扩展,从而通过 VS Code 对代码进行交互式调试。In this article, you'll learn how to set up Visual Studio Code Remote extension on an Azure Machine Learning Compute Instance so you can interactively debug your code from VS 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 Remote Development,可将容器、远程计算机或适用于 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.

先决条件Prerequisite

备注

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 this:

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 CodeInstall Visual Studio Code.

  2. 安装远程 Remote SSH 扩展Install the Remote SSH Extension.

  3. 单击左侧的“Remote-SSH”图标,显示 SSH 配置。Click the Remote-SSH icon on the left to show your SSH configurations.

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

  5. 选择“连接到当前窗口中的主机”****。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 Remote,可以将计算实例用作 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.