Recurrent Neural Networks
Recurrent Neural Networks (RNNs) are a class of deep learning models designed specifically for sequential data. Unlike conventional feed-forward networks, which treat each input as an isolated event, RNNs maintain an internal state that allows information to persist across time steps. This memory-like capability makes them indispensable in domains such as natural language processing, time-series forecasting, and speech recognition, where the meaning of the current input often depends on what came before.
At its heart, an RNN can be visualized as a neural network unrolled across time. At every time step t, the network takes the current input x__t and the hidden state from the previous step ht-1, then computes a new hidden state h__t. A standard formulation looks like this:
$$h_t = \tanh(W_{hh}h_{t-1} + W_{xh}x_t + b_h)$$
Here, Whh governs transitions between hidden states, Wxh maps the input to the hidden space, and _b_h is a bias term. The hyperbolic tangent activation introduces nonlinearity while keeping activations bounded. Because each hidden state feeds into the next, the network forms a chain of dependencies that lets it retain contextual information—for example, when interpreting a word in a sentence, earlier words can shape its meaning.
Vanishing and Exploding Gradients
In theory, RNNs can remember information over long horizons. In practice, training them is difficult because of the vanishing and exploding gradient problems.
During backpropagation through time, gradients are propagated backward across time steps using the chain rule. If the spectral norm of the recurrent weight matrix Whh is less than one, gradients shrink exponentially as they move through the sequence, making it nearly impossible for the network to learn long-range dependencies. Conversely, if the norm exceeds one, gradients can grow exponentially, destabilizing training or causing divergence.
To address these limitations, researchers introduced gated architectures such as Long Short-Term Memory networks (LSTMs) and Gated Recurrent Units (GRUs).
LSTM and GRU: Gated Improvements
LSTMs solve the gradient problem through a sophisticated gating mechanism and an internal cell state. Three primary gates regulate the flow of information:
- Forget gate: decides which parts of the cell state should be discarded.
- Input gate: controls how much new information is written into the cell state.
- Output gate: determines what portion of the cell state is exposed as the output.
GRUs offer a streamlined alternative. They merge the forget and input gates into a single update gate and add a reset gate to control how much past information is forgotten. With fewer parameters, GRUs often train faster than LSTMs while delivering comparable or even better performance on many tasks.
Implementation Example with PyTorch
Below is a minimal PyTorch implementation of a simple RNN layer:
import torch
import torch.nn as nn
class SimpleRNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super(SimpleRNN, self).__init__()
self.hidden_size = hidden_size
self.rnn = nn.RNN(input_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, output_size)
def forward(self, x):
# x shape: (batch_size, seq_len, input_size)
h0 = torch.zeros(1, x.size(0), self.hidden_size)
out, h_n = self.rnn(x, h0)
out = self.fc(h_n[-1])
return out
input_size = 10
hidden_size = 32
output_size = 5
model = SimpleRNN(input_size, hidden_size, output_size)
x = torch.randn(4, 5, input_size) # batch_size=4, seq_len=5
output = model(x)
print(output.shape) # Expected: torch.Size([4, 5])
Applications and Model Selection
RNNs and their variants suit any scenario involving sequential or temporally dependent data. Common use cases include:
- Natural language processing: machine translation, text generation, sentiment analysis.
- Time-series forecasting: stock price prediction, weather forecasting, sensor monitoring.
- Speech recognition: converting audio signals into text.
When choosing a model, a standard RNN may suffice for short sequences with simple dependencies. For longer sequences with long-range dependencies, LSTMs or GRUs are generally preferable. It is also worth noting that attention-based models such as Transformers have largely superseded RNNs in many natural language processing benchmarks. Still, RNNs retain unique value in resource-constrained environments and real-time edge computing scenarios where their recurrent structure and relatively modest computational footprint remain advantageous.