Week 6 · two surveys
| courage | care | skill | pride | the | banana | |
|---|---|---|---|---|---|---|
| logit \(z_i\) | 4.1 | 3 | 2.4 | 1.6 | -0.5 | -2 |
| probability \(p_i\) | 62% | 21% | 11% | 5% | 1% | <1% |
Definition 1 The softmax with temperature divides every logit by a positive number \(\tau\), the temperature, before the softmax is applied.
\[ p_i = \frac{\exp\!\left(z_i/\tau\right)}{\sum_{j}\exp\!\left(z_j/\tau\right)}, \qquad \tau>0 \tag{1}\]
One set of logits, three temperatures. The tokens keep their order in every panel; what changes is how the probability is shared among them.
| \(\tau=0.5\) | \(\tau=1\) | \(\tau=2\) | |
|---|---|---|---|
| every gap between logits | multiplied by 2 | unchanged | multiplied by 0.5 |
| courage against care | 9 times as likely | 3 times | 1.7 times |
| courage | 87% | 62% | 41% |
| banana | one token in 230,000 | one in 720 | one in 51 |
| a language model’s text | more predictable, generic | the distribution it was trained to output | varied, then incoherent |
\[ \frac{p_i}{p_j} = \exp\!\left(\frac{z_i-z_j}{\tau}\right) \tag{2}\]
Definition 2 A generative model can synthesise new examples whose statistics are similar to those of its training data.
Definition 3 A probabilistic generative model is a generative model that also assigns a probability \(\prob{\vect{x}\mid\params}\) to any example \(\vect{x}\).
\[ \prob{t_1,\ldots,t_N} = \prob{t_1} \prod_{n=2}^{N} \prob{t_n\mid t_1,\ldots,t_{n-1}} \]
Definition 4 A latent variable \(\vect{z}\) is a variable the model uses to explain an example \(\vect{x}\) but that is never observed in the data.
One model at three stages of training, seen two ways. Top: the samples it generates, beside the data. Bottom: the density it assigns, over the same data.
\[ \loss[\params] = -\sum_{i=1}^{I}\log\prob{\vect{x}_i\mid\params} \tag{3}\]
Definition 5 The generator is a network that maps a latent variable \(\vect{z}\), drawn from a simple distribution, to a sample \(\vect{x}^{*}\).
Definition 6 The discriminator is a network that takes an example and outputs an estimate of the probability that it is real rather than generated.
A GAN small enough to watch. The generator adds one learned number to its noise, which slides its samples along the axis; the discriminator is a sigmoid of position.
Definition 7 Mode collapse is the failure in which a generator ignores most of the variation in its latent variable and produces only one or a few distinct samples.
One small GAN at four chosen points in training, on data with eight modes. The generated clump is narrow and moves; it never settles on the data. The run was set up to fail: its discriminator learns ten times more slowly than its generator.
| Buys | Gives up |
|---|---|
| high-quality samples | any probability for an example |
| one pass through the generator per sample | stable training |
| a well-behaved latent space | coverage |
Definition 8 A normalising flow is a probabilistic generative model that transforms a simple base density over a latent variable \(\vect{z}\) into a density over the data, using a network that is invertible.
A flow in one dimension. A standard normal over the latent, bottom, passes through an invertible map, top right, and becomes the model’s density over the data, left. Each shaded strip holds the same probability before the map and after it.
\[ \prob{x\mid\params} = \left|\frac{\partial\,\mathrm{f}[z,\params]}{\partial z}\right|^{-1} \prob{z}, \qquad z=\mathrm{f}^{-1}[x,\params] \tag{4}\]
| Buys | Gives up |
|---|---|
| the exact probability of any example | architectural freedom: every layer must be invertible, with a stretch that is cheap to compute |
| the latent of any example, by running backwards | compression: \(\vect{z}\) is as large as \(\vect{x}\) |
| ordinary maximum-likelihood training | sample quality, behind GANs and diffusion models |
Definition 9 A latent variable model describes \(\prob{\vect{x}}\) indirectly: it models the joint distribution \(\prob{\vect{x},\vect{z}}\) of the data and a latent variable, and obtains \(\prob{\vect{x}}\) by marginalising over \(\vect{z}\).
Left: a latent with three values, each choosing one Gaussian; the density is their weighted sum. Right: a continuous latent, each value placing a small round Gaussian along a curve; the contours are the density they add up to.
Definition 10 A variational autoencoder pairs two networks and trains them together: a decoder that maps a latent variable \(\vect{z}\) to the mean of \(\prob{\vect{x}\mid\vect{z}}\), and an encoder that maps an example \(\vect{x}\) to the mean and variance of \(q(\vect{z}\mid\vect{x})\), a normal distribution that approximates the posterior \(\prob{\vect{z}\mid\vect{x}}\).
One training step of a variational autoencoder. The encoder proposes a distribution over latents for this example; one latent is drawn from it using noise from a side branch; the decoder turns that latent back into an example.
Definition 11 The evidence lower bound (ELBO) is a function of the encoder’s and decoder’s parameters that is less than or equal to the log-likelihood \(\log\prob{\vect{x}}\) of an example, for every setting of those parameters.
\[ \operatorname{ELBO} = \expectsub{q(\vect{z}\mid\vect{x})}{\log\prob{\vect{x}\mid\vect{z}}} - \kl{q(\vect{z}\mid\vect{x})}{\prob{\vect{z}}} \tag{5}\]
A variational autoencoder with a two-dimensional latent, trained on small handwritten digits. Left: where the encoder places training digits, each marked by its label, which the model never saw. Right: what the decoder draws at each point of a grid over the same space.
Eight training digits, and below each the image the decoder returns from the encoder’s mean latent for it.
| Buys | Gives up |
|---|---|
| any decoder architecture | the likelihood itself: only a lower bound on it is known |
| a compressed latent space in which nearby points decode alike | sharpness: samples and reconstructions are typically blurry |
| an encoder, as a by-product |
Definition 12 A diffusion model has a fixed forward process, which turns an example \(\vect{x}\) into noise through a series of latent variables \(\vect{z}_1,\ldots,\vect{z}_T\) by blending in a little Gaussian noise at each step, and a learned reverse process, which removes that noise one step at a time.
Diffusion on one-dimensional data that takes three values. Left: the fixed forward process carries examples into noise. Right: a trained network run backwards from pure noise, whose paths end on the three values.
\[ \loss = \sum_{t=1}^{T} \norm{\vect{g}_t[\vect{z}_t]-\noise_t}^{2} \tag{6}\]
Reparameterisation: the noisy version is the example scaled down plus noise scaled up, so the noise is a known input
Where it comes from: an evidence lower bound, as for the variational autoencoder; with the encoder fixed and per-step weights dropped, it simplifies to this
Scope: written for one example; training also sums over the examples
Training a diffusion model is ordinary regression.
| Buys | Gives up |
|---|---|
| high-quality samples | sampling speed: many network passes per sample; the other three take one |
| stable training, with a plain least-squares loss | a compact latent: each \(\vect{z}_t\) is as large as \(\vect{x}\), with no organisation by meaning |
Definition 13 Conditional generation draws samples from \(\prob{\vect{x}\mid\vect{c}}\), where \(\vect{c}\) is information supplied with the request, such as a class label or a text prompt.
A diffusion model on three overlapping classes, asked for one of them at three guidance strengths. With more guidance the samples leave the region shared with the other classes, and bunch together.
Definition 14 The Fréchet inception distance (FID) is a distance between the distribution of generated samples and the distribution of real examples, computed on the features of a pretrained image classifier, with each distribution approximated by a multivariate normal.
Definition 15 Precision is the fraction of generated samples that fall within the manifold of the real data.
Definition 16 Recall is the fraction of real examples that fall within the manifold of the generated samples.
The guided diffusion model at its highest strength, measured against real examples of the class it was asked for. Left: nearly every generated sample lies inside the real data’s manifold. Right: a large share of the real examples lies outside the generated samples’ manifold.
| Guidance strength | Precision | Recall |
|---|---|---|
| 0 | 97% | 96% |
| 1 | 99% | 97% |
| 4 | >99% | 68% |
| Family | Who supplies the pairing | Trained to | Pays with |
|---|---|---|---|
| GAN | nobody: a discriminator learns to tell the two sets apart | fool the discriminator | no probability, unstable training, coverage |
| Flow | the invertible map itself | maximise the exact likelihood | a restricted architecture |
| VAE | a learned encoder | maximise a bound on the likelihood | a bound and blur |
| Diffusion | a fixed noising process | predict the noise that was added | many sampling steps |
| Efficient sampling | Sample quality | Coverage | Well-behaved latent | Disentangled latent | Efficient likelihood | |
|---|---|---|---|---|---|---|
| GANs | ✓ | ✓ | ✗ | ✓ | ? | n/a |
| VAEs | ✓ | ✗ | ? | ✓ | ? | ✗ |
| Flows | ✓ | ✗ | ? | ✓ | ? | ✓ |
| Diffusion | ✗ | ✓ | ? | ✗ | ✗ | ✗ |
| Autoregressive | ✗ | ✓ | ? | n/a | n/a | ✓ |
Definition 17 Reinforcement learning is a framework for sequential decision-making in which an agent learns to choose actions in an environment so as to maximise the rewards it receives.
The running example. An agent moves on a grid; the goal pays +1, the hazard pays −1, and either ends the episode. A move goes where it was aimed most of the time and slips sideways otherwise.
The agent chooses an action from the state it sees. The environment answers with a reward and a new state, and the loop repeats until the episode ends.
Definition 18 A Markov decision process (MDP) consists of a set of states, a set of actions, transition probabilities \(\prob{s_{t+1}\mid s_t,a_t}\) and a reward distribution \(\prob{r_{t+1}\mid s_t,a_t}\). The next state and reward depend only on the current state and action, not on the history before them.
Definition 19 The return \(\ret_t\) is the discounted sum of the rewards received after time \(t\), where the discount factor \(\discount\in(0,1]\) weights a reward \(k\) steps further into the future by \(\discount^{k}\).
\[ \ret_t=\sum_{k=0}^{\infty}\discount^{k}\,r_{t+k+1} \tag{7}\]
Definition 20 A policy \(\policy(a\mid s)\) gives, for each state \(s\), a probability distribution over the actions \(a\). A deterministic policy puts all of that probability on one action.
Definition 21 The state value \(\statevalue{s}\) is the expected return from starting in state \(s\) and following the policy \(\policy\) thereafter: \(\statevalue{s}=\expectsub{\policy}{\ret_t\mid s_t=s}\).
Definition 22 The action value \(\actionvalue{s}{a}\) is the expected return from starting in state \(s\), taking action \(a\), and following the policy \(\policy\) thereafter: \(\actionvalue{s}{a}=\expectsub{\policy}{\ret_t\mid s_t=s,\,a_t=a}\).
The grid’s values under its best policy. Left: one state value per cell. Right: the four action values of each cell, each written on the side its move aims at, with the largest highlighted.
\[ \actionvalue{s}{a} = r(s,a) + \discount\sum_{s'}\prob{s'\mid s,a}\,\statevalue{s'} \tag{8}\]
| Method | Needs the rules? | Learns from | Updates |
|---|---|---|---|
| Dynamic programming | yes | the rules alone | at any time |
| Monte Carlo | no | whole episodes | when an episode ends |
| Temporal difference | no | single steps and its own estimates | after every step |
\[ q(s_t,a_t) \leftarrow q(s_t,a_t) + \alpha\Bigl( r(s_t,a_t)+\discount\max_{a}q(s_{t+1},a)-q(s_t,a_t) \Bigr) \tag{9}\]
The table as it is learned. Each cell shows its largest action value and the move that has it; the last panel is the exact answer from the rules. Value spreads backwards from the two paying cells.
Definition 23 An epsilon-greedy policy takes a uniformly random action with probability \(\epsilon\), and otherwise takes the action with the highest estimated action value.
\[ \policy(a\mid s) = \frac{\exp\!\left(q(s,a)/\tau\right)}{\sum_{a'}\exp\!\left(q(s,a')/\tau\right)} \tag{10}\]
Definition 24 A deep Q-network replaces the table of action values with a network that takes a state and outputs one action value for each action.
Definition 25 A policy gradient method learns a stochastic policy directly: a network with trainable parameters maps a state to a distribution over actions, and those parameters are adjusted to make actions that led to high return more probable.
Definition 26 An actor-critic method trains two networks together: the actor is the policy, and the critic estimates state values, which stand in for the future rewards the actor would otherwise have to wait for.
Definition 27 The decision transformer treats offline reinforcement learning as sequence modelling: a transformer decoder reads returns-to-go, states and actions as tokens and predicts the next action.
The decision transformer’s input. A logged episode is written as return-to-go, state, action, repeated; the decoder predicts the action that follows the latest state.
| Learns | Methods | Gives up |
|---|---|---|
| values, from known rules | dynamic programming | any world whose rules aren’t given |
| values, from experience, in a table | Monte Carlo, temporal difference, Q-learning | large state spaces |
| values, with a network | deep Q-networks | stable training: the target moves as the network learns |
| the policy | policy gradients | low-noise updates |
| both | actor-critic | simplicity: two networks to balance |
| from logs alone | decision transformer | the chance to explore |
| Where | The knob | For typical, similar outputs | For varied outputs |
|---|---|---|---|
| a language model’s sampler | temperature on the logits | low | high |
| a GAN | truncation threshold on the latent | tight | loose |
| a diffusion model | guidance strength | strong | weak |
| an agent’s policy | temperature on the action values | low: exploit | high: explore |