Composition and decoding in neural language models
Overview
Mohit Iyyer explains how neural language models turn word embeddings into a prefix representation, then use a softmax distribution to score or generate the next token. He compares greedy, ancestral, top-k, and top-p decoding with sum or average pooling, Bengio et al.'s 2003 fixed-window model, and recurrent neural networks, showing how weight sharing helps RNNs while sequential computation motivates transformers.
Key takeaways
- Next-token probabilities serve two different purposes: they score known continuations during training or evaluation, and they drive generation when no ground-truth continuation is available.
- Greedy decoding always selects the argmax token, while ancestral sampling draws proportionally to probability; top-k and top-p reduce exposure to low-probability tail tokens.
- Sum and average pooling are simple composition functions, but they erase word order and can lose distinctions such as the negation in “not horrible.”
- A fixed-window model concatenates position-specific embeddings, causing its parameter count to grow with window size and preventing the same word from sharing weights across positions.
- An RNN shares its update matrices across positions and can process variable-length prefixes, but its recurrence forces sequential computation that limits parallel training.
- Transformers address the RNN's sequential-computation bottleneck with parallel processing, while still imposing a finite context window.
Chapters
- The lecture moves from input and output embeddings to the composition function that creates a prefix vector.
- It introduces decoding as the process of turning next-token probabilities into generated text.
- Course logistics include project group assignments and Homework 0 submission through Gradescope.
- A prefix vector x of dimension d_h is multiplied by a vocabulary weight matrix W of shape V by d_h, then normalized by softmax.
- During training and evaluation, the model's probability for the observed next word determines how well it fits the example.
- Assigning low probability to the correct next token, such as Claude after “My favorite LLM is,” should produce a larger training update.
- Test-set next-token probabilities can be aggregated into perplexity.
- Unlike evaluation on a known test set, generation must choose a token without access to the ground-truth continuation.
- After a token is generated, it is appended to the prefix and the model computes a new distribution.
- Repeated next-token selection continues until the model emits an end-of-sequence token or reaches a length cap.
- Greedy decoding takes the argmax token at every time step, making the output deterministic for a given prefix.
- Generation can stop at the end-of-sequence token or after a fixed maximum number of tokens to prevent endless loops.
- Always selecting the top token limits diversity and can prevent a model from exploring alternative reasoning paths.
- Ancestral sampling draws each next token according to its probability, so repeated generations can differ.
- A long tail of low-probability vocabulary items can produce contextually inappropriate tokens despite each item's tiny probability.
- Top-k sampling restricts each draw to the k most probable tokens; the lecture uses k = 50 as an example.
- Top-p sampling keeps the highest-ranked tokens until their cumulative probability reaches a threshold, such as p = 0.90.
- In the example distribution, Claude and Gemini together reach the 90% cutoff, so lower-ranked options are excluded.
- Unlike top-k, nucleus sampling lets the number of eligible tokens vary with each prefix's probability distribution.
- Greedy decoding remains common for powerful, heavily post-trained models with peaked distributions; sampling is useful for more varied outputs.
- Each input token receives a d_e-dimensional embedding, and the composition function produces one d_h-dimensional representation of the prefix.
- That representation feeds the softmax layer, after which decoding selects a token that can be fed back into the model.
- The lecture previews four composition approaches: elementwise sum or average, fixed-window feed-forward models, recurrent neural networks, and transformer self-attention.
- Elementwise summation or averaging combines embeddings such as c1 through c4 into a single prefix vector.
- Pooling treats each embedding's contribution uniformly unless the embedding magnitudes themselves encode differences.
- Because word order disappears, “My favorite LLM is” can map to the same representation as a rearrangement such as “LLM is favorite my.”
- Order loss also obscures negation, as in the difference between “horrible” and “not horrible.”
- The fixed-window model takes the last four word embeddings and concatenates them into a 4d_e-dimensional input.
- A learned matrix projects the concatenation into a hidden representation, which is then used to predict the next token.
- The architecture preserves word order within the chosen window, unlike sum or average pooling.
- Bengio et al.'s 2003 work introduced this neural language-model approach well before it became widely used at larger scales.
- A feed-forward layer applies a learned matrix transformation followed by an elementwise nonlinearity; the softmax layer then maps the hidden state to vocabulary probabilities.
- A stack of such layers is called a multilayer perceptron, or MLP, and each layer adds trainable parameters.
- Embedding dimension d_e, hidden dimension d_h, and window size are design choices selected using held-out validation performance, such as perplexity.
- Larger parameter counts generally require more training data to learn useful input-output relationships.
- ReLU outputs zero for negative values and leaves positive values unchanged.
- Without nonlinearities, stacked matrix multiplications collapse into one equivalent matrix multiplication, so depth adds no expressive power.
- The tanh activation is smooth and bounded between -1 and 1, and is commonly used in recurrent networks.
- ReLU has zero derivative for negative inputs, while tanh gradients shrink near its -1 and 1 limits.
- A fixed-window model cannot use information that falls outside its selected context window.
- Increasing the window from four to five tokens expands the first-layer input and adds another position-specific block of weights.
- The same word, such as LLM, is processed by different weight blocks depending on whether it appears in the second or third slot, so learning is not shared across positions.
- Modern transformers also have finite context windows, although they do not use this primitive fixed-window composition method.
- A recurrent neural network begins with h0, a hidden state representing an empty sequence, then reads one embedded token at a time.
- Its update is h_t = f(W_h h_{t-1} + W_c c_t), where c_t is the current word embedding and f is typically tanh.
- The hidden-state matrix W_h has shape d_h by d_h, while W_c maps d_e-dimensional embeddings into the d_h-dimensional hidden space.
- The final hidden state is passed to the softmax layer to predict the next word.
- RNNs reuse W_h and W_c at every time step, so a word is processed by the same parameters regardless of its position.
- The model must compute each hidden state in order before reaching the final state, making long-prefix processing difficult to parallelize.
- Transformers were developed in part to avoid this sequential training bottleneck by allowing computations across input positions to run in parallel.
- The next lecture covers gradient descent and backpropagation before a detailed treatment of transformers.
Transcript excerpt
0:01 Okay, let's get started. So today we are going to continue with our um lecture on neural language models. So last time we discussed the um embeddings as well as the softmax layer. So the input and output embeddings of a neural language model. Today we'll talk about the middle part of the um the model which is the composition step. So how we go from a bunch of different word embeddings associated with the input to a single representation of the um input that we then feed into the softmax layer. Um so we'll talk about that but also the decoding step of a neural language model which is how do we go
0:50 from the predicted probability distribution over the next word to um you know generating a long piece of text from one of these models. All right. Um before we get into that some logistics. Uh so today is the deadline to submit your project group assignments if you want to do that. Um if not we will just randomly assign you to a group and uh what we do is uh when we complete all the group formation we will send one email CCing all the members of your group. So um you can then start organizing amongst yourselves and setting up meetings and plans and stuff like that. Okay, any questions on that? All right. Uh, your homework zero is
1:40 also due in a couple days. Um, so, uh, if you're not already on grade scope, please send us an email. I think we added everyone that, um, joined the class late, but if you're still not on for whatever reason, email the instructor's account and we will get you added. All right. So um let us uh kind of um talk about where we left off, right? 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, Mohit Iyyer.