Certainly! Let’s dive into how LSTM (Long Short-Term Memory) networks address the vanishing gradient problem in recurrent neural networks (RNNs), with a mathematical explanation.
Background: The Vanishing Gradient Problem in RNNs
In standard RNNs, the hidden state at time ( t ) is updated as:
[
h_t = \phi(W_{hh} h_{t-1} + W_{xh} x_t + b_h)
]
where ( \phi ) is a nonlinearity (e.g., tanh).
When training via backpropagation through time (BPTT), the gradient of the loss ( L ) with respect to parameters involves products of derivatives across many time steps:
[
\frac{\partial L}{\partial h_t} = \frac{\partial L}{\partial h_T} \prod_{k=t+1}^{T} \frac{\partial h_k}{\partial h_{k-1}}
]
If the Jacobian ( \frac{\partial h_k}{\partial h_{k-1}} ) has eigenvalues less than 1, repeated multiplication causes the gradient to vanish exponentially as ( T-t ) increases.
LSTM Architecture
LSTM introduces a cell state ( c_t ) and three gates: input (( i_t )), forget (( f_t )), and output (( o_t )). The key equations are:
[
\begin{align*}
f_t &= \sigma(W_f x_t + U_f h_{t-1} + b_f) \
i_t &= \sigma(W_i x_t + U_i h_{t-1} + b_i) \
o_t &= \sigma(W_o x_t + U_o h_{t-1} + b_o) \
\tilde{c}t &= \tanh(W_c x_t + U_c h{t-1} + b_c) \
c_t &= f_t \odot c_{t-1} + i_t \odot \tilde{c}_t \
h_t &= o_t \odot \tanh(c_t)
\end{align*}
]
where ( \odot ) denotes element-wise multiplication.
How LSTM Mitigates the Vanishing Gradient
1. The Cell State as a "Highway"
The cell state ( c_t ) is updated as:
[
c_t = f_t \odot c_{t-1} + i_t \odot \tilde{c}t
]
If the forget gate ( f_t ) is close to 1 and the input gate ( i_t ) is close to 0, the cell state is copied forward with little change:
[
c_t \approx c{t-1}
]
This allows information (and gradients) to flow unchanged across many time steps.
2. Gradient Flow Through the Cell State
Let’s compute the gradient of the loss ( L ) with respect to the cell state at time ( t ):
[
\frac{\partial L}{\partial c_t}
]
The gradient with respect to ( c_{t-1} ) is:
[
\frac{\partial L}{\partial c_{t-1}} = \frac{\partial L}{\partial c_t} \cdot \frac{\partial c_t}{\partial c_{t-1}}
]
From the update equation:
[
\frac{\partial c_t}{\partial c_{t-1}} = f_t
]
So, recursively:
[
\frac{\partial L}{\partial c_{t-k}} = \frac{\partial L}{\partial c_t} \prod_{j=1}^{k} f_{t-j+1}
]
If ( f_t \approx 1 ), the product remains close to 1, preventing vanishing gradients.
3. Comparison to Standard RNNs
In standard RNNs, the equivalent term is:
[
\prod_{j=1}^{k} W_{hh}^T \cdot \text{diag}(\phi'(h_{t-j}))
]
If the spectral norm of ( W_{hh} ) and the derivative of ( \phi ) are less than 1, the product shrinks exponentially.
In LSTM, the forget gate ( f_t ) is learned and can be set close to 1, so the product does not necessarily shrink.
Summary Table
| Model | Gradient Flow Across Time Steps | Vanishing Gradient? |
|---|
| RNN | ( \prod W_{hh}^T \phi'(h) ) | Yes (if < 1) |
| LSTM | ( \prod f_t ) | No (if ( f_t \approx 1 )) |
Conclusion
LSTM solves the vanishing gradient problem by introducing a cell state with a nearly linear update (when ( f_t \approx 1 )), allowing gradients to flow unchanged across many time steps. The forget gate ( f_t ) is learned and can be set to values close to 1, so the gradient does not vanish, unlike in standard RNNs.
Mathematically:
[
\frac{\partial L}{\partial c_{t-k}} = \frac{\partial L}{\partial c_t} \prod_{j=1}^{k} f_{t-j+1}
]
If ( f_t \approx 1 ), the product is stable, and the gradient does not vanish.
References:
- Hochreiter, S., & Schmidhuber, J. (1997). "Long Short-Term Memory." Neural Computation.
- Goodfellow, I., Bengio, Y., & Courville, A. (2016). "Deep Learning." (Section 10.10.1)