# 用于机器学习的 GitHub Codespaces 入门

了解如何使用 GitHub Codespaces 及其现成工具处理机器学习项目。

## 简介

本指南介绍使用 GitHub Codespaces 进行机器学习。 你将生成简单的图像分类器，了解 GitHub Codespaces 中预安装的一些工具，并了解如何在 JupyterLab 中打开 codespace。

## 生成简单的图像分类器

我们将使用 Jupyter 笔记本生成简单的图像分类器。

Jupyter 笔记本是一组可以逐个执行的单元。 我们将使用的笔记本包括许多单元，它们使用 [PyTorch](https://pytorch.org/) 生成图像分类器。 每个单元都是该过程的不同阶段：下载数据集，设置神经网络，训练模型，然后测试该模型。

我们将按顺序运行所有单元，以执行生成图像分类器的所有阶段。 当我们执行此操作时，Jupyter 会将输出保存回笔记本，以便可以检查结果。

### 创建“codespace”

1. 转到 [github/codespaces-jupyter](https://github.com/github/codespaces-jupyter) 模板存储库。
2. 单击“使用此模板”，然后单击“在 codespace 中打开”。

   ![屏幕截图显示“使用此模板”按钮和展开的下拉菜单，其中显示了“在 codespace 中打开”选项。](/assets/images/help/repository/use-this-template-button.png)

此模板的 codespace 会在基于 Web 的 Visual Studio Code 版本中打开。

### 打开图像分类器笔记本

GitHub Codespaces 使用的默认容器映像包括一组预安装在 codespace 中的机器学习库。 例如，Numpy、pandas、SciPy、Matplotlib、seaborn、scikit-learn、Keras、PyTorch、Requests 和 Plotly。 有关默认映像的详细信息，请参阅 [开发容器简介](/zh/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#using-the-default-dev-container-configuration) 和 [devcontainers/images](https://github.com/devcontainers/images/tree/main/src/universal) 仓库。

1. 在 VS Code 编辑器中，关闭显示的任何“入门”选项卡。
2. 打开 `notebooks/image-classifier.ipynb` 笔记本文件。

### 生成图像分类器

图像分类器笔记本包含下载数据集、训练神经网络以及评估其性能所需的所有代码。

1. 单击“全部运行”以执行所有笔记本单元。

   ![“image-classifier.ipynb”文件的编辑器选项卡顶部的屏幕截图。 光标悬停在标记为“运行全部”的按钮上。](/assets/images/help/codespaces/jupyter-run-all.png)

2. 如果系统提示你选择内核源，请选择**Python环境**，然后在建议的位置选择Python的版本。

   ![“选择Python环境”下拉列表的屏幕截图。 Python版本中的第一个选项标记为“推荐”。](/assets/images/help/codespaces/jupyter-choose-python.png)

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

   ![编辑器中单元格的屏幕截图，标题为“步骤 3：训练网络并保存模型”。](/assets/images/help/codespaces/jupyter-notebook-step3.png)

## 在 JupyterLab 中打开你的 codespace

可以在 JupyterLab ([github.com/codespaces](https://github.com/codespaces)) 的“你的 codespace”页或使用 GitHub CLI 打开 codespace。 有关详细信息，请参阅“[打开现有 codespace](/zh/codespaces/developing-in-a-codespace/opening-an-existing-codespace)”。

JupyterLab 应用程序必须安装在要打开的 Codespace 中。 默认开发容器映像包括 JupyterLab，因此从默认映像创建的 codespaces 将始终安装 JupyterLab。 有关默认映像的详细信息，请参阅“[开发容器简介](/zh/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#using-the-default-dev-container-configuration)”以及 [`devcontainers/images`](https://github.com/devcontainers/images/tree/main/src/universal) 仓库。 如果未在开发容器配置中使用默认映像，则可以通过将 `ghcr.io/devcontainers/features/python` 功能添加到 `devcontainer.json` 文件来安装 JupyterLab。 应该包括选项 `"installJupyterlab": true`。 有关详细信息，请参阅 `devcontainers/features` 存储库中 [`python`](https://github.com/devcontainers/features/tree/main/src/python#python-python) 功能的 README 文件。

## 为 codespace 配置 NVIDIA CUDA

> \[!NOTE]
> 本部分仅适用于可以在使用 GPU 的计算机上创建 codespace 的客户。 在试用期间，向选定的客户提供了选择使用 GPU 的计算机类型的功能。 此选项尚未普遍提供。

某些软件要求安装 NVIDIA CUDA 才能使用 codespace 的 GPU。 在这种情况下，可以使用 `devcontainer.json` 文件创建自己的自定义配置，并指定应安装 CUDA。 有关创建自定义配置的详细信息，请参阅 [开发容器简介](/zh/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#creating-a-custom-dev-container-configuration)。

有关在添加 `nvidia-cuda` 功能时运行的脚本的完整详细信息，请参阅 [devcontainers/features](https://github.com/devcontainers/features/tree/main/src/nvidia-cuda) 存储库。

1. 在代码空间内的编辑器中打开 `.devcontainer/devcontainer.json` 文件。

2. 添加包含以下内容的顶级 `features` 对象：

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

   有关 `features` 对象的详细信息，请参阅[开发容器规范](https://containers.dev/implementors/features/#devcontainer-json-properties)。

   如果使用为本教程创建的图像分类器存储库中的 `devcontainer.json` 文件，则 `devcontainer.json` 文件现在会如下所示：

   ```json
   {
     "customizations": {
       "vscode": {
         "extensions": [
           "ms-python.python",
           "ms-toolsai.jupyter"
         ]
       }
     },
     "features": {
       "ghcr.io/devcontainers/features/nvidia-cuda:1": {
         "installCudnn": true
       }
     }
   }
   ```

3. 保存更改。

4. 访问 VS Code Command Palette (<kbd>Shift</kbd>+<kbd>Command</kbd>+<kbd>P</kbd> / <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>)，然后开始键入“rebuild”。 单击“Codespace: 重生成容器”。

   ![命令面板的屏幕截图，其中包含“重新生成容器”搜索，下拉列表中突出显示了“Codespace: 重新生成容器”选项。](/assets/images/help/codespaces/codespaces-rebuild.png)

   > \[!TIP]
   > 有时可能需要完全重新生成容器以清除缓存并使用新映像重新生成容器。 有关详细信息，请参阅“[在 codespace 中重新生成容器](/zh/codespaces/developing-in-codespaces/rebuilding-the-container-in-a-codespace#about-rebuilding-a-container)”。将重新生成 codespace 容器。 这需要几分钟。 重新生成完成后，codespace 会自动重新打开。

5. 将更改发布到存储库，以便在将来从此存储库创建的任何新 codespace 中安装 CUDA。 有关详细信息，请参阅“[通过模板创建 codespace](/zh/codespaces/developing-in-a-codespace/creating-a-codespace-from-a-template#publishing-from-vs-code)”。