CS 601.471/671  ·  NLP: Self-supervised Models  ·  Fall 2026

Handout #1 — Foundations & Prerequisites

Session 1 (Tue, Sept 1)  ·  Slides: Course Overview  ·  Course page

This handout has two jobs. The first is to let you find out — honestly, and in private — whether you have the background this course assumes. The second is to take the ideas Session 1 could only put on a slide and actually work through them.

Assigned papers
None. This handout is the reading for Session 1.
Time
45–75 minutes, depending on how much of §1 you already know cold.
By the end you should be able to
  • compute inner products, matrix–vector products and quadratic forms, and say what each costs;
  • state the probability chain rule and use it to factor a joint distribution;
  • take gradients of the handful of expressions that keep reappearing in this course;
  • explain what makes a learning problem self-supervised, and write down the objective;
  • use the words self-supervised, pretrained, foundation model and LLM without hand-waving.
Where §1 comes from
In previous years these exercises were Homework 1: Background Review, and they were graded. This year the course has no graded homework — with current generative models, a take-home problem set no longer measures anything about you. So the problems are still here, the answers ship with them, and nobody is watching. The catch is that quizzes happen in class, on paper, and they assume this material. Working through §1 now is entirely in your own interest.
A calibration note, repeated from the old homework You will each have different strengths, so don't worry if some parts of §1 are a struggle. But if §1 is uniformly hard — if most items are unfamiliar rather than merely rusty — treat that as an early signal that you may not be ready for this course right now. That is useful information in week one, and much less painful than discovering it in week eight. The prerequisites section of the course page lists the courses that build this background.

1Prerequisite self-check

Five areas. Each opens with a short recap of why this course needs it — which the old homework did not include, because a lecture came before it — and then the problems, with answers you reveal one at a time. Try each before you look.

1.1  Linear algebra

Essentially everything in this course is a matrix multiplication with a nonlinearity somewhere. A transformer layer is a stack of them; attention is a matrix of inner products; a "hidden state" is a vector; "embedding dimension" is that vector's length. If matrix shapes are not automatic for you, the architecture sections later in the semester will feel like notation rather than mechanism.

One habit worth forming now: read every expression as shapes first. Model activations in this course almost always have shape (batch × sequence × dim), and the single most common bug in student code — in any year, at any level — is a silently broadcast dimension. NumPy and PyTorch will happily add a (32, 1, 768) to a (32, 128, 768) and give you something that trains badly instead of crashing.

Exercise 1.1a — basic operations HW1 §1.1

Let

$$ \alpha = 2,\quad x = \begin{bmatrix}0\\1\\2\end{bmatrix},\quad y = \begin{bmatrix}3\\2\\4\end{bmatrix},\quad z = \begin{bmatrix}1\\2\\-1\end{bmatrix},\quad A = \begin{bmatrix}3&2&2\\1&3&1\\1&1&3\end{bmatrix}. $$

Write $x_i$ for element $i$ of $x$. Evaluate:

  1. $\sum_{i=1}^n x_i y_i$  (inner product)
  2. $\sum_{i=1}^n x_i z_i$  (inner product of orthogonal vectors)
  3. $\alpha(x + y)$
  4. $\lVert x \rVert$  (Euclidean norm)
  5. $x^\top$
  6. $Ax$  (matrix–vector product)
  7. $x^\top A x$  (quadratic form)
Answers
  1. $0\cdot 3 + 1\cdot 2 + 2\cdot 4 = \mathbf{10}$
  2. $0\cdot 1 + 1\cdot 2 + 2\cdot(-1) = \mathbf{0}$. The two vectors are orthogonal, which is exactly what a zero inner product means — and it is why we will use the dot product as a similarity score.
  3. $2\begin{bmatrix}3\\3\\6\end{bmatrix} = \begin{bmatrix}\mathbf{6}\\\mathbf{6}\\\mathbf{12}\end{bmatrix}$
  4. $\sqrt{0^2+1^2+2^2} = \sqrt{5} \approx 2.236$
  5. $\begin{bmatrix}0&1&2\end{bmatrix}$ — a row vector. The transpose does not change the data, only which axis it lies along; that distinction is what makes $x^\top y$ a scalar and $x y^\top$ a matrix.
  6. $Ax = \begin{bmatrix}3\cdot0+2\cdot1+2\cdot2\\1\cdot0+3\cdot1+1\cdot2\\1\cdot0+1\cdot1+3\cdot2\end{bmatrix} = \begin{bmatrix}\mathbf{6}\\\mathbf{5}\\\mathbf{7}\end{bmatrix}$
  7. $x^\top(Ax) = \begin{bmatrix}0&1&2\end{bmatrix}\begin{bmatrix}6\\5\\7\end{bmatrix} = 0 + 5 + 14 = \mathbf{19}$
Exercise 1.1b — which rules actually hold? HW1 §1.2

Let $\{x, y, z\}$ be $n \times 1$ column vectors, $\{A, B, C\}$ be $n \times n$ real matrices, and $I$ the identity. True or false in general?

  1. $x^\top y = \sum_{i=1}^n x_i y_i$
  2. $x^\top x = \lVert x \rVert^2$
  3. $x^\top x = x x^\top$
  4. $(x-y)^\top(y-x) = \lVert x \rVert^2 - 2x^\top y + \lVert y \rVert^2$
  5. $AB = BA$
  6. $A(B + C) = AB + AC$
  7. $(AB)^\top = B^\top A^\top$
  8. $x^\top A y = y^\top A^\top x$
  9. $A^\top A = I$ if the columns of $A$ are orthonormal
