Skip to main content

Getting started with GitHub Codespaces for machine learning

Learn about working on machine learning projects with GitHub Codespaces and its out-of-the-box tools.

Introduction

This guide introduces you to machine learning with GitHub Codespaces. You’ll build a simple image classifier, learn about some of the tools that come preinstalled in GitHub Codespaces, configure your development environment for NVIDIA CUDA, and open your codespace in JupyterLab.

Building a simple image classifier

We'll use a Jupyter notebook to build a simple image classifier.

Jupyter notebooks are sets of cells that you can execute one after another. The notebook we'll use includes a number of cells that build an image classifier using PyTorch. Each cell is a different phase of that process: download a dataset, set up a neural network, train a model, and then test that model.

We'll run all of the cells, in sequence, to perform all phases of building the image classifier. When we do this Jupyter saves the output back into the notebook so that you can examine the results.

Creating a codespace

  1. Go to the github/codespaces-jupyter template repository.

  2. Above the file list, click Use this template, then select Open in a codespace.

    Use this template button

A codespace for this template will open in a web-based version of Visual Studio Code.

Opening the image classifier notebook

The default container image that's used by GitHub Codespaces includes a set of machine learning libraries that are preinstalled in your codespace. For example, Numpy, pandas, SciPy, Matplotlib, seaborn, scikit-learn, Keras, PyTorch, Requests, and Plotly. For more information about the default image, see "Introduction to dev containers" and the devcontainers/images repository.

  1. In the VS Code editor, close any "Get Started" tabs that are displayed.
  2. Open the notebooks/image-classifier.ipynb notebook file.

Building the image classifier

The image classifier notebook contains all the code you need to download a dataset, train a neural network, and evaluate its performance.

  1. Click Run All to execute all of the notebook’s cells.

    Screenshot of the Run All button

  2. Scroll down to view the output of each cell.

    Screenshot of Step 3 in the editor

Configuring NVIDIA CUDA for your codespace

Some software requires you to install NVIDIA CUDA to use your codespace’s GPU. Where this is the case, you can create your own custom configuration, by using a devcontainer.json file, and specify that CUDA should be installed. For more information on creating a custom configuration, see "Introduction to dev containers."

Note: For full details of the script that's run when you add the nvidia-cuda feature, see the devcontainers/features repository.

  1. Within the codespace, open the .devcontainer/devcontainer.json file in the editor.

  2. Add a top-level features object with the following contents:

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

    For more information about the features object, see the development containers specification.

    If you are using the devcontainer.json file from the image classifier repository you created for this tutorial, your devcontainer.json file will now look like this:

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

  4. Access the VS Code Command Palette (Shift+Command+P (Mac) / Ctrl+Shift+P (Windows/Linux)), then start typing "rebuild". Select Codespaces: Rebuild Container.

    Screenshot of Rebuild Container command in the Command Pallette

    Tip: You may occasionally want to perform a full rebuild to clear your cache and rebuild your container with fresh images. For more information, see "Performing a full rebuild of a container."

    The codespace container will be rebuilt. This will take several minutes. When the rebuild is complete the codespace is automatically reopened.
  5. Publish your change to a repository so that CUDA will be installed in any new codespaces you create from this repository in future. For more information, see "Creating a codespace from a template."

Opening your codespace in JupyterLab

You can open your codespace in JupyterLab from the "Your codespaces" page at github.com/codespaces, or by using GitHub CLI. For more information, see "Opening an existing codespace."

The JupyterLab application must be installed in the codespace you are opening. The default container image includes JupyterLab, so codespaces created from the default image will always have JupyterLab installed. For more information about the default image, see "Introduction to dev containers" and the devcontainers/images repository. If you're not using the default image in your dev container configuration, you can install JupyterLab by adding the ghcr.io/devcontainers/features/python feature to your devcontainer.json file. You should include the option "installJupyterlab": true. For more information, see the README for the python feature, in the devcontainers/features repository.