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 use GitHub CLI to open your codespace in JupyterLab.
Prerequisite
You have access to GitHub Codespaces. For more information, see "Creating a codespace."
Build 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 repository and a codespace
-
Go to the github/codespaces-getting-started-ml template repository and click Use this template.
-
Select an owner for the new repository, enter a repository name, select your preferred privacy setting, and click Create repository from template.
-
On the main page of the newly created repository, click the Code button and select the Codespaces tab.

If you don’t see this tab, GitHub Codespaces isn't available for you. For more information about access to GitHub Codespaces, see "Creating a codespace."
-
On the Codespaces tab, click Create codespace on main.
By default, a codespace for this repository opens in a web-based version of Visual Studio Code.
Open 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, TensorFlow, Keras, PyTorch, Requests, and Plotly. For more information about the default image, see "Introduction to dev containers" and the devcontainers/images repository.
-
In the VS Code editor, close any "Get Started" tabs that are displayed.
-
Open the
image-classifier.ipynbnotebook file. -
Click the Python kernel link at the top right of the editor.

-
In the drop-down menu, choose the kernel in the directory
/opt/python/latest/bin/python.
Build 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.
-
Click Run All to execute all of the notebook’s cells.

-
Scroll down to view the output of each cell.

Configure NVIDIA CUDA for your codespace
Some software, such as TensorFlow, 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.
-
Within a codespace, open the
.devcontainer/devcontainer.jsonfile in the editor. -
Add a top-level
featuresobject with the following contents:JSON "features": { "ghcr.io/devcontainers/features/nvidia-cuda:1": { "installCudnn": true } }For more information about the
featuresobject, see the development containers specification.If you are using the
devcontainer.jsonfile from the image classifier repository you created for this tutorial, yourdevcontainer.jsonfile will now look like this:{ "customizations": { "vscode": { "extensions": [ "ms-python.python", "ms-toolsai.jupyter" ] } }, "features": { "ghcr.io/devcontainers/features/nvidia-cuda:1": { "installCudnn": true } } } -
Save the change.
-
Access the VS Code Command Palette (Shift+Command+P (Mac) / Ctrl+Shift+P (Windows/Linux)), then start typing "rebuild". Select Codespaces: Rebuild Container.
The codespace container will be rebuilt. This will take several minutes. When the rebuild is complete the codespace is automatically reopened. -
Commit the change to the repository so that CUDA will be installed in any new codespaces you create from this repository in future.
Open your codespace in JupyterLab
The default container image that's used by GitHub Codespaces includes JupyterLab, the web-based Jupyter IDE. You can use GitHub CLI to open your codespace in JupyterLab without having to install anything else on your codespace.
-
In the terminal, enter the GitHub CLI command
gh cs jupyter. -
Choose the codespace you want to open.