Answers
  1. True — this is the definition.
  2. True — and the reason $\lVert\cdot\rVert^2$ is so much more pleasant to differentiate than $\lVert\cdot\rVert$.
  3. False, and not even the same type: the left side is a $1\times1$ scalar, the right side is an $n \times n$ matrix (the outer product). Shape-checking catches this immediately.
  4. False. Note $(x-y)^\top(y-x) = -(x-y)^\top(x-y) = -\lVert x-y\rVert^2$, so the right side is off by a sign: the correct identity is $\lVert x-y \rVert^2 = \lVert x \rVert^2 - 2x^\top y + \lVert y \rVert^2$.
  5. False — matrix multiplication does not commute. This is why the order of layers matters.
  6. True — distributivity holds.
  7. True — the transpose reverses the order. You will use this constantly when deriving backward passes.
  8. True. Both sides are the same scalar: transposing a $1\times1$ matrix leaves it alone, and $(x^\top A y)^\top = y^\top A^\top x$.
  9. True — entry $(i,j)$ of $A^\top A$ is the inner product of columns $i$ and $j$, which is $1$ when $i=j$ and $0$ otherwise precisely when the columns are orthonormal.
Exercise 1.1c — invertibility and diagonalization HW1 §1.3

Let $B = \begin{bmatrix}1&-1&0\\-1&2&-1\\0&-1&1\end{bmatrix}$.

  • Is $B$ invertible? If so, find $B^{-1}$.
  • Is $B$ diagonalizable? If so, give a diagonalization.
Answers

Not invertible. $\det(B) = 0$ — and you can see why without computing it: the three rows sum to the zero vector, so they are linearly dependent, and $B\mathbf{1} = 0$.

Diagonalizable anyway. Singularity and diagonalizability are independent properties: the first asks whether an eigenvalue is $0$, the second whether there are enough independent eigenvectors. The eigenvalues are $\lambda_1 = 3,\ \lambda_2 = 1,\ \lambda_3 = 0$, with eigenvectors

$$ v_1 = \begin{bmatrix}1\\-2\\1\end{bmatrix},\quad v_2 = \begin{bmatrix}1\\0\\-1\end{bmatrix},\quad v_3 = \begin{bmatrix}1\\1\\1\end{bmatrix}. $$

Three distinct eigenvalues force three independent eigenvectors, so with $P = \begin{bmatrix}v_1 & v_2 & v_3\end{bmatrix}$ we get $P^{-1} B P = D = \operatorname{diag}(3, 1, 0)$.

($B$ is symmetric, so the spectral theorem guaranteed a real orthogonal diagonalization before we computed anything — the eigenvectors above are mutually orthogonal.)

1.2  Probability

This course is, at bottom, about one probability distribution: a distribution over sequences of text. Everything else — architecture, training, decoding, evaluation — is machinery for representing it, fitting it, or sampling from it. So probability is not a prerequisite you use once and set aside.

Four ideas do most of the work, and only one of them appeared in the old homework.

Joint, conditional, marginal. $p(a, b)$ is the probability that both happen; $p(a \mid b) = p(a,b)/p(b)$ is the probability of $a$ once you know $b$; and $p(a) = \sum_b p(a, b)$ marginalizes $b$ away. "Predict the next word given the previous ones" is a conditional, and that is the only reason any of this is tractable.

The chain rule. Any joint distribution factors into a product of conditionals, with no assumptions whatsoever:

$$ p(x_1, x_2, \ldots, x_T) \;=\; \prod_{t=1}^{T} p(x_t \mid x_1, \ldots, x_{t-1}) \;=\; \prod_{t=1}^{T} p(x_t \mid x_{<t}). $$

This is the single most important equation in the course. §2 is essentially a discussion of its consequences, so make sure you can derive it (apply $p(a,b) = p(a \mid b)\,p(b)$ repeatedly) rather than just recognize it.

Categorical distributions. A distribution over a finite vocabulary of size $V$ is a vector of $V$ non-negative numbers summing to $1$. A neural network produces unnormalized logits $s \in \mathbb{R}^V$, and softmax turns them into such a vector: $p_i = e^{s_i} / \sum_j e^{s_j}$. Note what softmax is invariant to — adding a constant to every logit changes nothing — and that it can never output an exact zero.

Why log space. Multiply a few hundred numbers each around $10^{-4}$ and a 64-bit float underflows to exactly $0$. Sequences are long, so we always work with $\log p$: products become sums, and the numbers stay in a sane range. This is not a numerical footnote — it is why the loss you will write is a sum of log-probabilities rather than a product of probabilities.

Exercise 1.2a — basic probability HW1 §2.1
  1. You are offered a game. Your opponent rolls two ordinary 6-sided dice. If the difference between the rolls is at least 3, you win \$15. If you roll doubles, you win \$5. Otherwise nothing. What is a fair price for one ticket?
  2. Events $A$ and $B$ are mutually exclusive, so $P(A, B) = 0$. If $P(A) = 0.4$ and $P(A \cup B) = 0.95$, what is $P(B)$?
  3. Same numbers, but now assume $A$ and $B$ are independent instead of mutually exclusive. What is $P(B)$?
Answers

1. Count outcomes over the 36 equally likely pairs. Difference $\geq 3$: the pairs $(1,4),(1,5),(1,6),(2,5),(2,6),(3,6)$ and their six mirrors, so $12$ outcomes. Doubles: $6$ outcomes. These are disjoint, so

$$ \mathbb{E}[X] = \tfrac{12}{36}\cdot 15 + \tfrac{6}{36}\cdot 5 = 5 + \tfrac{5}{6} = \tfrac{35}{6} \approx \mathbf{\$5.83}. $$

A fair price is the expected payout: it is the price at which the game has zero expected value to either side.

2. Inclusion–exclusion: $P(A \cup B) = P(A) + P(B) - P(A,B)$, so $0.95 = 0.4 + P(B) - 0 \Rightarrow P(B) = \mathbf{0.55}$.

3. Independence means $P(A,B) = P(A)P(B) = 0.4\,P(B)$, so

