Training neural language models
Overview
Mohit Iyyer explains how neural language models learn next-word predictions through a three-step training loop: compute negative log-likelihood loss, calculate gradients with backpropagation, and update parameters using gradient descent. Using an RNN and a scalar toy network, he connects cross-entropy to one-hot training targets, demonstrates how backpropagation reuses intermediate derivatives, and explains why recurrent models need backpropagation through time; he also contrasts sequential RNN computation with the parallelism of transformers.
Key takeaways
- An RNN can train on next-word targets at every sequence position in one left-to-right pass, sharing the same softmax weights instead of recomputing separate passes for every prefix.
- Language-model cross-entropy is negative log-likelihood because each training example uses a one-hot target distribution that assigns probability 1 to the observed next word.
- When the same prefix has multiple continuations, minimizing aggregate negative log-likelihood favors a predicted distribution matching their observed frequencies.
- Backpropagation computes parameter gradients by applying the calculus chain rule from the loss backward and caching shared intermediate derivatives to avoid redundant work.
- Because RNN weights are reused across time steps, backpropagation through time accumulates gradient contributions from each step before updating those shared parameters.
- The learning rate controls the size of gradient updates: steps that are too large can overshoot low-loss regions and make training loss rise.
Chapters
- Project groups receive a primary mentor, who follows the work and oversees grading and feedback.
- The proposal is due in roughly two or three weeks and is intended to get groups started and provide a basis for feedback.
- Proposals and final reports use a new AI-detection process through Gradescope, with opportunities to review and revise submissions.
- An RNN processes word embeddings from left to right, updating its hidden state at each time step.
- The final hidden state is passed through a softmax layer to produce a vocabulary distribution for the next word.
- For the example prefix “My favorite LLM is,” the model’s target continuation is “Claude.”
- Each hidden state can predict the word that follows its prefix, rather than using computation only for the final position.
- For “My favorite LLM is,” intermediate predictions correspond to “favorite,” “LLM,” “is,” and “Claude.”
- The output weight matrix W_o is shared across time steps, just as the RNN shares its recurrent and embedding-projection weights.
- Training batches can contain unrelated sequences, such as “My favorite LLM is Claude” and “But I don’t really like AI.”
- Efficient training aims to predict next words at all positions across all batch sequences in parallel.
- RNN hidden states must be computed left to right, while transformer architectures address this limitation with parallel computation.
- The RNN parameter set includes the embedding matrix E, recurrent weights, embedding-projection weights, and output weights.
- Rows of E represent word embeddings for vocabulary items.
- Hidden states such as h₁ depend on the current input; shared weight matrices remain fixed across examples until training updates them.
- Neural-network parameters begin as random values, so initial next-word predictions do not reflect the training data.
- Training adjusts the elements of the parameter set θ to improve predictions across many observed sequences.
- Unlike an n-gram model, which can estimate probabilities by counting frequencies, a neural language model must learn useful parameters through optimization.
- The loss L(θ) measures how poorly the current model predicts training targets; language modeling commonly uses negative log-likelihood.
- The gradient contains a partial derivative for each parameter, indicating how changing that parameter affects the loss.
- To reduce loss, gradient descent updates parameters in the negative-gradient direction, scaled by a learning rate.
- A learning rate controls update size: an overly large step can overshoot a low-loss region and increase the loss.
- Neural-network loss surfaces can have many parameters and local minima, so optimization does not guarantee finding a global minimum.
- Training systems often use optimizers such as Adam, AdamW, and Muon rather than the basic stochastic gradient descent update.
- For a prefix such as “My favorite LLM is,” the training target is the observed next word, such as “Claude.”
- The model’s softmax assigns probabilities to vocabulary words; the loss is the negative log of the target word’s probability.
- Minimizing this loss is equivalent to maximizing the likelihood of the training data, and exponentiating the average loss yields perplexity.
- Cross-entropy compares a reference distribution P with the model’s predicted distribution Q over vocabulary words.
- For a single training example, P is one-hot: the observed next word has probability 1, and all other words have probability 0.
- With that one-hot target, cross-entropy simplifies to the negative log probability the model assigns to the observed word.
- If “My favorite LLM is” occurs 100 times with Claude following 50 times, Gemini 25 times, and ChatGPT 25 times, the best-fitting target distribution reflects those frequencies.
- A neural model can share information across similar prefixes through their embeddings, rather than relying only on exact prefix matches as an n-gram model does.
- Even short prefixes provide some training signal, though longer context can narrow the set of plausible next words.
- Backpropagation differentiates the composed functions in a neural network using the chain rule of calculus.
- The computation starts from the output loss and works backward through the operations and parameters that produced the prediction.
- Caching shared intermediate derivatives avoids recomputing the same products when calculating gradients for different parameters.
- The toy network uses scalar input x, target y, hidden state h = tanh(W₁x), output o = tanh(W₂h), and squared loss (y − o)²/2.
- Forward propagation computes intermediate values in order, from x through the hidden state and output to the loss.
- The gradient for W₂ follows the chain from loss to o, then to the pre-activation W₂h, with the local derivative of tanh expressed using the output.
- Computing the W₁ gradient extends the chain through the hidden state to the input, while reusing derivatives already computed for W₂.
- In an RNN, shared weights are used at every time step, so backpropagation through time accumulates each step’s contribution to their gradients.
- The update equations subtract the learning-rate-scaled gradients from W₁ and W₂; training repeats loss calculation, backpropagation, and updates.
- The next lecture will apply these training principles to transformer language models.
Transcript excerpt
0:13 All right, let's get started everyone. So today, um, we're going to be talking about training neural language models. All right, let's get started. Um, so I messed up the YouTube. So, um, I think this CL this lectures YouTube will just probably be a recording that you can uh watch later. Um, okay. So, first thing, some logistics. Uh, your final project group assignments should all be um released. So, everyone should have gotten an email with their group members. And uh from here on out you are expected to coordinate amongst
1:00 yourselves for the proposal and uh project report deliverables. Um if you didn't get such an email um please either see me or send an Piaza post or email to the instructor's account and we'll get that sorted for you. um just as uh some um you know information for your projects. Each of the uh groups is going to have one of the TAs or myself as a primary mentor. So uh whoever is your primary mentor will follow your project throughout the course of the semester. You can come to anyone's office hours to talk about your project, but um the person in who's like responsible for overseeing your and
1:49 grading your projects will likely be able to give you the most fine grade and feedback. Um okay, so the next deliverable for the project is the proposal, which is due in a couple of weeks, two or three weeks. Um that's not a lot of time. Uh we're hoping that you can you know do some reading on your own within your group to figure out topics of interest. Feel free to come to our office hours to discuss the feasibility of your project ideas and we can give you some feedback to help uh guide the design before you
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.