Recurrent Neural Networks
Recurrent Neural Networks (RNNs) are a class of neural networks designed to process sequential data by maintaining a hidden state that is updated at each time step, allowing information to persist across the sequence. They are foundational to modern sequence modeling in NLP, speech, and time-series analysis, enabling models to condition outputs on arbitrarily long input histories. A counterintuitive fact: despite being designed to capture long-range dependencies, vanilla RNNs are theoretically proven to forget information exponentially fast as sequence length grows — the very problem they were invented to solve.
The "aha moment" for RNNs is that a neural network can be made to process sequences by feeding the same set of weights repeatedly across time steps, while passing a compressed summary — the hidden state — from one step to the next. Think of it as a single neural network being unrolled into a chain, where each copy shares weights but receives new input and the memory of everything that came before. This weight sharing is what makes the model generalize across time positions, just as convolutional weight sharing generalizes across spatial positions. The hidden state is the network's working memory: it is the only bridge between past and future.
How It Works
At each time step , an RNN takes an input vector and the previous hidden state and produces a new hidden state . All time steps share the same weight matrices.
Explanation: The new hidden state is a nonlinear compression of two linear signals — one carrying memory from the past hidden state, one carrying the current input.
Symbols:
- — hidden state vector at step , dimension is a hyperparameter
- — recurrent weight matrix (past → present)
- — input weight matrix; is input dimension
- — bias vector
- — element-wise hyperbolic tangent, squashing values to
The output at each step (e.g., for sequence-to-sequence or language modeling) is:
Explanation: The hidden state is linearly projected and normalized into a probability distribution over output classes at each time step.
Symbols:
- — output weight matrix; is vocabulary or class size
- — output bias
The loss over an entire sequence of length is summed cross-entropy:
Explanation: The total sequence loss is the sum of the negative log-probability assigned to the correct target at every time step.
Symbols:
- — ground-truth class index at step
- — the model's predicted probability for the correct class
Training uses Backpropagation Through Time (BPTT), which unrolls the computation graph across all steps and applies the chain rule. The gradient of the loss with respect to the hidden state at an earlier step involves a product of Jacobians:
Explanation: The gradient must be multiplied through every time step between and , so it is scaled by the same recurrent weight matrix repeated times, causing exponential decay or explosion.
Symbols:
- — diagonal Jacobian of tanh; entries are
- The product of matrices each with spectral radius (typical after tanh) causes vanishing gradients
A minimal PyTorch implementation illustrates the core loop: ```python import torch import torch.nn as nn class VanillaRNN(nn.Module): def __init__(self, input_size: int, hidden_size: int, output_size: int): super().__init__() self.hidden_size = hidden_size
# Fused input+hidden linear for efficiency self.rnn_cell = nn.RNNCell(input_size, hidden_size, nonlinearity='tanh') self.output_layer = nn.Linear(hidden_size, output_size) def forward(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
# x: (seq_len, batch, input_size) seq_len, batch, _ = x.shape h = torch.zeros(batch, self.hidden_size, device=x.device) # h_0 outputs = [] for t in range(seq_len): h = self.rnn_cell(x[t], h) # Eq.[1] applied here outputs.append(self.output_layer(h)) # Eq.[2] applied here return torch.stack(outputs, dim=0), h # (seq_len, batch, output_size), final h ```
Sepp Hochreiter, a German computer scientist, identified and rigorously characterized the vanishing and exploding gradient problem in recurrent networks in his 1991 diploma thesis — years before it was widely recognized. Together with Jürgen Schmidhuber, he proposed the Long Short-Term Memory (LSTM) architecture in 1997, which introduced gating mechanisms specifically designed to let gradients flow over long sequences. Remarkably, Hochreiter's 1991 analysis was written in German and remained largely unknown to the English-speaking community for nearly a decade.
Yoshua Bengio, a Canadian AI researcher and 2018 Turing Award winner, co-authored the landmark 1994 paper "Learning Long-Term Dependencies with Gradient Descent is Difficult," providing the first rigorous mathematical proof that gradient-based methods struggle with long-range dependencies in RNNs. His work shifted the community's understanding from treating training difficulty as a practical nuisance to a fundamental mathematical barrier. Bengio is also known for his foundational contributions to word embeddings and deep learning theory, and he co-founded Mila — the Quebec AI Institute.
Intuition
Imagine you are reading a novel, but you can only keep a small sticky note of reminders as you turn each page. At every new page, you read the page's content and update your sticky note — erasing some old notes, adding new ones. At the end, everything you know about the story is compressed into whatever fits on that sticky note. That sticky note is the RNN's hidden state. The process of reading each page and rewriting the sticky note is exactly Equation [1]. The crucial insight is that the same sticky-note-update rules are applied at every page: the RNN doesn't have different "reading strategies" for page 1 versus page 200 — it reuses the same weights everywhere. This is weight sharing across time. Now imagine the catch: your sticky note can only hold 10 bullet points, and every time you update it, old bullets tend to get smudged and overwritten. By the time you reach page 200, almost nothing from page 1 survives legibly. This is vanishing gradients — the model loses the ability to learn from distant past context. LSTMs fix this by giving you a laminated card alongside your sticky note, one where you can deliberately choose what to protect and what to erase using gates.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Long Short-Term Memory (LSTM) · Gated Recurrent Unit (GRU) · Backpropagation Through Time · Attention Mechanism · Transformer Architecture