$$ 0.95 = 0.4 + P(B) - 0.4P(B) = 0.4 + 0.6\,P(B) \;\Longrightarrow\; P(B) = \frac{0.55}{0.6} = \frac{11}{12} \approx \mathbf{0.9167}. $$

Worth pausing on: the same two marginals are consistent with very different $P(B)$ depending on the dependence structure you assume. "Independent" is a modelling assumption, not a default.

Exercise 1.2b — expectation and variance HW1 §2.2

Two coins: $C_1$ comes up heads with probability $0.3$, $C_2$ with probability $0.9$. Repeat the following three times: choose a coin uniformly at random, then flip it once. Let $X$ be the total number of heads.

  1. What is $\mathbb{E}[X]$?
  2. What is $\operatorname{Var}[X]$?
  3. You earn $Y = \dfrac{1}{2 + X}$ dollars. What is $\mathbb{E}[Y]$?
Answers

First, collapse the two-stage experiment. Each trial independently produces a head with probability $p = 0.5(0.3) + 0.5(0.9) = 0.6$, so $X \sim \text{Binomial}(3, 0.6)$. The coin choice is re-randomized every trial, which is what keeps the trials independent — if you picked one coin once and flipped it three times, the trials would be correlated and the variance would be larger.

1. $\mathbb{E}[X] = np = 3(0.6) = \mathbf{1.8}$.

2. $\operatorname{Var}[X] = np(1-p) = 3(0.6)(0.4) = \mathbf{0.72}$. Via the definition, as a check: $\mathbb{E}[X^2] = \sum_k \binom{3}{k}p^k(1-p)^{3-k}k^2 = 3.96$, and $3.96 - 1.8^2 = 3.96 - 3.24 = 0.72$. ✓

3. $Y$ is a nonlinear function of $X$, so you must average over the distribution — $\mathbb{E}[1/(2+X)] \neq 1/(2 + \mathbb{E}[X])$. With $P(X=k) = \binom{3}{k}(0.6)^k(0.4)^{3-k}$:

$k$$P(X=k)$$1/(2+k)$product
0$0.4^3 = 0.064$$1/2$$0.0320$
1$3(0.6)(0.4)^2 = 0.288$$1/3$$0.0960$
2$3(0.6)^2(0.4) = 0.432$$1/4$$0.1080$
3$0.6^3 = 0.216$$1/5$$0.0432$
$$ \mathbb{E}[Y] = 0.0320 + 0.0960 + 0.1080 + 0.0432 = \frac{349}{1250} = \mathbf{0.2792}. $$

(For comparison, $1/(2 + \mathbb{E}[X]) = 1/3.8 \approx 0.2632$ — close, but not equal. Jensen's inequality tells you which way the gap must go, since $1/(2+x)$ is convex.)

Erratum vs. the old homework PDF The distributed solutions gave $\mathbf{0.355}$ here. That line dropped the $k = 0$ term and mis-multiplied the rest; $0.2792$ is correct. Its variance line was also garbled ($6.552 - 1.8^2$), though the final $0.72$ was right. If you worked from the old PDF and disagreed with it, you were right.
Exercise 1.2c — a variance paradox? HW1 §2.3

For i.i.d. random variables $X_1, \ldots, X_n$ with distribution $F$ and variance $\sigma^2$, we know $\operatorname{Var}[X_1 + \cdots + X_n] = n\sigma^2$. But if $X \sim F$, then $\operatorname{Var}[X + X] = \operatorname{Var}[2X] = 4\sigma^2$. With $n = 2$ these disagree. Is there a contradiction? Explain.

Answer

No. The identity $\operatorname{Var}[\sum X_i] = \sum \operatorname{Var}[X_i]$ requires the summands to be uncorrelated. In general

$$ \operatorname{Var}[X_1 + X_2] = \operatorname{Var}[X_1] + \operatorname{Var}[X_2] + 2\operatorname{Cov}[X_1, X_2]. $$

For two independent draws the covariance is $0$ and you get $2\sigma^2$. But $X + X$ is not two draws — it is one draw used twice, so $\operatorname{Cov}[X, X] = \operatorname{Var}[X] = \sigma^2$ and the formula gives $\sigma^2 + \sigma^2 + 2\sigma^2 = 4\sigma^2$. Consistent. A random variable is emphatically not independent of itself.

This distinction is not a trick question. "Are these two samples independent?" is exactly the question behind duplicated training data, contaminated evaluation sets, and why averaging $k$ samples from one model reduces variance far less than you would hope.

1.3  Calculus

Training a model means computing $\nabla_\theta \mathcal{L}$ and stepping downhill. PyTorch computes the gradient for you, but "autograd handles it" is not the same as understanding it: you need derivatives to read a paper's loss function, to recognize a vanishing or exploding gradient, and to know why some architectural choices train and others do not.

The one mechanism to have at your fingertips is the chain rule: if $z = f(g(x))$ then $\frac{dz}{dx} = f'(g(x))\, g'(x)$. Backpropagation is nothing more than this rule applied to a composition of many functions, evaluated right-to-left so that each intermediate is reused instead of recomputed. When you call loss.backward(), PyTorch walks the graph it recorded during the forward pass and accumulates $\partial \mathcal{L} / \partial \theta$ into .grad for every leaf tensor with requires_grad=True. Nothing more mysterious than that.

Exercise 1.3a — one-variable derivatives HW1 §3.1
  1. $f(x) = 4x^2 - 3x + 1$
  2. $f(x) = x(1 - x)$
  3. Let $p(x) = \dfrac{1}{1 + e^{-x}}$ for $x \in \mathbb{R}$. Differentiate $f(x) = x - \log p(x)$ and simplify the result in terms of $p$.

Throughout this course $\log$ means the natural logarithm. It may help to note that $p(x) = 1 - p(-x)$.

Answers

1. $f'(x) = 8x - 3$.

2. $f(x) = x - x^2$, so $f'(x) = 1 - 2x$.

3. The sigmoid satisfies $p'(x) = p(x)\bigl(1 - p(x)\bigr)$ — worth memorizing. Then

$$ f'(x) = 1 - \frac{p'(x)}{p(x)} = 1 - \bigl(1 - p(x)\bigr) = \mathbf{p(x)}. $$
Why this exercise is here That $f$ is not arbitrary. $-\log p(x)$ is the logistic loss when the true label is $1$, and $x - \log p(x) = -\log p(-x)$ is the loss when the label is $0$. So you have just derived a gradient of the form (predicted probability) − (true label) — the same shape you will meet again as the gradient of softmax cross-entropy, where it becomes $\hat{p} - y$ over the whole vocabulary. Remembering that one fact will demystify a lot of later derivations.
Exercise 1.3b — gradients HW1 §3.2

