11/11/26: Lect-11 (cont.)
Overview
Joseph Zambreno explains how the Transformer replaces recurrent sequence processing with parallel self-attention, using learned query, key, and value projections to connect tokens directly. He builds from attention scores and causal masking through positional encodings, beam search, and multi-head attention, then describes the encoder-decoder Transformer and demonstrates GPT-2 with 768-dimensional embeddings, 12 attention heads, and 12 layers.
Key takeaways
- Self-attention removes recurrent dependencies by comparing tokens in parallel, but autoregressive text generation still proceeds one token at a time.
- Scaled dot-product attention computes softmax(QKᵀ/√D)V; dividing by √D controls score magnitude as representation dimension D increases.
- Causal masks set future-position scores to negative infinity before softmax, enabling parallel next-token training without leaking future tokens.
- Positional encodings are necessary because content-only self-attention does not distinguish word order; GPT-2’s learned position table covers 1,024 positions.
- A Transformer block combines multi-head attention, a D-to-4D-to-D feed-forward network, layer normalization, and residual connections.
- Beam search maintains K candidate sequences rather than selecting one greedy token at each step, improving the chance of finding a strong overall sequence at additional computational cost.
Chapters
- Recurrent encoder-decoder models pass information through successive hidden states, making earlier-token information harder to retain as sequences grow.
- Cross-attention can use encoder hidden states, but the encoder still processes tokens sequentially before decoding can begin.
- The paper Attention Is All You Need showed that attention could do the core sequence-modeling work without recurrence.
- Self-attention has computation broadly comparable to recurrent and convolutional alternatives, but its token comparisons can be computed in parallel.
- A query represents what a token is looking for, keys provide information used to score matches, and values contain the information combined into the result.
- The library analogy separates searching book metadata, the keys, from retrieving relevant book content, the values.
- Bahdanau attention uses an additive score, while Transformer attention commonly uses dot products.
- Cross-attention and self-attention use the same basic lookup operation, but define queries, keys, and values from different input representations.
- A Python dictionary performs a hard lookup: querying key 7 returns its associated value 12, while querying absent key 6 returns nothing.
- Attention scores every key against a query, applies softmax to produce weights, and blends the associated values.
- A query near key 7 can still receive some contribution from key 3, unlike an exact-match lookup.
- Transformer attention learns what counts as a close query-key match through backpropagation rather than using a fixed distance rule.
- The worked sentence is “chip overheated because it,” with four token representations supplied as input vectors.
- Each token is represented by a D-dimensional vector; the example sets D to 2 so the arithmetic fits on a slide.
- Learned matrices WQ, WK, and WV transform each input representation into a query, key, and value.
- The projections preserve the model dimension, allowing a T-by-D token representation to remain compatible with subsequent attention calculations.
- For the token “it,” the example computes its query and takes dot products with the keys for “chip,” “overheated,” “because,” and “it.”
- The resulting scores are scaled, then passed through softmax to form attention weights that sum to one.
- The example assigns about 54% of the attention to “chip” and 26.6% to “overheated,” reflecting the made-up demonstration values.
- A weighted sum of the corresponding value vectors gives a representation influenced by all tokens, with the strongest contributions from the highest-weighted keys.
- Softmax on scores that are too small produces nearly uniform weights, while excessively large scores produce nearly one-hot weights.
- Near one-hot attention can weaken useful gradient signals during backpropagation.
- For random query and key vectors with unit variance, a length-D dot product has variance proportional to D.
- Dividing dot products by √D keeps softmax scores from becoming too extreme as the model dimension grows.
- Self-attention lets tokens within one sequence attend to one another; cross-attention uses decoder queries to attend to encoder keys and values.
- Self-attention projections include WQ, WK, and WV plus an output projection, with roughly 4D² weights before biases.
- Unlike convolution kernels, which connect fixed neighboring positions, attention computes token-to-token weights from the representations themselves.
- The original Transformer combines self-attention and cross-attention in its encoder-decoder design, without recurrent connections.
- During next-token training, a causal mask hides future positions so token i cannot use information from tokens i+1 and later.
- Masked attention scores are set to negative infinity before softmax, producing a lower-triangular pattern of allowed attention.
- The token “chip” can attend only to itself, while later tokens can use progressively more of the preceding sequence.
- Causal masks differ from padding masks, which exclude invalid pad tokens in fixed-length batches.
- Without positional information, self-attention treats tokens as a set, so reordering “chip overheated because it” would not change the content-based attention results.
- The lecture connects this limitation to the “dog bites man” versus “man bites dog” distinction.
- Sinusoidal positional encodings add sine and cosine values across vector dimensions to distinguish a token by its sequence position.
- GPT-2 uses a learned positional table with 1,024 positions; positions beyond that range can reuse encodings and create ambiguity.
- With teacher forcing and a causal mask, a model can process and score every position in a training sequence in one parallel pass.
- Autoregressive generation remains sequential: after predicting “was” in “the chip overheated because it,” the model must run again to predict the following token.
- Each new generated token creates additional attention connections, although cached keys and values can avoid recomputing some work.
- Exposure bias arises because training uses correct prior tokens while inference uses the model’s own predictions, allowing early mistakes to compound.
- Greedy decoding commits to the highest-probability token at each step, while sampling can choose among likely alternatives using temperature or top-P settings.
- Beam search retains the top K partial sequences, expands them, and prunes lower-scoring paths at each step.
- For beam width K=3, the model follows three candidate translations instead of committing immediately to one word.
- Beam search can reduce the impact of a bad early choice, but requires maintaining and scoring K candidate paths.
- Multi-head attention divides projected representations into multiple heads, allowing different attention patterns to be learned in parallel.
- One head might focus on the immediately preceding token, while another tracks a noun relevant to a pronoun such as “it.”
- The head outputs are combined after their separate attention calculations.
- The original Transformer used a model dimension of 512; the lecture notes that modern models can use dimensions as large as 16,384.
- A Transformer block combines multi-head attention with a two-layer per-token feed-forward network.
- The MLP expands each D-dimensional representation to 4D, applies an activation, and compresses it back to D.
- Layer normalization operates on each token representation, while residual connections add the original input X to the transformed output F(X).
- Residual paths, adapted from ResNet, preserve information and support backpropagation through deep stacks.
- The original Transformer remains an encoder-decoder sequence-to-sequence architecture, while GPT-2 uses a decoder-only design for text generation.
- The browser-based Transformer Explainer shows GPT-2 tokenizing “the chip overheated because it”; the typed phrase becomes six tokens because “overheated” splits.
- GPT-2 uses 768-dimensional representations, 12 attention heads, and 12 Transformer layers, with the feed-forward layer expanding to 4 × 768 dimensions.
- The demonstration predicts “was,” then continues autoregressively to produce “too hot,” illustrating that each new token is fed back into the model.
Transcript excerpt
0:00 Let's go ahead and get started. Where we left off and we we covered a lot of ground on Monday is we were talking about first encoder decoder architectures for sequence to sequence translation style sequence modeling tasks. And we somewhat motivated it. We tried to kind of show what that means in practice, motivated by looking at how when you have these recurrent networks, there is this information that gets passed from input through successive hidden states as they get computed and recomputed that gets passed from the input through the encoder side and then finally as a context that's shared with the decoder side. And the perceived shortcoming or limitation of that is that as your sequence gets longer and
0:48 longer, the ability for a long network to actually retain information about those earlier tokens becomes more challenging and and more limited as the sequence depth itself increases here. That was some of the motivation for the original idea behind calculating computing attention, what we were calling bottom-up or bottom-up attention here, which is we had those hidden states in our RNN on the encoder side and they're getting updated for every token update H of T update H of T. Originally kind of discard all that information along the way. The attention mechanism tries to answer the question of is there a useful way in one can make use of that hidden state information as it is being processed while it's it's
1:36 it is in the the process of being calculated here. And that was what the attention mechanism was attempting to do. We had this concept of queries, keys, and values and I think it's important enough we're going to we're going to strip that down to its more simplest form here in just a moment here. And we
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Joseph Zambreno.