CodeRigor

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.

Free to useLessons & exercises6 authored lessons3 units
Start with the first lesson.

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

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

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

  1. 01Attention: queries, keys and values

  2. 02A transformer block and a small language model

  3. 03Generating text: temperature, top-k and nucleus sampling

  4. 04Autoencoders and the variational autoencoder

  5. 05Normalizing flows: exact likelihood from invertible layers