🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
Search courses…
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

RNN, LSTM and GRU — Sequential Data Complete Guide

Deep LearningRNNs🟢 Free Lesson

Advertisement

Deep Learning

RNNs and LSTMs — Neural Networks That Remember

Explore recurrent neural networks designed to process sequential data with memory of past inputs.

  • Sequential processing — handle time series and text data
  • LSTM gates — solve the vanishing gradient problem
  • GRU simplification — efficient recurrent architectures

Memory is the diary we all carry about with us.

RNN, LSTM and GRU — Complete Guide

Recurrent networks process sequential data by maintaining a hidden state that carries information across time steps. Unlike Transformers, they process one token at a time with memory per step but sequential operations for a sequence of length .


Vanilla RNN

At each time step , the RNN computes:

Unrolled RNN over Timet-1RNNCelltanh(W·[h,x]+b)xt-1tRNNCelltanh(W·[h,x]+b)xtytt+1RNNCelltanh(W·[h,x]+b)xt+1yt+1TRNNCelltanh(W·[h,x]+b)xTyTht-1htht+1Same weights W, W• The RNN cell is the same function applied at every time step (weight sharing across time)• Hidden state ht ∈ ℝd encodes all information about the past (fixed-size bottleneck)Sequence length n determines the number of unrolled steps — cannot parallelize!• Problem: Gradient ∂L/∂ht = ∏ ∂hk/∂hk-1 → 0 exponentially (vanishing gradient)

How the RNN processes sequences: This diagram shows an RNN "unrolled" across time steps. At each step t, the RNN cell takes two inputs: the current input x_t (e.g., a word in a sentence) and the previous hidden state h_{t-1} (memory of all past inputs). It combines them through W·[h,x]+b and applies tanh to produce the new hidden state h_t and output y_t. The crucial detail: the SAME weights W are used at every time step — this is weight sharing across time, which means the model learns a single function that works regardless of sequence length. The hidden state h_t acts as a compressed memory of everything seen so far. The information bottleneck problem is visible: all past information must fit into the fixed-size vector h_t. The red text at the bottom highlights the critical flaw: during backpropagation, gradients multiply through the chain of time steps, causing them to vanish (or explode) exponentially, making it impossible to learn long-range dependencies beyond ~10-20 steps.


LSTM (Long Short-Term Memory)

LSTM (Hochreiter and Schmidhuber, 1997) introduces a cell state as an information highway, with three gates controlling information flow:

LSTM Cell ArchitectureCell State ct — Information HighwayForgetGateσ(W·[ht-1,xt])×ct-1InputGateσ(W·[ht-1,xt])Candidatetanh(·)t×+ct = ft⊙ct-1 + it⊙c̃tOutputGateσ(W·[ht-1,xt])×tanh(ct)htLSTM EquationsForget:ft = σ(Wf·[ht-1, xt] + bf)Input:it = σ(Wi·[ht-1, xt] + bi), c̃t = tanh(Wc·[ht-1, xt] + bc)Cell:ct = ft ⊙ ct-1 + it ⊙ c̃tOutput:ot = σ(Wo·[ht-1, xt] + bo), ht = ot ⊙ tanh(ct)

GRU (Gated Recurrent Unit)

GRU (Cho et al., 2014) simplifies LSTM by merging the cell and hidden state and using only two gates:

GRU Cell Architecturextht-1ResetGate (r)UpdateGate (z)Candidateh̃ = tanh(W·[r⊙h,x])Interpolateht = (1-z)⊙ht-1 + z⊙h̃thtoutput at time t

Bidirectional RNN

Processes the sequence in both directions and concatenates hidden states:

Use case: NER, sentiment analysis — where full context is available. Cannot be used for autoregressive generation.


Sequence-to-Sequence Architecture

Encoder-Decoder (Seq2Seq)EncoderThecatsath₁h₂h₃Contexth₃Decoder<S>Ilgattosedevas₀s₁s₂s₃Output: "Il gatto sedeva"Bottleneck ProblemEntire input sequence compressed into fixed-size vector hnLong sequences lose information → Attention mechanism solves thisTeacher forcing: feed ground-truth as input during training

PyTorch Implementation


Key Takeaways


What to Learn Next

-> Transformers Learn the architecture replacing RNNs.

-> NLP Fundamentals Master natural language processing basics.

-> Time Series Analysis Apply RNNs to time-dependent data.

-> Attention Deep Dive Understand how attention solves the bottleneck.

-> Neural Networks Understand the foundation of deep learning.

-> Sequence-to-Sequence Build models for translation and summarization.

Need Expert Machine Learning Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement