What a recurrent neural network is
A recurrent neural network (RNN) is a type of neural network built for data that arrives in order, such as sentences, audio, stock prices, or sensor readings. Instead of treating each input as separate, it carries a memory of what it has already seen. The main types are the simple RNN, the long short-term memory network (LSTM), the gated recurrent unit (GRU), and bidirectional and stacked versions of these. LSTMs and GRUs are the variants you are most likely to meet in practice, because they handle longer sequences better than the simple form.
How a recurrent network reads a sequence
A recurrent network handles one step of a sequence at a time. At each step it takes the current input, combines it with a hidden state that summarizes everything seen so far, and produces an updated hidden state and, often, an output. The same weights are reused at every step. That reuse is why one network can handle sequences of different lengths.
The simple (vanilla) RNN
The simple RNN is the basic form. Its hidden state is updated with one simple nonlinear step at each point in the sequence. Early versions, such as the simple recurrent network Jeffrey Elman described in 1990, were used to study patterns in language and other sequences.
In practice, simple RNNs struggle with long-range dependencies. During training, the error signals that flow back through many steps tend either to shrink toward zero, which is called the vanishing gradient problem, or to grow very large, which is called exploding gradients. The result is that the network loses track of the early parts of a long sequence. Gradient clipping, which caps the size of each update, is a common fix for exploding gradients. The vanishing gradient problem is a main reason the gated designs below were developed.
Long short-term memory (LSTM) networks
Hochreiter and Schmidhuber introduced the LSTM in 1997. It adds a cell state, a separate memory that can carry information across many steps, along with three gates that control it:
- The forget gate decides what to discard from the cell state.
- The input gate decides what new information to store.
- The output gate decides which part of the cell state feeds into the hidden state.
Because the cell state can pass through many steps with only small changes, gradients have an easier path during training. LSTMs are more complex and slower per step than simple RNNs, but they usually handle long-range dependencies much better.
Gated recurrent units (GRUs)
The GRU, introduced by Cho and colleagues in 2014, is a simpler gated design with two gates:
- The update gate controls how much of the old state to keep.
- The reset gate controls how much of the past to use when forming the new candidate state.
GRUs have no separate cell state, so they have fewer parameters than an LSTM. On many tasks they often perform similarly to LSTMs. Which one works better depends on the data, so it is worth trying both.
Bidirectional RNNs
A bidirectional RNN runs two recurrent layers over the same sequence. One reads from start to end, and the other reads from end to start. Their outputs are combined, so each position can use context from both before and after it. This suits tasks where the full input is available at once, such as tagging each word in a sentence. It does not suit live prediction, where the future of the sequence is not yet known.
Stacked RNNs
A stacked RNN places several recurrent layers on top of each other, with the output sequence of one layer feeding the next. Each layer can pick up patterns at a different level of detail, and stacking is one way to make a recurrent model deep. Extra layers add computation and can make training harder, so stacks are often kept fairly shallow.
Input and output arrangements
RNNs can be wired to match the shape of the task:
- One to one: a single input and a single output, much like an ordinary network.
- One to many: one input produces a sequence, such as generating a caption from a single image.
- Many to one: a sequence goes in and one output comes out, such as labeling a movie review as positive or negative.
- Many to many: sequences go in and sequences come out. Translation is one example, where the output can have a different length. Labeling every frame of a video is another, where the output lines up with the input.
Encoder-decoder (sequence-to-sequence) models
An encoder-decoder model pairs two recurrent networks. The encoder reads the input sequence and compresses it into a final state. The decoder starts from that state and generates the output one step at a time. Systems built this way were a major step in neural machine translation around 2014. One weakness was that a single fixed-size state had to hold the whole input. Attention mechanisms were added to address that, and attention later became the core of transformers.
Where RNNs fit today
Transformers, introduced in 2017, use attention to relate every position in a sequence to every other position, without a step-by-step loop. They can be trained in parallel more easily than RNNs, and they now dominate many language tasks. Learners often ask, “Is ChatGPT a RNN?” The short answer is no, because ChatGPT is built on a transformer architecture.
RNN variants still show up where data arrives one piece at a time, where memory or computing power is tight, and in some smaller time-series problems. Understanding how they work also explains why attention was such a change.
Choosing a type
A few rules of thumb help when you are starting out:
- Try a GRU or an LSTM first for a sequence task with modest data. GRUs are often the lighter choice.
- Use a bidirectional model when the whole sequence is available up front and you need context from both sides.
- Keep the simple RNN for teaching and for very short sequences.
- For large language tasks or other long-range problems with plenty of data and compute, transformers are often the stronger option.
Whichever you choose, compare it against a simple baseline on held-out data before deciding. That comparison will tell you more than any general rule.