LSTM Gradient Flow › Worked Numerical Example: Tracing a Gradient Through the Cell State

Michael BrenndoerferMay 12, 20253 min read
Language AI HandbookAll chapters
Part 11 of 67Recurrent Neural Networks
Chapter 79LSTM Gradient Flow › Worked Numerical Example: Tracing a Gradient Through the Cell State
Reading section 4 of 17 · Worked Numerical Example: Tracing a Gradient Through the Cell State · 3 minSection 4 of 17 · 3 min

Book contents

Section 4 of 17 · 3 min read

LSTM Gradient Flow › Worked Numerical Example: Tracing a Gradient Through the Cell StateOne section · Sections in this chapter4 / 17 ↓
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

Abstract derivations are easier to internalize when paired with a concrete example. Let's trace a gradient backward through a small LSTM for exactly three timesteps, keeping all numbers visible.

Setup

Consider an LSTM with a single hidden dimension (h=1h = 1) processing a sequence of three timesteps. This is a toy model, but it captures the essential structure. At each step, the gate values are scalars rather than vectors.

Suppose the learned (or assumed for this example) gate values are:

  • Timestep 1: f1=0.95f_1 = 0.95, i1=0.3i_1 = 0.3, o1=0.8o_1 = 0.8
  • Timestep 2: f2=0.90f_2 = 0.90, i2=0.5i_2 = 0.5, o2=0.7o_2 = 0.7
  • Timestep 3: f3=0.85f_3 = 0.85, i3=0.4i_3 = 0.4, o3=0.9o_3 = 0.9

And the candidate cell state values (the tanh⁡\tanh of the gate pre-activation) are:

  • Timestep 1: c~1=0.6\tilde{c}_1 = 0.6
  • Timestep 2: c~2=−0.3\tilde{c}_2 = -0.3
  • Timestep 3: c~3=0.7\tilde{c}_3 = 0.7

We start with c0=0c_0 = 0.

Forward Pass: Computing Cell States

The cell state evolves as ct=ft⋅ct−1+it⋅c~tc_t = f_t \cdot c_{t-1} + i_t \cdot \tilde{c}_t:

Step 1: c1=0.95⋅0+0.3⋅0.6=0.18c_1 = 0.95 \cdot 0 + 0.3 \cdot 0.6 = 0.18

Step 2: c2=0.90⋅0.18+0.5⋅(−0.3)=0.162−0.15=0.012c_2 = 0.90 \cdot 0.18 + 0.5 \cdot (-0.3) = 0.162 - 0.15 = 0.012

Step 3: c3=0.85⋅0.012+0.4⋅0.7=0.0102+0.28=0.2902c_3 = 0.85 \cdot 0.012 + 0.4 \cdot 0.7 = 0.0102 + 0.28 = 0.2902

The cell state carries information across time, here starting at 0, rising to 0.18, dipping to 0.012 (the negative candidate overwriting most of the stored value), then jumping to 0.29.

Backward Pass: Tracing the Gradient

Suppose the loss at timestep 3 has gradient ∂L/∂c3=1.0\partial L / \partial c_3 = 1.0 with respect to the cell state.

We ask: what is the gradient with respect to c0c_0? In the cell state path, this is:

∂L∂c0=∂L∂c3⋅f3⋅f2⋅f1=1.0⋅0.85⋅0.90⋅0.95=0.727\frac{\partial L}{\partial c_0} = \frac{\partial L}{\partial c_3} \cdot f_3 \cdot f_2 \cdot f_1 = 1.0 \cdot 0.85 \cdot 0.90 \cdot 0.95 = 0.727

Nearly 73% of the original gradient reaches c0c_0 through the cell state path. Now compare what would happen in a vanilla RNN. The multiplicative factor at each step would be tanh⁡′(zt)⋅whh\tanh'(z_t) \cdot w_{hh}. For typical values, suppose these factors are 0.70.7, 0.60.6, 0.650.65. Then:

∂L∂h0≈1.0⋅0.65⋅0.60⋅0.70=0.273\frac{\partial L}{\partial h_0} \approx 1.0 \cdot 0.65 \cdot 0.60 \cdot 0.70 = 0.273

Only 27% survives after three steps, and the decay rate is steeper per step. Extrapolating to 30 steps: if the RNN decay factor per step averages 0.640.64 and the LSTM forget gate product per step averages 0.900.90, then after 30 steps the RNN retains 0.6430≈10−50.64^{30} \approx 10^{-5} of the gradient while the LSTM cell retains 0.9030≈0.0420.90^{30} \approx 0.042. That is roughly a 4,000-fold difference in gradient magnitude.

What the Cell State Gradient Drives

The gradient ∂L/∂c0\partial L / \partial c_0 tells the optimizer how to adjust anything that influenced c0c_0 in order to reduce the loss. Since c0=0c_0 = 0 by convention (the initial cell state), this gradient acts on the initial state itself. In practice, many implementations make c0c_0 trainable: the optimizer can learn a better starting cell state for the task.

More importantly, the cell state gradient also flows back through the input gate and candidate cell state at step 1 to reach the weights that processed the very first input. This is how the LSTM achieves long-range credit assignment: the cell state acts as a near-lossless wire carrying the loss signal backward to wherever it needs to go.