Understanding Long Short-Term Memory (LSTM) Networks

Long Short-Term Memory (LSTM) networks are a specialized type of recurrent neural network (RNN) designed to address the vanishing gradient problem and capture long-range dependencies in sequential data. At their core, LSTMs manage information flow through a system of gates and memory states, enabling precise control over what is retained, updated, and output over time. This article provides a comprehensive breakdown of LSTM mechanics, focusing on their gates, hidden and cell states, mathematical foundations, and practical applications.

LSTM Diagram

Core Components of an LSTM

An LSTM processes sequential data one timestep at a time, using three critical gatesβ€”forget gate, input gate, and output gateβ€”to regulate the flow of information. These gates work in tandem with two internal states: the hidden state and the cell state.

1. The Forget Gate: Filtering Irrelevant Information

The forget gate determines which parts of the long-term memory (cell state) should be discarded or retained. It takes the previous hidden state htβˆ’1h_{t-1} and the current input xtx_t, concatenates them, and applies a sigmoid activation to produce values between 0 and 1. These values act as filters:

  • Values close to 1 indicate information to keep.
  • Values close to 0 indicate information to discard.

For example, in a sentence like "The cat, which was hungry, sat on the mat," the forget gate might retain "cat" and "hungry" while discarding less relevant details as the sentence progresses. Mathematically, this is expressed as:

ft=Οƒ(Wfβ‹…[htβˆ’1,xt]+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)

Here, ftf_t is the forget gate’s output, WfW_f and bfb_f are learnable weights and biases, and Οƒ\sigma is the sigmoid function. The previous cell state Ctβˆ’1C_{t-1} is then multiplied element-wise by ftf_t, selectively erasing outdated information.

2. The Input Gate: Updating Memory with New Information

The input gate decides what new information to add to the cell state. It has two components:

  1. A sigmoid layer that identifies which values to update.
  2. A tanh layer that generates candidate values C~t\tilde{C}_t for addition to the cell state.

The updated cell state CtC_t combines the filtered past memory (from the forget gate) and the new candidate values:

Ct=ftβŠ™Ctβˆ’1+itβŠ™C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t

Here, iti_t is the output of the input gate’s sigmoid layer, and βŠ™\odot denotes element-wise multiplication. This step ensures the cell state evolves by integrating relevant new context while preserving essential long-term information.

3. The Output Gate: Generating the Hidden State

The output gate controls what information from the cell state is exposed as the hidden state hth_t. It uses a sigmoid layer to decide which parts of the cell state to highlight, then multiplies this by a tanh-scaled version of the cell state:

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 hidden state serves as a filtered summary of the cell state, containing only the information relevant to the current timestep. For instance, in a translation task, hth_t might focus on the subject of a sentence while downplaying less critical details.

Hidden State vs. Cell State: Roles and Interactions

Hidden State hth_t: The Short-Term Messenger

The hidden state acts as the LSTM’s interface with the external world. At each timestep, it is passed to the next layer or used for predictions, encapsulating the immediate context needed for the task. Think of hth_t as a snapshot of the cell state after being refined by the output gateβ€”a concise representation of what matters "right now."

Cell State CtC_t: The Long-Term Memory Bank

The cell state functions as the LSTM’s persistent memory, carrying information across many timesteps. It is updated sequentially but never directly exposed, allowing it to maintain a coherent narrative of the sequence. For example, in a story generation task, CtC_t might track overarching plot points, while hth_t focuses on the current sentence.

Mathematical Foundations

LSTMs rely on the interplay of sigmoid and tanh activations to balance information retention and flow:

  • Sigmoid Οƒ\sigma: Squashes values to [0, 1], ideal for gating (e.g., deciding what to forget).
  • Tanh: Squashes values to [-1, 1], normalizing candidate values for stable training.

alt text alt text alt text

The equations governing an LSTM cell are:

  1. Forget Gate:
ft=Οƒ(Wfβ‹…[htβˆ’1,xt]+bf)f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)
  1. Input Gate and Candidate Memory:
it=Οƒ(Wiβ‹…[htβˆ’1,xt]+bi),C~t=tanh⁑(WCβ‹…[htβˆ’1,xt]+bC)i_t = \sigma(W_i \cdot [h_{t-1}, x_t] + b_i), \quad \tilde{C}_t = \tanh(W_C \cdot [h_{t-1}, x_t] + b_C)
  1. Cell State Update:
Ct=ftβŠ™Ctβˆ’1+itβŠ™C~tC_t = f_t \odot C_{t-1} + i_t \odot \tilde{C}_t
  1. Output Gate and Hidden State:
ot=Οƒ(Woβ‹…[htβˆ’1,xt]+bo),ht=otβŠ™tanh⁑(Ct)o_t = \sigma(W_o \cdot [h_{t-1}, x_t] + b_o), \quad h_t = o_t \odot \tanh(C_t)

Why the Hidden State is Output, Not the Cell State

The cell state CtC_t contains raw, unfiltered memory, which is often too voluminous or irrelevant for immediate tasks. Exposing it directly could overwhelm downstream layers with unnecessary details. Instead, the hidden state hth_t provides a distilled version of CtC_t, emphasizing context critical to the current timestep. This design mirrors human cognition: while our brains store vast amounts of information, we consciously focus only on what’s immediately relevant.

Applications of LSTMs

LSTMs excel in tasks requiring context retention over long sequences:

  1. Time Series Forecasting: Predicting stock prices or weather patterns by modeling temporal dependencies.
  2. Natural Language Processing (NLP): Machine translation, sentiment analysis, and text generation.
  3. Speech Recognition: Converting audio signals into text by processing phoneme sequences.
  4. Healthcare: Analyzing patient vitals over time for early disease detection.

LSTMs solve the limitations of traditional RNNs through a gated architecture that balances short-term relevance and long-term memory. The forget gate filters outdated information, the input gate integrates new data, and the output gate generates context-aware hidden states. Together, the hidden state hth_t and cell state CtC_t enable LSTMs to handle complex sequential tasks, from language modeling to predictive analytics. By understanding these mechanics, practitioners can better leverage LSTMs in applications demanding robust temporal or contextual understanding.