What is a recurrent neural network (RNN)?
A recurrent neural network (RNN) is a neural network that reads a sequence one element at a time and carries a hidden state from each step to the next. At every step it combines the previous hidden state with the current input using the same weights, so the hidden state acts as a running summary of everything read so far. The final state, or the state at each step, feeds an output layer.
RNNs were the main model for text, speech, and time series before Transformers. They powered early neural machine translation, speech recognition, handwriting recognition, and next-word prediction on phone keyboards. They still matter for streaming data and small devices because each new input costs a fixed amount of compute, and their training problems explain why LSTMs, GRUs, and attention were invented.
Key ideas
- An RNN updates a hidden state with h_t = tanh(W_h · h_(t−1) + W_x · x_t + b).
- The same weights are reused at every time step.
- The hidden state is a fixed-size summary of everything read so far.
- Training unrolls the network and runs backpropagation through time.
- Gradients shrink or grow exponentially with sequence length.
- Gated cells such as the LSTM and GRU were designed to keep long-range information.
Common questions
What is the difference between an RNN and a feedforward network?
A feedforward network maps one fixed-size input to one output and keeps no memory between inputs. An RNN has a loop: its output at one step is fed back in at the next step. That loop lets it handle inputs of any length and take earlier inputs into account.
Can an RNN handle sequences of any length?
In principle yes, since the same cell runs once per element. In practice a plain RNN loses information from more than a few dozen steps back because of vanishing gradients and the fixed-size state. LSTMs stretch this to hundreds of steps, and attention removes the bottleneck by looking back at every position directly.
Why do RNNs use tanh instead of ReLU?
The same weights are applied again at every step, so an unbounded activation like ReLU can let the hidden state grow out of control on long sequences. Tanh bounds each value between −1 and +1 and is centered at zero. ReLU RNNs can work with careful initialization, such as an identity W_h, but tanh is the default.
Are RNNs still used today?
Transformers have replaced them for most language tasks, but recurrent models are still used for streaming audio, on-device keyboards, and sensor data where memory and latency are tight. Recent state space models such as Mamba also keep a recurrent state, which lets them generate each new token at a fixed cost.
Related explainers
- LSTMs — Gates and memory cells for long sequences
- Vanishing & Exploding Gradients — Why deep networks struggle and how to fix it
- Backpropagation — How networks learn from their mistakes
- Seq2Seq & Beam Search — Encode an input sequence and decode an output — the architecture behind translation
- Embeddings — Turning words and categories into vectors
- The Transformer Architecture — A visual guide to 'Attention Is All You Need'
References
- Elman (1990), "Finding Structure in Time", Cognitive Science
- Bengio, Simard & Frasconi (1994), "Learning Long-Term Dependencies with Gradient Descent Is Difficult", IEEE Transactions on Neural Networks
- Pascanu, Mikolov & Bengio (2013), "On the Difficulty of Training Recurrent Neural Networks"
- Dive into Deep Learning, "Recurrent Neural Networks"
Last updated