{"meta":{"title":"Getting started with GitHub Codespaces for machine learning","intro":"Learn about working on machine learning projects with GitHub Codespaces and its out-of-the-box tools.","product":"Codespaces","breadcrumbs":[{"href":"/en/codespaces","title":"Codespaces"},{"href":"/en/codespaces/developing-in-a-codespace","title":"Developing in a codespace"},{"href":"/en/codespaces/developing-in-a-codespace/getting-started-with-github-codespaces-for-machine-learning","title":"Machine learning"}],"documentType":"article"},"body":"# Getting started with GitHub Codespaces for machine learning\n\nLearn about working on machine learning projects with GitHub Codespaces and its out-of-the-box tools.\n\n## Introduction\n\nThis 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, and find out how to open your codespace in JupyterLab.\n\n## Building a simple image classifier\n\nWe'll use a Jupyter notebook to build a simple image classifier.\n\nJupyter 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](https://pytorch.org/). 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.\n\nWe'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.\n\n### Creating a codespace\n\n1. Go to the [github/codespaces-jupyter](https://github.com/github/codespaces-jupyter) template repository.\n2. Click **Use this template**, then click **Open in a codespace**.\n\n   ![Screenshot of the \"Use this template\" button and the dropdown menu expanded to show the \"Open in a codespace\" option.](/assets/images/help/repository/use-this-template-button.png)\n\nA codespace for this template will open in a web-based version of Visual Studio Code.\n\n### Opening the image classifier notebook\n\nThe 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](/en/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#using-the-default-dev-container-configuration) and the [devcontainers/images](https://github.com/devcontainers/images/tree/main/src/universal) repository.\n\n1. In the VS Code editor, close any \"Get Started\" tabs that are displayed.\n2. Open the `notebooks/image-classifier.ipynb` notebook file.\n\n### Building the image classifier\n\nThe image classifier notebook contains all the code you need to download a dataset, train a neural network, and evaluate its performance.\n\n1. Click **Run All** to execute all of the notebook’s cells.\n\n   ![Screenshot of the top of the editor tab for the \"image-classifier.ipynb\" file. A cursor hovers over a button labeled \"Run All.\"](/assets/images/help/codespaces/jupyter-run-all.png)\n\n2. If you are prompted to choose a kernel source, select **Python Environments**, then select the version of Python at the recommended location.\n\n   ![Screenshot of the \"Select a Python Environment\" dropdown. The first option in the list of Python versions is labeled \"Recommended.\"](/assets/images/help/codespaces/jupyter-choose-python.png)\n\n3. Scroll down to view the output of each cell.\n\n   ![Screenshot of the cell in the editor, with the header \"Step 3: Train the network and save model.\"](/assets/images/help/codespaces/jupyter-notebook-step3.png)\n\n## Opening your codespace in JupyterLab\n\nYou can open your codespace in JupyterLab from the \"Your codespaces\" page at [github.com/codespaces](https://github.com/codespaces), or by using GitHub CLI. For more information, see [Opening an existing codespace](/en/codespaces/developing-in-a-codespace/opening-an-existing-codespace).\n\nThe JupyterLab application must be installed in the codespace you are opening. The default dev 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](/en/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#using-the-default-dev-container-configuration) and the [`devcontainers/images`](https://github.com/devcontainers/images/tree/main/src/universal) 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`](https://github.com/devcontainers/features/tree/main/src/python#python-python) feature, in the `devcontainers/features` repository.\n\n## Configuring NVIDIA CUDA for your codespace\n\n> \\[!NOTE]\n> This section only applies to customers who can create codespaces on machines that use a GPU. The ability to choose a machine type that uses a GPU was offered to selected customers during a trial period. This option is not generally available.\n\nSome 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](/en/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/introduction-to-dev-containers#creating-a-custom-dev-container-configuration).\n\nFor full details of the script that's run when you add the `nvidia-cuda` feature, see the [devcontainers/features](https://github.com/devcontainers/features/tree/main/src/nvidia-cuda) repository.\n\n1. Within the codespace, open the `.devcontainer/devcontainer.json` file in the editor.\n\n2. Add a top-level `features` object with the following contents:\n\n   ```json copy\n     \"features\": {\n       \"ghcr.io/devcontainers/features/nvidia-cuda:1\": {\n         \"installCudnn\": true\n       }\n     }\n   ```\n\n   For more information about the `features` object, see the [development containers specification](https://containers.dev/implementors/features/#devcontainer-json-properties).\n\n   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:\n\n   ```json\n   {\n     \"customizations\": {\n       \"vscode\": {\n         \"extensions\": [\n           \"ms-python.python\",\n           \"ms-toolsai.jupyter\"\n         ]\n       }\n     },\n     \"features\": {\n       \"ghcr.io/devcontainers/features/nvidia-cuda:1\": {\n         \"installCudnn\": true\n       }\n     }\n   }\n   ```\n\n3. Save the change.\n\n4. Access the VS Code Command Palette (<kbd>Shift</kbd>+<kbd>Command</kbd>+<kbd>P</kbd> / <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>), then start typing \"rebuild\". Click **Codespaces: Rebuild Container**.\n\n   ![Screenshot of the Command Palette with a search for \"rebuild container\" and the \"Codespace: Rebuild Container\" option highlighted in the dropdown.](/assets/images/help/codespaces/codespaces-rebuild.png)\n\n   > \\[!TIP]\n   > You may occasionally want to perform a full rebuild to clear your cache and rebuild your container with fresh images. For more information, see [Rebuilding the container in a codespace](/en/codespaces/developing-in-codespaces/rebuilding-the-container-in-a-codespace#about-rebuilding-a-container).\n   > The codespace container will be rebuilt. This will take several minutes. When the rebuild is complete the codespace is automatically reopened.\n\n5. 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](/en/codespaces/developing-in-a-codespace/creating-a-codespace-from-a-template#publishing-from-vs-code)."}