Set up PyTorch
- Lesson 5
- 40 min
- Module 2: Tensors and PyTorch
Lesson 5 of 23 · Deep Learning and Transformers ExplainedSet up PyTorch
1. What a neural network is
2. Tensors and PyTorch
3. How neural networks learn
4. Diagnose training problems
5. Represent sequences and meaning
6. Assemble a transformer
7. Use pretrained models
8. Under the hood: the mathematicsOptional
What you will be able to do
Get PyTorch working, in Colab or on your own computer. Then prove it with a first run: you can compare every number that it prints with this page. Then each later lesson starts from a setup that you know is good.
NotebookThe first run in a notebook (the Colab route)
The notebook checks PyTorch, runs the first-run code of this lesson, and downloads the tickets with their checksums. It needs no installation. About 10 minutes.
Colab and Kaggle open your own copy. They need a free Google or Kaggle account. Stuck? See the solution notebook.
Before you startWhat to know and to have ready
What you need to know
- Tensors, shapes and the dtype
float32, from Tensors and shapes. - For the local route: the terminal and the working directory, from Files and terminal, and virtual environments, from Isolate dependencies in the Python course.
What you need on your computer
- For the notebook route: a browser and a free Google account (Colab) or Kaggle account.
- For the local route: Python 3.12 or newer (PyTorch 2.14.1 needs 3.10 or newer) and VS Code. You also need an internet connection and about 1 GB of free disk space. The download is about 130 MB on macOS and Windows and about 200 MB on Linux.
Tested with PyTorch 2.14.1 on macOS (Apple silicon), with Python 3.14.6 and 3.12.13. Also tested on Linux (Debian 13, ARM, in a container) with Python 3.12.15 and the CPU version. Not tested by us: Windows, Linux on an Intel or AMD processor, and the notebook in Colab and Kaggle. We ran the notebook in Jupyter on macOS. The commands for those systems follow PyTorch’s own instructions.
Check the oven before you cook
Section titled “Check the oven before you cook”A cook in a new kitchen does not start with a dinner for twelve guests. First, she heats the oven and bakes one simple thing that she knows well. If it comes out as it always does, the oven works. If it does not, she finds out now, with time to fix it, and not when the guests are waiting.
The problem: a setup that only half works
Section titled “The problem: a setup that only half works”A PyTorch setup can fail in quiet ways. Python finds an old version, or a different Python than the one you installed into. On Linux, a plain pip install torch downloads the version for NVIDIA graphics cards. The torch package alone is 555 MB, and its graphics libraries add more than 1 GB. And even a perfect setup prints different random numbers on every run, so you cannot compare your output with this page:
import torchprint(torch.rand(3))print(torch.rand(3))The output (your numbers differ):
tensor([0.8203, 0.2212, 0.8055])tensor([0.1816, 0.2661, 0.1560])torch.rand(3) makes 3 random numbers between 0 and 1. Each call gives new numbers. A network starts with random weights, so an untrained network on your computer would give other scores than the one in The forward pass. You could not tell a setup problem from chance.
The idea: a known-good first run, with a seed
Section titled “The idea: a known-good first run, with a seed”A known-good first run is a small program whose exact output is known in advance. You run it once, right after the setup, and compare each line. If every line matches, the setup works. If a line differs, you know which part to fix before you start real work.
For the numbers to match, the random numbers must repeat. A computer’s random numbers come from a fixed recipe that starts from one number, the seed. The name fits: the same seed always grows the same plant. torch.manual_seed(0) sets the seed to 0, so the next random numbers are always the same:
torch.manual_seed(0)print(torch.rand(3))torch.manual_seed(0)print(torch.rand(3))The output:
tensor([0.4963, 0.7682, 0.0885])tensor([0.4963, 0.7682, 0.0885])Text description of the diagram
The diagram has two panels. The left panel, “No seed”, runs print(torch.rand(3)) two times. The first time gives 0.8203, 0.2212 and 0.8055. The second time gives 0.1816, 0.2661 and 0.1560. The numbers are different each time. The right panel, “With a seed”, sets torch.manual_seed(0) before each print(torch.rand(3)). Both times give 0.4963, 0.7682 and 0.0885: the same numbers each time.
The seed also fixes the starting weights of a network. nn.Linear(3, 2) makes a layer with 3 inputs and 2 outputs, as in Neurons and layers. Its 6 weights start random, and with seed 0 they are always these:
from torch import nn
torch.manual_seed(0)layer = nn.Linear(3, 2) # a new layer: 3 inputs, 2 outputs, random starting weightsprint(layer.weight)The output:
Parameter containing:tensor([[-0.0043, 0.3097, -0.4752], [-0.4249, -0.2224, 0.1548]], requires_grad=True)A seed does not make a result better. It makes it repeatable, so that you can compare and find bugs. Every lesson of this course sets the seed. PyTorch warns that random numbers can still differ between versions and between kinds of hardware. We got the same seeded numbers with PyTorch 2.14.1 on macOS and on Linux.
There are two routes to a working setup. Both end with the same first run.
Text description of the diagram
The diagram starts with two boxes. The first is a notebook in Colab or Kaggle, where PyTorch is already installed. The second is your own computer, with the project folder ticket-router, a virtual environment .venv and pip install torch. Both boxes lead to one box: run the first-run code. From there, a question: is the output the same as in the lesson? If yes, you are ready for the next lesson. If no, read the error, or reset the setup and try again; a dashed arrow leads back to the first run.
The first-run program
Section titled “The first-run program”Here is the program. Every later step runs it. It prints the versions, a seeded tensor of shape (2, 3) with its sum, and it looks for a graphics card. torch.cuda.is_available() is true when PyTorch can use an NVIDIA graphics card. torch.backends.mps.is_available() is true on a Mac with Apple silicon.
"""The known-good first run of the deep learning course: versions, device and a seeded tensor."""import platform
import torch
print("Python ", platform.python_version())print("PyTorch", torch.__version__)
torch.manual_seed(0) # the same seed gives the same random numbersx = torch.rand(2, 3) # 2 rows, 3 columns, numbers between 0 and 1print(x)print("shape", tuple(x.shape), "| dtype", x.dtype, "| device", x.device)print("sum", round(x.sum().item(), 4))
if torch.cuda.is_available(): accelerator = "cuda (an NVIDIA graphics card)"elif torch.backends.mps.is_available(): accelerator = "mps (an Apple silicon graphics unit)"else: accelerator = "none"print("accelerator:", accelerator, "| this course uses the CPU")The output, on a Mac with Python 3.14.6:
Python 3.14.6PyTorch 2.14.1tensor([[0.4963, 0.7682, 0.0885], [0.1320, 0.3074, 0.6341]])shape (2, 3) | dtype torch.float32 | device cpusum 2.4265accelerator: mps (an Apple silicon graphics unit) | this course uses the CPUThe output, on Linux with the CPU version:
Python 3.12.15PyTorch 2.14.1+cputensor([[0.4963, 0.7682, 0.0885], [0.1320, 0.3074, 0.6341]])shape (2, 3) | dtype torch.float32 | device cpusum 2.4265accelerator: none | this course uses the CPULines 3 to 6 must match exactly: the tensor, its shape, dtype and device, and the sum 2.4265. The other lines can differ. Your Python version can be another 3.12 or newer. The PyTorch version ends with +cpu on Windows and Linux, and Colab can have a newer 2.x. The accelerator line depends on your hardware.
Route 1: a notebook in Colab or Kaggle
Section titled “Route 1: a notebook in Colab or Kaggle”Colab and Kaggle run notebooks on their own computers, with PyTorch already installed. This is the fastest route, and the course’s notebooks all work there.
- In the box The first run in a notebook at the top of this page, select Open in Colab. Sign in with your Google account if Colab asks.
- Check the runtime: Runtime › Change runtime type shows CPU. Keep it. The course does not need a graphics card.
- Run the first code cell with Shift+Enter. You see a line that starts with
PyTorch 2.and ends withdevice cpu. - Run the next two setup cells. The data cell ends with
Ready: train 4696 tickets, valid 600, test 600.The helpers cell prints nothing. - Run the first-run cell in section 1. Compare lines 3 to 6 with the output above. The check cell below it prints
Check passed.
In Kaggle, select Open in Kaggle instead. Turn on Internet in the notebook settings before step 4; Kaggle asks for a verified phone number for this. Your notebook copy is private, and your changes stay in your own account.
Route 2: your own computer
Section titled “Route 2: your own computer”Work on your computer if you want to keep files between sessions or work without a browser. The course’s code runs on any ordinary laptop.
Make the project folder and the environment
Section titled “Make the project folder and the environment”-
Open a terminal. Check the Python version:
Terminal window python3 --versionTerminal window python --versionTerminal window python3 --versionYou see
Python 3.12or a higher number. If you see 3.11 or lower, or “command not found”, install Python as in Install and verify Python. -
Make the folder
ticket-routerand go into it:Terminal window mkdir -p ~/projects/ticket-routercd ~/projects/ticket-routerTerminal window mkdir ~\projects\ticket-routercd ~\projects\ticket-routerTerminal window mkdir -p ~/projects/ticket-routercd ~/projects/ticket-router -
Make the virtual environment, then activate it:
Terminal window python3 -m venv .venvsource .venv/bin/activateTerminal window python -m venv .venv.venv\Scripts\Activate.ps1If PowerShell says that running scripts is disabled, see the Windows common mistake in Isolate dependencies.
Terminal window python3 -m venv .venvsource .venv/bin/activateIf the output says that
ensurepipis not available, runsudo apt install python3-venvfirst. -
Check that the prompt starts with
(.venv). From now on, typepythonon every system.
Run every command of this course in this folder, with the environment active.
Install PyTorch
Section titled “Install PyTorch”-
Install PyTorch:
Terminal window python -m pip install torch==2.14.1Terminal window python -m pip install torch==2.14.1 --index-url https://download.pytorch.org/whl/cpuTerminal window python -m pip install torch==2.14.1 --index-url https://download.pytorch.org/whl/cpuIt takes one or two minutes.
==2.14.1asks for the exact version that the course tested. -
Install NumPy, pandas, and
ipykernelfor notebooks in VS Code. The check cells of the course’s notebooks use pandas.Terminal window python -m pip install numpy==2.5.3 pandas==3.0.6 ipykernel -
Record the versions, as in Isolate dependencies:
Terminal window python -m pip freeze > requirements-lock.txtThe file lists about 40 packages, including
torch==2.14.1(torch==2.14.1+cpuon Windows and Linux) andnumpy==2.5.3.
Run the first run
Section titled “Run the first run”-
In VS Code, open the folder
ticket-router(File › Open Folder). -
Make a file
first_run.pyand copy the program above into it. Save it. -
In the terminal, run it:
Terminal window python first_run.py -
Compare lines 3 to 6 with the output above: the tensor,
shape (2, 3) | dtype torch.float32 | device cpuandsum 2.4265.
Get the tickets
Section titled “Get the tickets”The next lesson reads Larkfield’s tickets. The script get_data.py downloads them with the course’s ticketnet.py, and checks each file’s checksum. It uses only Python’s standard library.
-
Download the script into the project folder:
Terminal window curl -fsSL -O https://raw.githubusercontent.com/Nextia-Intelligence/nextia-learning-labs/main/C06/get_data.pyTerminal window curl.exe -fsSL -O https://raw.githubusercontent.com/Nextia-Intelligence/nextia-learning-labs/main/C06/get_data.pyType
curl.exe, notcurl. In Windows PowerShell,curlis a different command.Terminal window curl -fsSL -O https://raw.githubusercontent.com/Nextia-Intelligence/nextia-learning-labs/main/C06/get_data.py -
Run it:
Terminal window python get_data.pyThe output:
Downloaded data/train.csvDownloaded data/valid.csvDownloaded data/test.csvDownloaded data/split_manifest.jsonDownloaded ticketnet.pyReady: train 4696 tickets, valid 600, test 600. -
Run it a second time. It prints only the
Ready:line, because every file is there and correct.
The folder now holds .venv/, requirements-lock.txt, first_run.py, get_data.py, ticketnet.py and data/ with three CSV files and split_manifest.json. To use notebooks in VS Code, select the .venv kernel, as in Open a notebook in VS Code in the machine learning course.
Optional: a graphics card
Section titled “Optional: a graphics card”A graphics card does many multiplications at the same time, so it trains large networks much faster. The networks of this course are small and train in seconds or minutes on a CPU. So every number in this course is a CPU number, and you do not need a graphics card. A graphics card also gives slightly different decimals, so your results would not match this page exactly.
If you want to try one later, the usual pattern picks the device once, then moves the tensors (and the network) there with .to(device):
device = "cuda" if torch.cuda.is_available() else "cpu"x = torch.rand(2, 3).to(device)print(x.device)The output, on a computer with no NVIDIA graphics card:
cpuOn a Mac, the device "mps" works in the same way. Every tensor of one operation must be on the same device. Here is what happens on a Mac when a tensor on the graphics unit meets a tensor on the CPU:
on_gpu = torch.ones(3, device="mps")on_cpu = torch.ones(3)print(on_gpu + on_cpu)The output:
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, mps:0 and cpu!on_gpu.to("cpu") + on_cpu works and gives tensor([2., 2., 2.]). In Colab, Runtime › Change runtime type offers a graphics card, free with time limits. We have not tested the course on it.
Reset the setup
Section titled “Reset the setup”To get back to a known state:
- Colab or Kaggle: in Colab, select Runtime › Disconnect and delete runtime. In Kaggle, stop the session and start it again. Then run the cells again from the top.
- The environment: run
deactivate, delete the folder.venv, then do Make the project folder and the environment from step 3, and Install PyTorch again. - The data and
ticketnet.py: runpython get_data.py --reset. It deletesdata/andticketnet.pyand downloads them again. The last line is theReady:line.
After a reset, run python first_run.py again and compare.
Guided practice: your first run
Section titled “Guided practice: your first run”- Choose your route: the notebook, or your own computer.
- Do every step of that route.
- Run the first run. Write down the sum that it prints.
- Write down your Python version, your PyTorch version and the accelerator line.
- Get the tickets. Check the
Ready:line.
Show what a good result looks like
The sum is sum 2.4265, and the tensor is exactly [[0.4963, 0.7682, 0.0885], [0.1320, 0.3074, 0.6341]]. The Python version is 3.12 or newer, and PyTorch starts with 2. (2.14.1 on your own computer). The accelerator line says none on most Windows and Linux laptops and in a Colab CPU runtime, and mps on a Mac with Apple silicon. The data line is Ready: train 4696 tickets, valid 600, test 600. If all of this matches, your setup is good.
Your turn: another seed
Section titled “Your turn: another seed”Change one thing in first_run.py: torch.manual_seed(0) becomes torch.manual_seed(1). Before you run it, predict: will the tensor be the same as before? Will two runs with seed 1 give the same numbers? Then run it twice.
Show the answer
tensor([[0.7576, 0.2793, 0.4031], [0.7347, 0.0293, 0.7999]])shape (2, 3) | dtype torch.float32 | device cpusum 3.0038The numbers are different from seed 0, and they are the same in both runs with seed 1. Each seed starts its own fixed sequence of random numbers. So “set a seed” is not enough in a report: write which seed. This course always uses seed 0. Change it back before the next lesson.
Common mistake
No module named 'torch'
Symptom: you installed PyTorch, but first_run.py stops at its first import:
Traceback (most recent call last): File "/Users/amira/projects/ticket-router/first_run.py", line 4, in <module> import torchModuleNotFoundError: No module named 'torch'Cause: the script runs with a Python that does not have PyTorch. Usually the environment is not active in this terminal: a new terminal starts without it, and the prompt has no (.venv). In VS Code, the Run button can also use another interpreter than the terminal.
Fix:
- Check which Python runs:
python -c "import sys; print(sys.executable)". - If the path is not in
ticket-router/.venv, activate the environment and run the script again. - If the path is correct, PyTorch is not in this environment. Do Install PyTorch again.
- In VS Code, select the
.venvinterpreter, as in Isolate dependencies.
What it means for real AI work: most “it does not work on my computer” problems are a different interpreter or a different package version. A first run that prints the versions finds them in seconds.
Knowledge check
Section titled “Knowledge check”Check your understanding
Why does every lesson of this course set a seed? What does a seed not do?
Show the answer
A network starts with random weights, and other steps use random numbers too. With a seed, the random numbers repeat, so your run gives the same numbers as the lesson. Then a difference means a real difference, such as a bug or another setup, not chance. A seed does not make a result better or more correct. It only makes it repeatable. And it is one choice among many: another seed gives other numbers, so a report should name the seed.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Your first run prints PyTorch 2.14.1+cpu and accelerator: none, but sum 2.4265 and the same tensor as this page. A friend's run prints PyTorch 2.14.1 and accelerator: mps. Is one of the setups wrong?
Show the answer
No. Both are good. The lines that must match are the seeded tensor, its shape, dtype and device, and the sum 2.4265. +cpu only says that you installed the CPU version from PyTorch’s server. That is the right choice on Windows and Linux. The accelerator line depends on the hardware: your friend has a Mac with Apple silicon. The course uses the CPU on both computers, so both get the same numbers.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Check your understanding
Tomás runs pip install torch on a Linux laptop. The download is more than 1.5 GB and takes very long. What happened, and what should he run instead?
Show the answer
On Linux, the standard PyTorch package is built for NVIDIA graphics cards. pip downloads the torch package (555 MB) and the graphics libraries that it needs, more than 1.5 GB in all. The course uses the CPU, so he does not need them. He should delete the environment, make it again, and run python -m pip install torch==2.14.1 --index-url https://download.pytorch.org/whl/cpu. That downloads the CPU version, about 200 MB.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
Recap
- A known-good first run is a small program with a known output. Run it after every setup and every reset, and compare it line by line.
- A seed makes random numbers repeat.
torch.manual_seed(0)thentorch.rand(2, 3)gives a tensor whose sum is 2.4265. The seed fixes a new network’s starting weights too. - Colab or Kaggle: PyTorch is already installed; open the notebook and run it. Your computer:
ticket-routerwith.venv, thenpip install torch==2.14.1(with the CPU--index-urlon Windows and Linux),numpy==2.5.3,pandas==3.0.6andipykernel. get_data.pydownloads the tickets andticketnet.py, checks them, and ends withReady: train 4696 tickets, valid 600, test 600.- A graphics card is optional. Detect it with
torch.cuda.is_available()ortorch.backends.mps.is_available(); every number in this course is a CPU number. - Reset: delete
.venvand make it again, or runpython get_data.py --reset, then run the first run again.
Next, you turn Larkfield’s tickets into tensors and send them through a first network.
Next: The forward pass