Deep Learning · Sequence Modeling

LSTM Networks Explained: How Neural Networks Learn to Remember

Long Short-Term Memory networks solved one of deep learning's oldest problems: how to remember information across long sequences without the signal vanishing. This guide breaks down the architecture, the three gates, the math, and where LSTMs are still used in production today.

| By affordable AI, Nagpur
01 · The Problem LSTM Solves

Why plain RNNs forget everything

A standard Recurrent Neural Network (RNN) processes a sequence one step at a time, passing a hidden state forward like a note handed down a line of people. In theory this hidden state can carry information from many steps ago. In practice, it can't — not for long. Every time the note is copied through a tanh or sigmoid activation and multiplied by a weight matrix, the gradient used to update earlier weights shrinks a little more. Over dozens of time steps it shrinks to almost nothing. This is the vanishing gradient problem, and it means vanilla RNNs are effectively short-sighted: they can relate a word to the one before it, but not to one from three paragraphs earlier.

Long Short-Term Memory (LSTM) networks, introduced by Sepp Hochreiter and Jürgen Schmidhuber in 1997, fix this with a simple but powerful idea: separate what the network remembers from what the network outputs, and give it learned gates that decide what to keep, what to add, and what to expose at every single time step.

grad time steps → Vanilla RNN — gradient vanishes LSTM — gradient flows via the cell state
Figure 1 — Gradient strength across time steps: the cell state gives LSTM a near-uninterrupted path for gradients to flow backward.

02 · Architecture

The cell state: a conveyor belt for memory

The core of an LSTM is the cell state (often written Cₜ). Picture it as a conveyor belt that runs straight through every time step with only minor, deliberate edits made to it — no heavy matrix multiplications, no repeated squashing through activation functions. Because the path is so direct, gradients can travel back through many time steps without shrinking to zero. This is the single architectural choice that makes long-range memory possible.

Alongside the cell state, the LSTM also maintains a hidden state (hₜ), which is the short-term, filtered version of the cell state that gets passed to the next layer and used as the network's output at that time step. Three gates — forget, input, and output — control the traffic between these two states.

Inputs at each time step t xₜ → current input vector  |   hₜ₋₁ → previous hidden state  |   Cₜ₋₁ → previous cell state

03 · The Three Gates

How LSTM decides what to remember

Every gate is a small neural layer: a sigmoid function squashes its output between 0 and 1, acting like a dial rather than an on/off switch — 0 means "block everything," 1 means "let everything through," and anything in between is a partial pass.

×

1. Forget Gate

Looks at hₜ₋₁ and xₜ and decides what fraction of the old cell state to keep. A value near 0 erases that piece of memory; near 1 preserves it fully.

fₜ = σ(W_f · [hₜ₋₁, xₜ] + b_f)
+

2. Input Gate

Decides which new values to write into the cell state. A candidate layer proposes new content; the gate decides how much of it actually gets added.

iₜ = σ(W_i · [hₜ₋₁, xₜ] + b_i)
C̃ₜ = tanh(W_C · [hₜ₋₁, xₜ] + b_C)

3. Output Gate

Decides what part of the (now updated) cell state should be exposed as the hidden state — the value passed forward and used for predictions.

oₜ = σ(W_o · [hₜ₋₁, xₜ] + b_o)
hₜ = oₜ * tanh(Cₜ)

04 · Walkthrough

One time step, from input to output

01

Combine the new input with the previous hidden state

hₜ₋₁ and xₜ are concatenated and fed into all three gates plus the candidate layer, each with its own learned weight matrix.

02

Forget the irrelevant parts of memory

The old cell state is scaled by the forget gate's output: Cₜ₋₁ * fₜ. This removes information that is no longer useful for the task.

03

Add new, relevant information

The candidate values C̃ₜ are scaled by the input gate and added in: Cₜ = (Cₜ₋₁ * fₜ) + (iₜ * C̃ₜ).

04

Produce the filtered output

