Backpropagation Through Time › Deriving BPTT Gradients
Language AI HandbookAll chapters Backpropagation Through Time › Deriving BPTT GradientsOne section · Sections in this chapter3 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
With the unrolled computation graph established, standard backpropagation applies. The challenge is that the loss at time depends on , which depends on , which depends on , and so on. Computing the gradient of the total loss with respect to a parameter like requires following these chains back through time and aggregating every contribution.
The Chain Rule Through Time
The gradient of the total loss with respect to decomposes into per-timestep contributions:
where:
- : total gradient for the recurrent weight matrix
- : the gradient contribution from the loss at timestep
Each per-timestep gradient must account for the fact that depends on , which itself depends on through all previous timesteps. Applying the chain rule:
where:
- : gradient of the loss at time with respect to the hidden state at time , obtained directly from the output layer
- : Jacobian capturing how the hidden state at time depends on the hidden state at an earlier time , computed as a product of local Jacobians
- : how the hidden state at time directly depends on the weight matrix (via the term in the RNN update)
The last term, , captures the direct effect: at timestep , the operation uses explicitly, so differentiating this term with respect to gives (as an outer product structure). The middle term carries this local gradient signal all the way from timestep forward to timestep , where the loss occurs.
The Product of Jacobians
The middle term is the key quantity in understanding both how BPTT works and why it is numerically challenging. It is computed via the chain rule through the sequence of hidden state transitions from to :
where:
- : a product of Jacobian matrices, one for each timestep between and
- : the Jacobian of hidden state with respect to hidden state
Each local Jacobian captures two effects: the tanh nonlinearity applied elementwise at step , and the recurrent weight matrix that mixes hidden state dimensions. Working through the differentiation:
where:
- : a diagonal matrix whose entries are the tanh derivative at each hidden unit at timestep . The tanh derivative is , so the diagonal entries are for each dimension .
- : the transpose of the recurrent weight matrix, arising because we differentiate with respect to
The tanh derivative is a number between 0 and 1 for any input . When is large in magnitude (the tanh is saturated), the derivative is near 0. When is near zero, the derivative is near 1. This saturation behavior is the root cause of the vanishing gradient problem: at timesteps where hidden units are saturated, the local Jacobian multiplies the incoming gradient by something close to zero, annihilating it.
The product of these Jacobians across timesteps is what makes BPTT expensive and unstable. When is large (long sequences), you are multiplying many matrices together. If the eigenvalues of the resulting product are less than 1 in magnitude, the gradient vanishes exponentially. If greater than 1, it explodes exponentially. This is the vanishing and exploding gradient problem, analyzed in depth in the next chapter.
Gradient Accumulation Over Time
Because the same weight matrix is used at every timestep, its gradient is the sum of contributions from every timestep in the sequence. The complete gradient combines contributions from all future losses and all past hidden state paths:
where:
- The outer sum iterates over each timestep's loss
- The inner sum iterates over all past timesteps that contributed to through
- The product is the chain of Jacobians connecting time to time
This double sum has terms in total, though in practice the backward pass is structured to avoid redundant work through the delta recurrence described below. The same accumulation applies symmetrically to , with replacing the terminal term.
To see what this equation computes, think about a specific loss, say at timestep 10 of a 20-step sequence. This loss propagates a gradient signal backward through steps 10, 9, 8, ..., 1. This contributes to 's gradient via ten different paths of different lengths. Meanwhile, propagates through all 20 steps, and propagates through just 1 step. The total gradient for is the aggregate of all these signals, each attenuated by the product of Jacobians along its path.
The Error Signal Flowing Backward
In practice, BPTT works by defining a per-timestep error signal that captures the total gradient flowing into hidden state from all future losses. This quantity is evaluated right to left, from down to :
where:
- : the total error signal at timestep , combining local and future gradient contributions
- : the local gradient from the loss at time only (obtained from the output layer's backward pass)
- : the gradient flowing back from the next timestep, multiplied by the transposed Jacobian
- by convention (no loss beyond the last timestep)
The base case initializes , the gradient from the final timestep's local loss. Then the recurrence propagates this signal backward, accumulating contributions at each step. At each intermediate timestep , the error signal has two components: one from the local loss at that step, and one from all the future error signals that have been folded together through the backward recurrence. The second component is what allows gradients from the end of the sequence to reach parameters that contributed to early-sequence hidden states.
Once all values have been computed, the weight gradients follow directly from outer products summed across all timesteps:
where:
- : the elementwise product of the error signal with the tanh derivative vector , which applies the local activation gradient before accumulating
- : the previous hidden state as a row vector, so the outer product gives a matrix for
- : the input vector at time as a row vector, giving a matrix for
This is the same outer-product structure as a feedforward network, just summed over all timesteps rather than evaluated once. Each outer product contributes a rank-1 update to the gradient matrix, and the total gradient is the sum of such rank-1 updates. This structure also reveals why increasing hidden size increases the parameter count and gradient computation quadratically: the recurrent weight matrix is , and computing outer products at each of the timesteps scales as .
Why Gradients Must Flow Backward Through Time
It is worth stepping back to appreciate why gradient flow through time is necessary rather than a mathematical artifact. Consider training an RNN to model language. To predict the word "cat" at position 20 in a sentence that begins "The fluffy orange ___", the model needs to recognize that the adjectives "fluffy" and "orange" are relevant. Gradient signal flowing backward from the prediction error at position 20 to the words at positions 2, 3, and 4 is what teaches the model to pay attention to those early adjectives. If that gradient signal cannot reach those positions (because it has been attenuated to near-zero), the model cannot adjust its weights to learn those long-range associations. BPTT is the mechanism by which learning happens; gradient decay is the mechanism by which it fails.