Compute $\nabla f(\mathbf{x})$ for each:

  1. $f(\mathbf{x}) = x_1^2 + e^{x_2}$,  $\mathbf{x} \in \mathbb{R}^2$
  2. $f(\mathbf{x}) = e^{x_1 + x_2 x_3}$,  $\mathbf{x} \in \mathbb{R}^3$
  3. $f(\mathbf{x}) = a^\top \mathbf{x}$,  $\mathbf{x}, a \in \mathbb{R}^2$
  4. $f(\mathbf{x}) = \mathbf{x}^\top A \mathbf{x}$ with $A = \begin{bmatrix}2 & -1\\-1 & 1\end{bmatrix}$
  5. $f(\mathbf{x}) = \tfrac{1}{2}\lVert \mathbf{x} \rVert^2$,  $\mathbf{x} \in \mathbb{R}^d$

Hint: for 4 and 5, write the expression out as a summation first.

Answers
  1. $\begin{bmatrix}2x_1 & e^{x_2}\end{bmatrix}$
  2. $e^{x_1 + x_2x_3}\begin{bmatrix}1 & x_3 & x_2\end{bmatrix}$ — one chain rule, three partial derivatives of the exponent.
  3. $a^\top$. Linear functions have constant gradients; this is the vector version of $\frac{d}{dx}(ax) = a$.
  4. In general $\nabla \mathbf{x}^\top A \mathbf{x} = \mathbf{x}^\top (A + A^\top)$. Here $A$ is symmetric, so this is $2\mathbf{x}^\top A = \begin{bmatrix}4x_1 - 2x_2 & -2x_1 + 2x_2\end{bmatrix}$.
  5. $\mathbf{x}^\top$. This is why the $\tfrac{1}{2}$ is conventionally there — it cancels the $2$ — and why $L_2$ regularization contributes a term proportional to the weights themselves ("weight decay").

(Whether a gradient is a row or a column vector is a convention. Pick one and be consistent; the shapes will tell you if you have slipped.)

Exercise 1.3c — prove the sigmoid identity Quiz 1, sp2025

Exercise 1.3a used the fact that $\sigma'(z) = \sigma(z)\bigl(1-\sigma(z)\bigr)$ without proving it. Prove it, for $\sigma(z) = 1/(1+e^{-z})$.

Answer

Write $\sigma(z) = (1 + e^{-z})^{-1}$ and apply the chain rule:

$$ \frac{d\sigma}{dz} = -(1+e^{-z})^{-2}\cdot(-e^{-z}) = \frac{e^{-z}}{(1+e^{-z})^{2}}. $$

Now rewrite $e^{-z}$ in terms of $\sigma$. From $\sigma = 1/(1+e^{-z})$ we get $1 + e^{-z} = 1/\sigma$, so $e^{-z} = \dfrac{1-\sigma}{\sigma}$. Substituting:

$$ \frac{d\sigma}{dz} = \frac{(1-\sigma)/\sigma}{(1/\sigma)^{2}} = \frac{1-\sigma}{\sigma}\cdot\sigma^{2} = \sigma(z)\bigl(1-\sigma(z)\bigr). $$

The trick worth keeping is the second step: expressing the derivative in terms of the function's own output. It is why a sigmoid layer's backward pass needs only the value it already computed going forward, and the same trick works for $\tanh$ and for softmax.

Exercise 1.3d — Jacobians Quiz 1, sp2024 & sp2025

A gradient is what you get when a function returns one number. When it returns a vector, the derivative is a matrix: the Jacobian $J_{ij} = \partial f_i / \partial x_j$ — row $i$ is the gradient of output $i$. Every layer of a neural network is such a function, so this is the object backpropagation actually multiplies.

  1. Compute the Jacobian of $f(x,y,z) = \bigl[\,x^2 + y,\; xy,\; z^2/y\,\bigr]$, then evaluate it at $(x,y,z) = (1, \tfrac12, -1)$.
  2. Let $\sigma$ be applied element-wise to a vector, $\sigma(z) = [\sigma(z_1), \ldots, \sigma(z_d)]$. What does its Jacobian look like, and why does that shape matter computationally?
Answers

1. Differentiate each component with respect to each variable:

$$ J_f(x,y,z) = \begin{bmatrix} 2x & 1 & 0 \\ y & x & 0 \\ 0 & -z^2/y^2 & 2z/y \end{bmatrix}, \qquad J_f\!\left(1, \tfrac12, -1\right) = \begin{bmatrix} 2 & 1 & 0 \\ \tfrac12 & 1 & 0 \\ 0 & -4 & -4 \end{bmatrix}. $$

Check the two entries that are easy to get wrong: at $y = \tfrac12$, $-z^2/y^2 = -1/\tfrac14 = -4$, and $2z/y = -2/\tfrac12 = -4$.

2. Diagonal, with $\sigma(z_i)\bigl(1-\sigma(z_i)\bigr)$ down the diagonal and zeros everywhere else — because output $i$ depends on input $i$ alone.

