跳至主要内容

使用 GitHub Codespaces 进行机器学习入门

了解如何使用 GitHub Codespaces 开发机器学习项目以及其开箱即用的工具。

简介

本指南将向你介绍如何在 GitHub Codespaces 中进行机器学习。你将构建一个简单的图像分类器,了解 GitHub Codespaces 预装的一些工具,并学习如何在 JupyterLab 中打开你的代码空间。

构建一个简单的图像分类器

我们将使用 Jupyter Notebook 来构建一个简单的图像分类器。

Jupyter Notebook 是一系列可以依次执行的单元格。我们将使用的 notebook 包含多个单元格,利用 PyTorch 构建图像分类器。每个单元格对应该过程的不同阶段:下载数据集、设置神经网络、训练模型,然后测试模型。

我们将依次运行所有单元格,以完成图像分类器构建的全部阶段。运行时,Jupyter 会将输出保存回 notebook,方便你查看结果。

创建代码空间

  1. 前往 github/codespaces-jupyter 模板仓库。

  2. 点击 Use this template(使用此模板),然后点击 Open in a codespace(在代码空间中打开)。

    Screenshot of the "Use this template" button and the dropdown menu expanded to show the "Open in a codespace" option.

此模板的代码空间将在基于网页的 Visual Studio Code 中打开。

打开图像分类器 notebook

GitHub Codespaces 使用的默认容器镜像已预装了一组机器学习库,包括 Numpy、pandas、SciPy、Matplotlib、seaborn、scikit-learn、Keras、PyTorch、Requests 和 Plotly。有关默认镜像的更多信息,请参阅 开发容器简介以及 devcontainers/images 仓库。

  1. 在 VS Code 编辑器中,关闭所有显示的 “Get Started” 选项卡。
  2. 打开 notebooks/image-classifier.ipynb notebook 文件。

构建图像分类器

图像分类器 notebook 包含完成下载数据集、训练神经网络以及评估其性能所需的全部代码。

  1. 点击 Run All(运行全部)来执行 notebook 的所有单元格。

    Screenshot of the top of the editor tab for the "image-classifier.ipynb" file. A cursor hovers over a button labeled "Run All."

  2. 如果出现选择内核来源的提示,选择 Python Environments(Python 环境),然后在推荐位置选择相应的 Python 版本。

    Screenshot of the "Select a Python Environment" dropdown. The first option in the list of Python versions is labeled "Recommended."

  3. 向下滚动以查看每个单元格的输出。

    Screenshot of the cell in the editor, with the header "Step 3: Train the network and save model."

在 JupyterLab 中打开你的代码空间

你可以在 “Your codespaces” 页面 github.com/codespaces 上打开代码空间的 JupyterLab,或使用 GitHub CLI。更多信息请参阅 打开已有代码空间

JupyterLab 必须已安装在你要打开的代码空间中。默认的开发容器镜像已包含 JupyterLab,因此基于默认镜像创建的代码空间始终具备 JupyterLab。有关默认镜像的更多信息,请参阅 开发容器简介以及 devcontainers/images 仓库。如果你的 devcontainer 配置未使用默认镜像,可以通过在 devcontainer.json 中添加 ghcr.io/devcontainers/features/python 特性并加入 "installJupyterlab": true 来安装 JupyterLab。更多信息请参阅 devcontainers/features 仓库中 python 特性的 README。

为你的代码空间配置 NVIDIA CUDA

注意

本节仅适用于能够在配备 GPU 的机器上创建代码空间的用户。GPU 机器类型的选择曾在试用期间向部分客户开放,且目前尚未普遍提供。

某些软件需要安装 NVIDIA CUDA 才能使用代码空间的 GPU。若出现此类需求,你可以通过在 devcontainer.json 中自定义配置并指定安装 CUDA。有关创建自定义配置的更多信息,请参阅 开发容器简介

有关添加 nvidia-cuda 特性时运行的脚本的完整细节,请查看 devcontainers/features 仓库。

  1. 在代码空间中,使用编辑器打开 .devcontainer/devcontainer.json 文件。

  2. 在顶层添加一个 features 对象,内容如下

    JSON
      "features": {
        "ghcr.io/devcontainers/features/nvidia-cuda:1": {
          "installCudnn": true
        }
      }
    

    有关 features 对象的更多信息,请参阅 开发容器规范

    如果你正在使用本教程中为图像分类器仓库创建的 devcontainer.json 文件,则你的 devcontainer.json 现在应如下所示

    {
      "customizations": {
        "vscode": {
          "extensions": [
            "ms-python.python",
            "ms-toolsai.jupyter"
          ]
        }
      },
      "features": {
        "ghcr.io/devcontainers/features/nvidia-cuda:1": {
          "installCudnn": true
        }
      }
    }
    
  3. 保存更改。

  4. 打开 VS Code 命令面板(Shift+Command+P / Ctrl+Shift+P),输入 “rebuild”。点击 Codespaces: Rebuild Container

    Screenshot of the Command Palette with a search for "rebuild container" and the "Codespace: Rebuild Container" option highlighted in the dropdown.

    提示

    有时你可能需要进行完整的重建,以清除缓存并使用全新镜像重新构建容器。更多信息请参阅 在代码空间中重建容器。代码空间容器将被重新构建,过程需要几分钟,完成后代码空间会自动重新打开。

  5. 将你的更改推送到仓库,这样在未来从此仓库创建的新代码空间中就会自动安装 CUDA。更多信息请参阅 从模板创建代码空间

© . This site is unofficial and not affiliated with GitHub, Inc.