The updated cell state is squashed through tanh and scaled by the output gate to produce hₜ, which becomes both this step's output and the next step's short-term memory.

Cₜ₋₁ ───────────────────────────────▶ Cₜ × forget fₜ + iₜ · C̃ₜ tanh × hₜ output output oₜ
Figure 2 — A single LSTM cell: the forget gate scales old memory, the input gate adds new content, and the output gate filters what becomes hₜ.

05 · Comparison

LSTM vs. GRU vs. vanilla RNN

Gated Recurrent Units (GRUs) are a popular, lighter alternative to LSTM. Here's how the three architectures stack up.

Property Vanilla RNN LSTM GRU
Gates None 3 (forget, input, output) 2 (reset, update)
Separate cell state No Yes No — merged into hidden state
Long-range memory Poor Strong Strong, slightly shorter horizon
Parameters Fewest Most ~25% fewer than LSTM
Training speed Fastest Slower Faster than LSTM
Best for Very short sequences Long, complex dependencies Smaller datasets, faster iteration

06 · Real-World Use

Where LSTMs are used in production

Even with Transformers dominating headlines, LSTMs remain a strong, resource-efficient choice for sequential data — especially when sequences are long, data is limited, or low-latency inference matters.

01

Language Modeling

Next-word prediction, text generation, and early machine translation systems before attention-based models.

02

Time Series Forecasting

Stock price trends, energy demand, weather, and sensor data where order and long-term seasonality matter.

03

Speech Recognition

Converting audio waveforms into text by modeling phoneme sequences over time.

04

Anomaly Detection

Flagging unusual patterns in server logs, network traffic, or industrial sensor readings.

05

Music Generation

Learning melodic and rhythmic structure to generate new musical sequences note by note.

06

Handwriting Recognition

Modeling pen-stroke sequences to transcribe handwritten text.

07

Video Activity Recognition

Combining CNN features per frame with an LSTM to model motion over time.

08

Predictive Text & Autocomplete

On-device keyboards that suggest the next word based on typing history.


07 · Trade-offs

Advantages and limitations

Advantages

  • Handles long-range dependencies far better than vanilla RNNs
  • Gating mechanism gives fine-grained control over what's remembered
  • Works well with limited training data compared to large Transformers
  • Lower memory and compute footprint at inference time — good for edge devices

Limitations

  • Sequential computation — can't parallelize across time steps like Transformers
  • Still struggles with extremely long sequences (thousands of steps)
  • More parameters and slower to train than GRUs
  • Largely outperformed by attention-based models on large-scale NLP tasks

08 · Implementation

A minimal LSTM in Keras

Here's a compact, practical example of building an LSTM-based sequence classifier using TensorFlow/Keras.

python — sequence_classifier.py
# 1. Import the building blocks from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense, Embedding, Dropout # 2. Define the model model = Sequential([ Embedding(input_dim=10000, output_dim=128), LSTM(64, return_sequences=False), # 64 memory units Dropout(0.3), Dense(32, activation='relu'), Dense(1, activation='sigmoid') # binary output ]) # 3. Compile model.compile( optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'] ) # 4. Train model.fit(X_train, y_train, epochs=10, batch_size=32, validation_split=0.2)

09 · FAQ

Frequently asked questions

Is LSTM still relevant with Transformers around?

Yes, particularly for on-device, low-latency, or resource-constrained settings, and for tasks like time-series forecasting where sequence length is manageable and data is limited.

How many LSTM units should I use?

There's no universal number — start with 32–128 units for small-to-medium tasks and tune based on validation performance and overfitting.

What's the difference between "cell state" and "hidden state"?

The cell state is the long-term memory carried across time steps with minimal transformation. The hidden state is the filtered, short-term output derived from it at each step.

Can LSTM handle bidirectional context?

Yes — a Bidirectional LSTM runs two LSTMs (forward and backward) and combines their outputs, which is common in NLP tasks like named entity recognition.