That matters enormously. A general $d \times d$ Jacobian costs $O(d^2)$ to store and $O(d^2)$ to multiply by (Handout §1.4). A diagonal one is $d$ numbers, and multiplying by it is an element-wise product — $O(d)$. So an element-wise activation is essentially free in the backward pass too, which is a large part of why networks are built from element-wise nonlinearities rather than something that mixes coordinates.

Softmax is the exception: it normalizes across coordinates, so its Jacobian is not diagonal.

1.4  Algorithms and asymptotic cost

Complexity is not an academic exercise in this course — it is the reason the field looks the way it does. Attention costs $O(T^2 d)$ in sequence length $T$; that single fact drives context-length limits, the entire efficient-attention literature, and much of the hardware discussion later in the semester. If you can count the cost of a matrix multiplication, you can read those papers.

Exercise 1.4 — big-O HW1 §4
  1. Merge sort on a list of $n$ numbers.
  2. Finding the third-largest element of an unsorted list of $n$ numbers.
  3. Finding the smallest element greater than $0$ in a sorted list of $n$ numbers.
  4. Looking up the value for a key in a hash table with $n$ scalar entries.
  5. Computing $A\mathbf{x}$ where $A$ is $n \times d$ and $\mathbf{x}$ is $d \times 1$.
  6. Computing $\mathbf{x}^\top A \mathbf{x}$ where $A$ is $d \times d$.
  7. Computing $AB$ where $A$ is $m \times n$ and $B$ is $n \times d$.
Answers
  1. $O(n \log n)$ — $\log n$ levels of merging, $O(n)$ work per level.
  2. $O(n)$. A single pass keeping the top three; no sort needed. (In general, the $k$-th largest is $O(n)$ for fixed $k$.)
  3. $O(\log n)$ by binary search.
  4. $O(1)$ on average, $O(n)$ worst case when every key collides.
  5. $O(nd)$ — $n$ output entries, each a $d$-term inner product.
  6. $O(d^2)$ — dominated by the $A\mathbf{x}$ product; the final inner product is only $O(d)$.
  7. $O(mnd)$ — $md$ output entries, each an $n$-term inner product.
Carry this forward Items 5–7 are the entire cost model for a neural network. A linear layer mapping $d_{\text{in}} \to d_{\text{out}}$ over a batch of $B$ sequences of length $T$ costs $O(B\,T\,d_{\text{in}}\,d_{\text{out}})$. Self-attention additionally forms a $T \times T$ matrix of inner products, hence $O(T^2 d)$ — quadratic in sequence length. When you later read that some method is "linear attention", that is the exponent it is talking about.

1.5  Python and PyTorch

All code in this course is Python with PyTorch. If you know another language well you can pick up Python quickly; PyTorch itself is a smaller surface than it looks, because a model is just a Python class and a tensor is just an array that remembers what was done to it.

Below is the minimum you should be able to read without looking anything up. Run it — a CPU-only install is enough for every line here.

Tensors: shape, dtype, device

import torch

x = torch.tensor([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0]])          # from a nested list
print(x.shape, x.dtype, x.device)            # torch.Size([2, 3]) torch.float32 cpu

torch.zeros(2, 3); torch.ones(2, 3)          # filled
torch.randn(2, 3)                            # standard normal
torch.arange(6).reshape(2, 3)                # 0..5, viewed as 2x3

# Shape manipulation you will use constantly:
x.T                       # (3, 2)  transpose
x.reshape(3, 2)           # same data, new shape
x.unsqueeze(0).shape      # (1, 2, 3)  add an axis -- e.g. a batch dimension
x.squeeze(0).shape        # remove size-1 axes

Element-wise ops, matmul, and broadcasting

a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([10.0, 20.0, 30.0])

a + b                     # element-wise; same as torch.add(a, b)
a * b                     # element-wise product -- NOT a matrix product
a @ b                     # inner product -> tensor(140.)

W = torch.randn(4, 3)     # a linear layer's weights
W @ a                     # (4,) -- the matrix-vector product from Exercise 1.1a

# Broadcasting: a size-1 (or missing) axis is stretched to match.
h = torch.randn(32, 128, 3)      # (batch, sequence, dim)
h + a                            # a is (3,) -> broadcast over batch and sequence: fine
# h + torch.randn(128)           # RuntimeError -- shapes 3 and 128 are incompatible

# The dangerous case: this does NOT error, and is almost never what you meant.
bad = h + torch.randn(32, 1, 3)  # silently added the same vector to every position

Softmax and cross-entropy

import torch.nn.functional as F

logits = torch.tensor([2.0, 1.0, 0.1])       # unnormalized scores over a 3-word vocabulary
p = F.softmax(logits, dim=-1)                # tensor([0.6590, 0.2424, 0.0986]) -- sums to 1
print(p.sum())                               # tensor(1.)

# Softmax is shift-invariant: adding a constant to every logit changes nothing.
torch.allclose(F.softmax(logits + 100, dim=-1), p)     # True

# Prefer log_softmax over log(softmax(.)) -- it is the numerically stable version.
logp = F.log_softmax(logits, dim=-1)

# Cross-entropy = negative log-probability of the correct class.
target = torch.tensor([0])                   # the true next word is index 0
loss = F.cross_entropy(logits.unsqueeze(0), target)
print(loss, -logp[0])                        # the same number: 0.4170
The one identity to internalize F.cross_entropy(logits, target) is $-\log p(\text{target})$, with the softmax folded in. So minimizing cross-entropy over a corpus is maximizing $\sum_t \log p(x_t \mid x_{<t})$ — the log of the chain-rule product from §1.2. The loss function and the probability model are the same object seen from two sides. §2 makes this explicit.

Autograd

x = torch.tensor([3.0], requires_grad=True)

