Long Short-Term Memory (1997) › The Mathematics of Gating

Michael BrenndoerferMarch 7, 20256 min read
History of Language AIAll chapters
Part 2 of 9The Statistical Turn
Chapter 28Long Short-Term Memory (1997) › The Mathematics of Gating
Reading section 4 of 14 · The Mathematics of Gating · 6 minSection 4 of 14 · 6 min

Book contents

Section 4 of 14 · 6 min read

Long Short-Term Memory (1997) › The Mathematics of GatingOne section · Sections in this chapter4 / 14 ↓
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

To make these concepts precise, we need to examine the mathematical formulation of LSTM operations. While the architecture may appear complex at first, each component serves a clear purpose in the memory management system.

The forget gate computes:

ft=σ(Wf⋅[ht−1,xt]+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)

Here, σ\sigma denotes the sigmoid function, which maps its input to values between 0 and 1. The weight matrix WfW_f and bias bfb_f are learned parameters, while [ht−1,xt][h_{t-1}, x_t] represents the concatenation of the previous hidden state and current input. The resulting vector ftf_t contains values between 0 and 1 for each element of the cell state, indicating how much of that element should be retained.

The input gate operates in two stages. First, it determines which values to update:

it=σ(Wi⋅[ht−1,xt]+bi)i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i)

Then, a separate layer creates candidate values for addition to the cell state:

C~t=tanh⁡(WC⋅[ht−1,xt]+bC)\tilde{C}_t = \tanh(W_C \cdot [h_{t-1}, x_t] + b_C)

The tanh function produces values between -1 and 1, allowing both positive and negative updates to the cell state. The combination of iti_t and C~t\tilde{C}_t determines what new information gets incorporated.

With both forget and input gates computed, the cell state update becomes:

Ct=ft⊙Ct−1+it⊙C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t

where ⊙\odot denotes element-wise multiplication. This equation elegantly captures the memory update process: the previous cell state is scaled by the forget gate (discarding irrelevant information), and the new candidate values are scaled by the input gate (incorporating relevant new information), then these are combined through addition.

Finally, the output gate determines what gets exposed:

ot=σ(Wo⋅[ht−1,xt]+bo)o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o) ht=ot⊙tanh⁡(Ct)h_t = o_t \odot \tanh(C_t)

The cell state passes through a tanh to push values into the range between -1 and 1, then the output gate selectively filters this signal to produce the hidden state.

This mathematical structure explains why LSTMs solve the vanishing gradient problem. Notice that the cell state update includes an additive term: Ct=ft⊙Ct−1+it⊙C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t. During backpropagation, gradients can flow backward through this addition operation without passing through repeated multiplicative transformations that cause exponential growth or decay. The cell state provides a highway for gradient flow, while the gates learn when to let gradients through and when to block them. When the forget gate learns to output values near 1 for certain cell state elements, it creates a path for gradients to flow unimpeded across many time steps, enabling the learning of long-range dependencies that were impossible for simple RNNs.

Out[2]:
Visualization
LSTM cell diagram. A teal cell-state highway runs from C sub t minus 1 through a forget multiplication and an addition to C sub t. The previous hidden state h sub t minus 1 and current input x sub t join at a concatenate node, then feed forget, input, candidate, and output layers. The input gate and candidate multiply before entering the cell-state addition. The updated cell state passes through tanh and multiplies with the output gate to produce h sub t.
Information flow through an LSTM cell. Gates derived from the previous hidden state and current input decide what to forget, what candidate information to add, and what part of the updated cell state to expose as the new hidden state.