Chapter 1 · Part 1

Install Python & PyTorch

Unlike the other courses on this site, this one is hands-on. By the end you'll have trained a real neural network that reads handwritten digits — and it all runs on the machine in front of you. This first chapter gets that machine ready. Budget five minutes here; the actual project is the next five chapters.

You need three things: a recent Python, an isolated environment to install into, and the PyTorch library itself.

1 · Check your Python

PyTorch needs Python 3.9 or newer. Check what you have:

terminal
python3 --version

If that prints Python 3.9 or higher, you're set. If the command isn't found or the version is older, install a current Python from python.org/downloads first.

2 · Make a virtual environment

Never install project libraries into your system Python — one project's versions will eventually break another's. A virtual environment is a private, throw-away copy of Python just for this project. Create one and activate it:

macOS / Linux
python3 -m venv .venv
source .venv/bin/activate
Windows (PowerShell)
python -m venv .venv
.venv\Scripts\Activate.ps1

Your prompt should now start with (.venv). Everything you pip install from here lands inside that folder and nowhere else. (To leave it later, run deactivate.)

3 · Install PyTorch

With the environment active, install the two libraries this project needs — PyTorch itself and torchvision, which bundles the digit dataset we'll use:

terminal
pip install torch torchvision

This is the plain CPU build — perfect for this project, since our network is tiny and trains in seconds either way. If you have an NVIDIA GPU and want the CUDA build, use the picker at pytorch.org/get-started to get the exact command for your setup. On a Mac, this same command gives you Apple's MPS GPU acceleration for free.

4 · Verify it worked

Create a file called check.py and run it. This confirms PyTorch imported and tells you which device you'll be training on:

check.py
import torch

# Pick the fastest device available: NVIDIA GPU, Apple GPU, or CPU.
if torch.cuda.is_available():
  device = "cuda"
elif torch.backends.mps.is_available():
  device = "mps"
else:
  device = "cpu"

print("PyTorch version:", torch.__version__)
print("Training on:", device)
terminal
python check.py

You should see something like:

PyTorch version: 2.5.1
Training on: cpu

cpu is completely fine — the whole project runs in under a minute on it. If you see cuda or mps, even better. Either way, your machine is ready. Next we'll meet the one object everything in PyTorch is built from: the tensor.