y = 4 * x**2 - 3 * x + 1        # Exercise 1.3a.1
y.backward()                    # walk the recorded graph, accumulate d y / d x
print(x.grad)                   # tensor([21.]) -- because 8(3) - 3 = 21

# .grad ACCUMULATES. Forgetting to reset it is a classic silent bug:
x.grad.zero_()                  # in a training loop: optimizer.zero_grad()
Exercise 1.5a — softmax, and what it ignores Quiz 1, sp2025
  1. Compute $\operatorname{softmax}([2, 1, 0])$. No need to simplify.
  2. Now compute $\operatorname{softmax}([10002, 10001, 10000])$. How does it compare to the previous answer, and why?
  3. Let $X' = \operatorname{softmax}(X)$ for a vector $X$ of length $n$. What is $\sum_{x' \in X'} x'$?
Answers

1. Dividing through by $e^0 = 1$:

$$ \left[\frac{e^2}{1 + e^1 + e^2},\; \frac{e^1}{1 + e^1 + e^2},\; \frac{1}{1 + e^1 + e^2}\right] \approx [0.665,\; 0.245,\; 0.090]. $$

2. Identical. Softmax is shift-invariant: adding a constant $c$ to every logit multiplies numerator and denominator by $e^{c}$, which cancels. Since $[10002, 10001, 10000] = [2,1,0] + 10000$, the outputs match exactly.

This is not a curiosity — it is a numerical necessity. $e^{10002}$ overflows a 64-bit float, so every real implementation subtracts $\max_i z_i$ from the logits first, which changes nothing mathematically and everything practically. It also means logits are only meaningful up to an additive constant: a model that outputs $[2,1,0]$ and one that outputs $[10002,10001,10000]$ are the same model.

3. Exactly $1$, by construction — the denominator is the sum of the numerators. Softmax always returns a valid distribution, which is why an output that is negative or exceeds 1 means the softmax is missing, not broken.

Exercise 1.5b — cross-entropy at the start of training Quiz 1, sp2024 & sp2025

You evaluate a classifier on 20 instances. It assigns equal probability to each of 10 classes on every instance. What is the cross-entropy loss, averaged over the instances?

Choose from: $-\log(10)$, $-\log(20)$, $-0.1\log(10)$, $-0.2\log(10)$, $-\log(0.1)$, $-\log(0.2)$, $-10\log(0.1)$, $-20\log(0.1)$.

Answer

$\mathbf{-\log(0.1)} \approx 2.303$ nats.

Cross-entropy on one example is $-\log p(\text{correct class})$ — see the identity above. Ten classes at equal probability means $p = 0.1$ for whichever class is correct, so each instance contributes $-\log(0.1)$. The question asks for the average, and the average of 20 identical numbers is that number. $-20\log(0.1)$ is the sum, and it is the trap: read whether a loss is summed or averaged before you compare it to anything.

Worth memorizing as a sanity check: an untrained classifier over $V$ classes starts at a loss of about $\log V$. If your training run begins somewhere else, something is wrong before the first gradient step — with $V = 50{,}000$ you should see roughly $\log(50000) \approx 10.8$ nats. This is the same fact that will reappear in Handout #2 as "a uniform model has perplexity $V$".

Exercise 1.5c — reading a training loop Quiz 1, sp2024 & sp2025
class FeedforwardNN(nn.Module):
    def __init__(self, input_size):
        super().__init__()
        self.hidden = nn.Linear(input_size, 5, bias=False)
        self.output = nn.Linear(5, 2, bias=False)

    def forward(self, x):
        x = F.relu(self.hidden(x))
        return self.output(x)
  1. With input_size = 3, how many trainable parameters does this network have?
  2. If x has shape (4, 3), what shape does forward return? What shape is the resulting cross-entropy loss?
  3. A training loop calls three of loss.backward(), optimizer.step() and optimizer.zero_grad() each iteration. What is the correct order, and what breaks if you omit zero_grad?
Answers

1. 25. The first layer's weight is $5 \times 3 = 15$ numbers, the second's is $2 \times 5 = 10$, and bias=False means no bias vectors. ReLU has no parameters.

2. (4, 2)nn.Linear maps the last dimension and leaves the leading ones alone, so a batch of 4 gives 4 rows of 2 logits. The loss is a scalar: cross_entropy reduces over the batch by default (reduction="mean").

3. zero_grad()backward()step(). (Equivalently, clearing at the end of the previous iteration.) The one ordering that is definitely wrong is step() before backward() — you would be applying last iteration's gradients.

Omitting zero_grad does not crash. PyTorch accumulates into .grad, so each step would use the sum of every gradient seen so far: the effective step size grows without bound and training diverges for no visible reason. This is the silent-failure mode flagged in the autograd snippet above, and it is worth recognizing from the symptom alone.

Not in this handout The old Homework 1 continued into word embeddings, one-hot vs. distributional representations, Dataset/DataLoader, nn.Module, and training a sentiment classifier on IMDB. That is a genuine lecture's worth of material and it belongs with the sessions that cover it, not in a background review — expect it in a later handout. For now, tensors, matmul, softmax, cross-entropy and .backward() are enough.

2What "self-supervised" actually means

The slides assert that self-supervised models "are predictive models of the world, learned from cheaply available unlabeled data." That sentence is doing a lot of work. Here is what is underneath it.

2.1  The label problem, and the trick that dissolves it

Ordinary supervised learning needs pairs $(x, y)$: a review and its sentiment, an image and its class, a sentence and its translation. Humans produce $y$, which makes it the scarce ingredient. Annotated datasets top out in the millions of examples, cost real money, and only ever teach the one task you annotated for.

Self-supervision sidesteps this entirely: construct $y$ from $x$ itself. Take a sentence, hide part of it, and ask the model to reproduce the hidden part. Nobody annotated anything, so every sentence ever written is training data — and, as the widget below makes concrete, one sentence yields not one example but one per position.

