Skip to content

Set up PyTorch

  • Lesson 5
  • 40 min
  • Module 2: Tensors and PyTorch

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)Colab · Kaggle · GitHub

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 readyAbout 40 minutes

What you need to know

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.

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.

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 torch
print(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])
Two panels. No seed: print(torch.rand(3)) the first time gives 0.8203, 0.2212, 0.8055 and the second time 0.1816, 0.2661, 0.1560: different numbers each time. With a seed: torch.manual_seed(0), then print(torch.rand(3)), gives 0.4963, 0.7682, 0.0885 both times: the same numbers each time.
The same code without and with a seed. The numbers without a seed come from one real run; yours will be different.
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 weights
print(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.

Two starting boxes: a notebook in Colab or Kaggle, where PyTorch is already there; and your computer, with the folder ticket-router, a .venv environment and pip install torch. Both lead to: run the first-run code. Then a question: same output as the lesson? Yes leads to: ready for the next lesson. No leads to: read the error, or reset and try again, with a dashed arrow back to the first run.
Two routes, one check. Whichever route you take, the first run must print the numbers on this page.
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.

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.

first_run.py
"""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 numbers
x = torch.rand(2, 3) # 2 rows, 3 columns, numbers between 0 and 1
print(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.6
PyTorch 2.14.1
tensor([[0.4963, 0.7682, 0.0885],
[0.1320, 0.3074, 0.6341]])
shape (2, 3) | dtype torch.float32 | device cpu
sum 2.4265
accelerator: mps (an Apple silicon graphics unit) | this course uses the CPU

The output, on Linux with the CPU version:

Python 3.12.15
PyTorch 2.14.1+cpu
tensor([[0.4963, 0.7682, 0.0885],
[0.1320, 0.3074, 0.6341]])
shape (2, 3) | dtype torch.float32 | device cpu
sum 2.4265
accelerator: none | this course uses the CPU

Lines 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.

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.

  1. 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.
  2. Check the runtime: Runtime › Change runtime type shows CPU. Keep it. The course does not need a graphics card.
  3. Run the first code cell with Shift+Enter. You see a line that starts with PyTorch 2. and ends with device cpu.
  4. Run the next two setup cells. The data cell ends with Ready: train 4696 tickets, valid 600, test 600. The helpers cell prints nothing.
  5. 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.

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”
  1. Open a terminal. Check the Python version:

    Terminal window
    python3 --version

    You see Python 3.12 or a higher number. If you see 3.11 or lower, or “command not found”, install Python as in Install and verify Python.

  2. Make the folder ticket-router and go into it:

    Terminal window
    mkdir -p ~/projects/ticket-router
    cd ~/projects/ticket-router
  3. Make the virtual environment, then activate it:

    Terminal window
    python3 -m venv .venv
    source .venv/bin/activate
  4. Check that the prompt starts with (.venv). From now on, type python on every system.

Run every command of this course in this folder, with the environment active.

  1. Install PyTorch:

    Terminal window
    python -m pip install torch==2.14.1

    It takes one or two minutes. ==2.14.1 asks for the exact version that the course tested.

  2. Install NumPy, pandas, and ipykernel for 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
  3. Record the versions, as in Isolate dependencies:

    Terminal window
    python -m pip freeze > requirements-lock.txt

    The file lists about 40 packages, including torch==2.14.1 (torch==2.14.1+cpu on Windows and Linux) and numpy==2.5.3.

  1. In VS Code, open the folder ticket-router (File › Open Folder).

  2. Make a file first_run.py and copy the program above into it. Save it.

  3. In the terminal, run it:

    Terminal window
    python first_run.py
  4. Compare lines 3 to 6 with the output above: the tensor, shape (2, 3) | dtype torch.float32 | device cpu and sum 2.4265.

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.

  1. 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.py
  2. Run it:

    Terminal window
    python get_data.py

    The output:

    Downloaded data/train.csv
    Downloaded data/valid.csv
    Downloaded data/test.csv
    Downloaded data/split_manifest.json
    Downloaded ticketnet.py
    Ready: train 4696 tickets, valid 600, test 600.
  3. 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.

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:

cpu

On 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.

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: run python get_data.py --reset. It deletes data/ and ticketnet.py and downloads them again. The last line is the Ready: line.

After a reset, run python first_run.py again and compare.

  1. Choose your route: the notebook, or your own computer.
  2. Do every step of that route.
  3. Run the first run. Write down the sum that it prints.
  4. Write down your Python version, your PyTorch version and the accelerator line.
  5. 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.

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 cpu
sum 3.0038

The 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 torch
ModuleNotFoundError: 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:

  1. Check which Python runs: python -c "import sys; print(sys.executable)".
  2. If the path is not in ticket-router/.venv, activate the environment and run the script again.
  3. If the path is correct, PyTorch is not in this environment. Do Install PyTorch again.
  4. In VS Code, select the .venv interpreter, 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.

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.

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.

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.

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) then torch.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-router with .venv, then pip install torch==2.14.1 (with the CPU --index-url on Windows and Linux), numpy==2.5.3, pandas==3.0.6 and ipykernel.
  • get_data.py downloads the tickets and ticketnet.py, checks them, and ends with Ready: train 4696 tickets, valid 600, test 600.
  • A graphics card is optional. Detect it with torch.cuda.is_available() or torch.backends.mps.is_available(); every number in this course is a CPU number.
  • Reset: delete .venv and make it again, or run python 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