The Mathematics of Large Language Models

Setup & Installation

Please complete this page before the first lecture. It should take about 5 minutes. If anything fails, bring your laptop 15 minutes early on day 1 and we will fix it together.

We run the whole course in Google Colab: it works in the browser, on any laptop (including tablets and Chromebooks), there is nothing to install, and it gives you free GPU time that will be handy for training in Steps 6–8.

Google Colab

  1. You need a Google account.
  2. Go to colab.research.google.com and choose New notebook.
  3. In the first cell, type 2 + 2, press Shift+Enter, and confirm you get 4. That’s a Jupyter notebook: cells of Python you run one at a time. All course materials are distributed as notebooks.
  4. Paste the verification script below into a cell and run it. Everything the course needs (PyTorch, NumPy, matplotlib) is pre-installed on Colab.
  5. Optional, for Steps 6–8: Runtime → Change runtime type → T4 GPU gives you a free GPU. Re-run the verification script and it should report the GPU.

Colab disconnects idle sessions and wipes their files, so save notebooks to your Google Drive (Colab does this by default) and re-download data files at the top of each notebook — our notebooks always include the download cell.

Verify your setup

Run this in a Colab cell. It checks everything the course needs:

import sys
print("Python:", sys.version.split()[0])
assert sys.version_info >= (3, 10), "Need Python 3.10+"

import torch, numpy, matplotlib
print("PyTorch:", torch.__version__)
print("NumPy:", numpy.__version__)

# A tiny tensor computation with gradients — the engine of the whole course:
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
loss = (x ** 2).sum()
loss.backward()
assert torch.allclose(x.grad, 2 * x.detach())
print("Autograd: OK  (d/dx sum(x^2) = 2x  ✓)")

device = ("cuda" if torch.cuda.is_available()
          else "mps" if getattr(torch.backends, "mps", None)
                        and torch.backends.mps.is_available()
          else "cpu")
print("Compute device:", device, "(cpu is fine for this course)")
print("\nAll good — see you at Lecture 1.")

Expected output ends with All good — see you at Lecture 1. If you get an error you can’t decipher, email it to the instructor or bring it to class.

Python primer

There is a Python primer available (~90 minutes), covering:

It is not required — you can learn as you go. The project introduces everything else (NumPy arrays, PyTorch tensors, classes) as it is needed, and each construct is explained the first time it appears.

Another good self-contained refresher: the official Python tutorial, sections 3–5.