The lecture this lesson is built around: attention as a way for each output step to look at all input positions, instead of one fixed summary vector. Watch it first, then build the weighted lookup and its masks here.
COURSE / REAL PRACTICE
Deep learning 3: attention and generation
Lectures 18 to 23 of CMU’s public 11-785 course as builds: attention, a transformer language model, text sampling, a variational autoencoder’s loss, a normalizing flow and a diffusion sampler. Every build is graded against PyTorch or an exact independent check. Unofficial practice material, not affiliated with CMU.
Read the explanation and diagrams, then work through the practice. Your browser profile supplies your saved progress.
What you’ll work toward
Build multi-head attention and a small transformer language model, sample text from a model’s scores, compute a variational autoencoder’s loss and a flow’s exact likelihood, and finish by training and sampling a small diffusion model on a CPU.
Read an explanation, inspect the diagrams and work through the available checks, debugging exercises and builds. Feedback and prerequisite links guide the next step.
Before you start
This course builds on these skills:
Individual lessons can add prerequisites. Your lesson progress shows your actual readiness.
A look inside the first lesson
Attention: queries, keys and values
import numpy as np
rng = np.random.default_rng(0)
for d in (4, 64, 1024):
a, b = rng.normal(size=(20000, d)), rng.normal(size=(20000, d))
print(d, (a * b).sum(axis=1).std().round(1)) # about sqrt(d)
q, keys = np.ones(64), np.array([np.full(64, 0.5), np.full(64, 0.25)])
for scale in (1.0, np.sqrt(64)):
s = keys @ q / scale
w = np.exp(s - s.max()) / np.exp(s - s.max()).sum()
print(round(scale, 1), w.round(3).tolist())Your lesson sequence
Follow the same units and lessons in either view. Prerequisite links and your practice progress stay with each lesson.
Attention and language models
- LESSON 01.01
Attention: queries, keys and values
Replace the single context vector with a weighted lookup: scaled dot-product attention with masks, then several heads in parallel, matched against PyTorch.
Open lesson - LESSON 01.02
A transformer block and a small language model
Assemble attention, residual connections, layer normalization and position information into a decoder block, then check that the model is causal and can learn.
Open lesson - LESSON 01.03
Generating text: temperature, top-k and nucleus sampling
Turn a language model’s scores into text: rescale them with a temperature, cut the tail with top-k or nucleus filtering, draw a token and feed it back in.
Open lesson
Latent variables and exact likelihood
- LESSON 02.01
Autoencoders and the variational autoencoder
From a bottleneck that reconstructs its input to a model you can sample: the reparameterization trick, the closed-form KL term and the negative ELBO.
Open lesson - LESSON 02.02
Normalizing flows: exact likelihood from invertible layers
Build affine coupling layers that run in both directions, track the log-determinant of each, and train by maximizing an exact log-likelihood.
Open lesson
WATCH ALONG
Videos for these lessons
One hand-picked talk, lecture or tutorial per lesson, in course order. They play here when you press play; nothing loads from the video provider before that.
5 videos· 7 h 10 min
01Attention: queries, keys and values
02A transformer block and a small language model
Paired with this lesson by topic: transformers built from attention. Watch it first for how the pieces fit into one architecture, then build the decoder block here and test it against the properties it must have.
03Generating text: temperature, top-k and nucleus sampling
The schedule lists this lecture as large language models. Watch it for the models whose scores this lesson turns into text, then build the sampling loop and its filters here.
04Autoencoders and the variational autoencoder
The first of the two lectures this lesson is built around: autoencoders and the move from a plain bottleneck code to a generative model. Watch it before the build, then implement the pieces it motivates here.
05Normalizing flows: exact likelihood from invertible layers
The schedule lists this lecture as normalizing flows and diffusion. Watch it for the flow half: invertible maps, the change-of-variables formula and the likelihood it gives. The diffusion half belongs to the next lesson.