Objective constructor Type any sentence and toggle the objective. Count the supervised examples that appear out of unlabeled text.

Both objectives in that widget manufacture labels from raw text, but they are not interchangeable. The causal one defines a probability distribution over sequences, which means you can generate from it. The masked one sees both sides of each blank, which tends to produce better representations for classification but leaves you with no coherent way to sample text. That difference is why the generative branch is the one that grew into the models you use daily, and we will return to it in Sessions 12–13.

2.2  Writing down the objective

Take the causal case. We want to model $p(x_1, \ldots, x_T)$, a distribution over entire documents — an object of astronomical size, since there are $V^T$ possible documents. The chain rule from §1.2 rescues us:

$$ p(x_1, \ldots, x_T) = \prod_{t=1}^{T} p(x_t \mid x_{<t}). $$

No approximation has been made. But the right-hand side is something we can actually build: each factor is a distribution over one vocabulary of size $V$, so a single network $f_\theta$ that maps a prefix to $V$ logits, applied at every position, defines the whole joint. Fitting it by maximum likelihood over a corpus $\mathcal{D}$ means maximizing

$$ \sum_{x \in \mathcal{D}} \sum_{t=1}^{T} \log p_\theta(x_t \mid x_{<t}), $$

which — by the identity at the end of §1.5 — is exactly minimizing average cross-entropy. Three prerequisites from §1 have now collapsed into one line: the chain rule gave us the factorization, log space made the product a sum, and cross-entropy is the sum's negation.

Where this goes next That one line is the whole of Session 2's agenda. How do you estimate each factor $p_\theta(x_t \mid x_{<t})$ — by counting, or by learning? How do you measure whether the result is any good? And what happens when a factor comes out as exactly zero? Handout #2 takes all three questions up, starting from the simplest possible answer: count.

2.3  Why prediction turns into knowledge

Here is the claim that should still feel surprising: an objective as narrow as "predict the next word" produces systems that write working code and prove theorems. Why should it?

Because for many sequences, the only way to reduce the loss is to model the process that generated them. Consider the last token of each of these:

To predict…the model has to have learned…
The capital of Australia is ___a fact about the world
The keys to the cabinet ___ missingsyntax — agreement across an intervening noun
>>> 17 * 23 = ___arithmetic, or a very large lookup table
for i in range(n):\n total ___the syntax and idiom of Python
She lied, so he no longer ___ hersomething about people and consequences

None of these were trained for. They fall out of one objective applied to text that happens to encode facts, syntax, arithmetic, code and social reasoning. This is the sense in which the slides call these "predictive models of the world": the world left its fingerprints on the text, and pressure to predict the text becomes pressure to reconstruct the fingerprints.

The argument has a limit, and holding both halves at once is the right posture for this course. Reducing prediction loss is correlated with acquiring knowledge, not identical to it. A model can also reduce loss by memorizing surface statistics that happen to work, which is a good first explanation for the failures on slide 6 — confident hallucination, lost constraints, brittleness under small distribution shifts. Capable and brittle are not in tension; they are two consequences of the same objective.

3Why natural language

Self-supervision is not specific to text. The slides show the image version: mask out patches of a photograph and predict them, and generative image models follow. So why is most of this course about language?

Not because language is easier — it is not. Three reasons, in increasing order of interest.

It is available. The web is an enormous, cheap, already-digitized archive of text. This is the mundane reason, and historically the decisive one.

It is how we specify tasks. Because instructions, questions and answers are all text, a model of text can be pointed at a new task by describing it, with no new output layer and no retraining. The interface and the training data are the same medium. Nothing analogous is true of pixels.

It is already a compression of the world. This is the argument worth sitting with. Natural language is our species' accumulated attempt to encode everything we know about the world as efficiently as we can transmit it. A sentence is not raw sensor data; it is the output of a human deciding what mattered enough to say. So text is pre-abstracted — the hard perceptual work of deciding which features are significant has already been done, by people, over millennia.

That is why predicting text is a shortcut to world knowledge in a way that predicting pixels is not. Predict the missing patch of a photograph and you learn about edges, textures and lighting. Predict the missing word of a sentence and you are forced to learn whatever the writer was thinking about.

4How we got here

The slides pose a question worth taking seriously: neural networks are old — the perceptron dates to 1958, backpropagation to the 1980s, and the core architecture of a transformer is a stack of matrix multiplications that any of those researchers would have recognized. So why did almost everything happen in the last fifteen years?

Three forces, and the honest answer is that the algorithms were the smallest of them.

ForceWhat changedWhy it mattered
Data The web turned human text into a corpus by accident. Self-supervision can only exploit scale if the scale exists. The objective was available in 1990; the trillion tokens were not.
Compute GPUs made the one operation these models need — dense matrix multiplication — fast and, per FLOP, steadily cheaper. Training runs that would have taken centuries became weeks. Note from §1.4 that the bottleneck is a single, extremely parallel primitive; the hardware got to specialize.
Algorithms Better optimizers, normalization, initialization, and above all architectures that parallelize across a sequence instead of stepping through it. Mostly these unlocked the other two. The transformer's decisive property is not that it models language better in principle — it is that it uses a GPU well.

The framing to take from this: progress has come less from new ideas about intelligence than from removing the constraints that stopped old ideas from being tried at scale. It is also the reason a course on this material has to spend real time on efficiency and systems — those are not implementation details downstream of the science, they are frequently the thing that decides what is scientifically possible.

5Terminology, used precisely

Five terms get used interchangeably in press coverage and often in papers. They are not synonyms, and being sloppy with them makes it hard to say what a given piece of work actually claims. Each picks out a different kind of property.

