Recommended resources
The freely available resources this lesson will direct you to.
- Stanford CS224N: NLP with Deep Learning | Winter 2019 | Lecture 7 – Vanishing Gradients, Fancy RNNswww.youtube.com
- Stanford CS224N: NLP with Deep Learning | Spring 2024 | Lecture 5 - Recurrent Neural Networkswww.youtube.com
- CS231n Deep Learning for Computer Visioncs231n.github.io
- 9.7. Backpropagation Through Timed2l.ai
Hello! This course begins with a time-sensitive sequence-modeling sprint, alongside the longer systems and C++ track. In this first lesson, you will build the mathematical picture needed for that sprint: how a vanilla recurrent neural network (RNN) updates its state, and why training it on a long stream can become difficult even when the forward computation is straightforward.
By the end, you should be able to write the recurrence with correctly shaped matrices, trace how an early input can affect a later prediction, and identify the repeated derivatives responsible for vanishing or exploding gradients. Allow about 40 minutes, including the video and reading below.
One update rule, reused over time
Suppose a model receives a sequence of input vectors . For a stream of measurements, might contain all features observed at step . An RNN maintains a hidden state : a fixed-size vector computed from the current input and the preceding state. It starts from an initial state , often set to zero at the beginning of an independent sequence.
Watch how the same computation is reused at each step. The lecture uses words as inputs, but its architecture applies equally to numerical feature vectors.
Stanford CS224N: NLP with Deep Learning | Spring 2024 | Lecture 5 - Recurrent Neural Networks
Watch “Recurrent Neural Networks” from Stanford Online for a visual account of the hidden state and shared weights.
Watch the RNN construction. Focus especially on the initial state around 00:53:00 and the bias and tanh activation around 00:54:04. Treat each word embedding in the example as a stand-in for a general input vector.
To derive the update, we need a way to combine two sources of information: the new input and the previous state . Give each source a learned linear transformation, add a bias, and apply a componentwise nonlinearity:
This is the vanilla RNN hidden-state recurrence. If and , then has shape , has shape , and has length . The tanh function acts on each entry of . An optional output projection makes a prediction, for example,
The matrices are shared across time. There is one , not a newly learned matrix for every . Consequently, the parameter count does not increase with sequence length, although processing a longer sequence takes more computation.
A two-step expansion makes the dependence on history explicit:
The first input can influence , and therefore a prediction at step 2, only through the state carried forward. The same principle extends to later steps. It does not guarantee that useful information survives indefinitely: every update transforms the state again.

Why training has a long backward path
Consider a loss on a prediction made at the final step. A change to an early hidden state affects that loss through every intervening state. Backpropagation through time (BPTT) applies the ordinary chain rule to this unfolded computation.
The following reading makes an important distinction: a shared weight affects the state directly at the current step and indirectly through earlier states. You do not need to memorize its general product-and-sum notation; track those two routes of influence.
9.7. Backpropagation Through Time
Read “9.7. Backpropagation Through Time” from Dive into Deep Learning to connect the forward recurrence to gradients of shared parameters.
In “9.7.1. Analysis of Gradients in RNNs,” read the gradient analysis, ending with the short “9.7.1.1. Full Computation” subsection. Pay particular attention to equations (9.7.4) and (9.7.7): identify the direct parameter effect and the products that carry an earlier effect across time. It is fine to skip the proof of the intermediate sequence identity in (9.7.5).
For our tanh recurrence, define the Jacobian of one state update with respect to the preceding state:
The expression is componentwise. It comes from the derivative of tanh: if a component of is near or , its corresponding derivative is near zero. Thus, even a sizeable recurrent weight need not transmit much gradient when the activation is saturated.
For an earlier step , repeated use of the chain rule gives
Using column gradients, the part of the final loss signal reaching is
There are Jacobians in that product. Sequence length matters because the gradient must survive repeated multiplication, not because the model acquires more parameters.
The hidden states themselves are not the parameters we train, so why inspect their gradients? The signal reaching also determines how much the final loss can teach a weight used around step . Because is reused, its total gradient adds contributions from all its uses. A tiny distant contribution can be overshadowed by nearer ones even when the overall parameter gradient is not tiny.
Shrinking and growing products
In a one-dimensional RNN, a single backward step multiplies the gradient by
Imagine, just for illustration, that this factor stays near . Across 50 steps, the distant signal is scaled by approximately , or . This is a vanishing gradient: a late loss provides almost no guidance about how to change an early influence. If instead the factor stays near , its product across 50 steps is about . This is an exploding gradient, which can produce unstable parameter updates. Real Jacobians vary from step to step; the examples illustrate what persistent contraction or amplification does.
The matrix case has the same principle, with an important qualification. Since tanh derivatives have magnitude at most one,
Thus, if the spectral norm is below one, the norm of a gradient travelling through many such steps is bounded by a geometrically shrinking quantity. A norm above one, however, does not guarantee explosion: the activation derivatives may be small, and successive matrices may amplify different directions. What matters is the product of the actual Jacobians along the sequence.
Watch the next excerpt for the visual chain-rule argument and its consequence for learning from distant observations. Its simplified weight-magnitude discussion is intuition, rather than a guarantee for every matrix and sequence.
Stanford CS224N: NLP with Deep Learning | Winter 2019 | Lecture 7 – Vanishing Gradients, Fancy RNNs
Watch the vanishing-gradients portion of Stanford Online’s “Vanishing Gradients, Fancy RNNs” to reinforce the derivation and see why distant signals matter.
Watch the backward path for the chain-rule picture, then the gradient factors for the repeated-weight argument. Continue through the practical effect: compare feedback from a nearby loss with feedback from a distant one.

Sigmoid has a derivative no greater than , while tanh can have a derivative near one around zero. That helps explain the contrast in the plotted settings, but tanh can also saturate and cause vanishing gradients. Likewise, a graph labeled by a weight matrix’s spectral radius cannot, by that label alone, predict every gradient: the activation values and the directions encountered during backpropagation matter too.
For a long measurement stream, the practical distinction is between processing an early observation and learning to use it later. The forward recurrence can accept the observation and continue updating its state. If the relevant backward signal vanishes, training struggles to adjust the model so that observation remains useful many steps later. If gradients explode, updates can instead become erratic or numerically invalid.
Takeaways
A vanilla RNN repeatedly applies one learned update to the current input and previous hidden state. Unfolding that update reveals why a late loss depends on early states through a product of many Jacobians. Persistent contraction makes long-range learning weak; persistent amplification can destabilize training. Neither outcome follows from sequence length alone—the weights, activations, and states jointly determine the gradient path.
Later in this sprint, you will use a gated recurrent model and control the length and magnitude of training gradients. First, the next lesson tackles a separate practical constraint: streaming sequence-sized groups from Parquet into PyTorch without loading the entire dataset into memory.