TermWhat it describesWhat it does not tell you
Self-supervised model The training signal: labels were derived from the data, not from annotators. Nothing about size, architecture, modality, or what it is used for.
Pretrained model The workflow: this training is a first stage, and something else — fine-tuning, alignment, adaptation — comes after. Whether the first stage was self-supervised at all (ImageNet pretraining was fully supervised).
Foundation model The role: one model that many downstream applications build on, rather than one model per task. Anything technical. It is a claim about an ecosystem, not an architecture — and it is aspirational until the ecosystem exists.
Large language model The modality and scale: text, and a lot of parameters. Where "large" begins — the threshold has moved by three orders of magnitude in a decade and will move again.
Generative model The capability: it defines a distribution you can sample from. How it was trained. A masked language model is self-supervised and pretrained but not usefully generative.

So the categories cross-cut, and the entities we care about tend to satisfy several at once. Meanwhile self-supervision itself sits inside a nesting of older ideas:

Machine learning learn a function from data (1950s–) Deep learning learn the representation too, in many layers (2010s–) Self-supervised learning the labels come from the data itself Large language models this course spends most of its time here
Each box is a strictly narrower commitment than the one containing it. Moving inward adds an assumption; nothing inward is a different field.
Also from the slides Avoid inventing stacked combinations — "frontier foundation model", "large generative pretrained foundation LLM". Each adjective should be doing work. If you cannot say which property a word is asserting, delete it. This applies to your final project write-up.

6Check yourself

The §1 exercises covered the technical prerequisites. These are conceptual, and closer in style to what an in-class quiz asks: short, no computation, but you need to have actually thought about §2–5.

Q1. A colleague says self-supervised learning is "unsupervised learning with extra steps." What is the substantive distinction?

Answer
Self-supervised learning still has a supervised objective — real $(x, y)$ pairs with a real loss and real gradients. The pairs are constructed automatically rather than annotated. Classical unsupervised methods (clustering, PCA) have no target to predict at all. The practical consequence: self-supervision inherits everything we know about optimizing supervised losses, which is why it scaled and clustering did not.

Q1b. Which of these best describes self-supervised learning? (This exact question has appeared on the quiz three years running, so the three-part answer is worth having ready.)

  • ○ Models trained to predict outcomes, but disconnected from real-world tasks.
  • ○ Models trained on large amounts of unlabeled data, learning predictive models of the world, tightly connected to real-world tasks.
  • ○ Models that use large amounts of data but have no predictive capability.
  • ○ Self-learning models that require minimal data for training.
  • ○ A form of supervised learning with less labeled data.
Answer

The second. The definition has three parts, and all three do work:

  1. predictive models of the world — the objective is prediction, §2.2;
  2. trained on large amounts of freely available unlabeled data — the labels are manufactured, §2.1, which is what makes the scale possible;
  3. tightly connected to real-world tasks we care about — §2.4's argument that predicting text forces useful knowledge.

Note why the last option is wrong, since it is the tempting one: self-supervision is not supervised learning with fewer labels. It has as many training targets as there are tokens — it just does not need a human to write them.

Q2. The chain rule factorization $p(x_1,\ldots,x_T) = \prod_t p(x_t \mid x_{<t})$ makes no independence assumptions. So where does a language model's error actually come from?

Answer
From the factors, not the factorization. The identity is exact; the approximation is that a finite network $f_\theta$ with finite training data can only imperfectly represent each conditional $p(x_t \mid x_{<t})$. An n-gram model additionally does assume independence, by truncating the conditioning context to the previous $n-1$ words — that is a second, separate error, and Handout #2 §1.3 takes it up.

Q3. Is every pretrained model a foundation model? Is every foundation model self-supervised?

Answer
No to both. "Pretrained" describes a workflow — a small model pretrained on one narrow corpus and fine-tuned for one task is pretrained but foundational to nothing. "Foundation model" describes a role in an ecosystem, and a model could in principle occupy that role having been trained with human labels (ImageNet-pretrained backbones played roughly that role for years). The terms describe different kinds of property, which is exactly why they are not interchangeable.

Q4. Of the three forces in §4, which do you think is currently the binding constraint — and what would it take to change your mind?

Answer
There is no single right answer here, and the point of the question is the second half. A defensible case: high-quality text is the constraint, since compute keeps growing while the stock of human-written text does not — which is why synthetic data, data curation and multiple epochs are all live research areas. An equally defensible case: compute, since every published scaling result says more of it still helps. What should move you is evidence, e.g. results showing that additional compute on fixed data has stopped paying, or that synthetic data substitutes cleanly for human text. Be able to say what would change your mind; we will revisit this when we cover scaling laws.

Q5. Why can you sample fluent text from a causal language model but not from a masked one, even though both were trained self-supervised on the same corpus?

Answer
The causal model's factors compose into a single valid joint distribution over sequences, by the chain rule — so sampling token by token, feeding each choice back in, draws from that joint. A masked model only ever learned conditionals of the form $p(x_i \mid x_{\neq i})$, each conditioned on both sides. There is no ordering of those conditionals whose product is a coherent joint, so iterative unmasking is a heuristic, not a sampler.

Q6. Slide 6 lists things LLMs still fail at — simple logic puzzles, tracking constraints, confident hallucination. Give an explanation grounded in the training objective rather than in "the model isn't smart enough."

Answer
The objective rewards plausible continuations, not true ones. A fluent, well-formed wrong answer scores well on the training distribution because confident text is what the corpus contains; text is not annotated with whether it was correct. Nothing in $\sum_t \log p_\theta(x_t \mid x_{<t})$ prefers a truthful answer over a probable-looking one, and nothing prefers admitting ignorance — human writing rarely does. Later sessions on alignment are largely about adding the signal the pretraining objective does not contain.

7Optional further reading

Strictly optional — nothing here is assumed in class or on a quiz. Use it if a §1 area was shaky, or if you want to get ahead of Session 2.

If §1 was rusty

If you want to read ahead