
14 July 2026
From RNN to Seq2Seq
In July I went back to the sequence models that came before the Transformer. RNN, GRU, LSTM and Seq2Seq form one chain, each built on the last: an RNN reads a sequence one step at a time, GRU and LSTM add gates so it remembers for longer, and Encoder–Decoder arranges them into "read one sentence, then write another". This post follows that order.
First, three terms that are easy to blur: Seq2Seq is the task, turning one variable-length sequence into another; Encoder–Decoder is the architecture; RNN, GRU and LSTM are the sequence units you can put inside that architecture.
RNN: one fixed rule, a changing state
An RNN reads a sequence step by step with the same parameters, squeezing everything read so far into its current hidden state. At step :
- : the current input
- : the previous state
- : the new state, mixing the past with the current input
- : parameters shared by every time step
One thing puzzled me at first: if every step uses the same , wouldn't every come out the same? It doesn't. is a fixed rule; is data that keeps changing. Even simplified to , a different going in gives a different result.
During training, is used at every step. BPTT (backpropagation through time) unrolls the network over time and adds up the gradient from each use:
Why an RNN forgets long sequences
A plain RNN remixes the old state with the new input at every step, with no explicit way to keep, delete or write. Once sequences get long, this breaks down:
- The hidden state has a fixed size, so the longer the history, the harder it gets squeezed.
- New information overwrites old information, and what you need later may already be gone.
- Gradients have to pass through a long recurrent chain, so they tend to vanish or explode.
- Even information that did get stored can't be looked up by content when needed, the way you'd look something up in a table.
Long-range dependencies really need two things: storing the information, and pulling out the right piece when it's needed. GRU and LSTM mainly help with the first. The second had to wait for attention.
GRU: two gates on the state update
A GRU still keeps a single hidden state, but uses two gates to decide how much old information to keep and how much new information to write.
The update gate controls how the old state and the candidate state are mixed:
A small copies more of the old state; a large writes more of the candidate . This direct copy path allows , which lets important information and gradients cross many steps more easily.
The reset gate controls how much of the past is used when making the candidate:
A large keeps drawing on the past; a small one weakens the old state, close to starting fresh.
Some textbooks swap the coefficients on the old state and the candidate in the update-gate formula. So don't memorize "0 means this, 1 means that". Check which term multiplies in the formula in front of you.
LSTM: a separate lane for long-term memory
Besides the hidden state , an LSTM keeps a cell state as a steadier channel for long-term memory. holds long-term information; is what this step shows to the outside. Three gates handle deleting, writing and reading.
The forget gate decides how much of the old cell state to keep. A dimension near 1 is kept; near 0 it's forgotten:
The input gate decides which parts of the candidate are worth writing:
Long-term memory is updated by addition: new memory = the old memory that was kept + the new information allowed in:
The output gate decides which parts of long-term memory become the current output:
Side by side
| Model | State | Gates | How it updates | Trade-offs |
|---|---|---|---|---|
| RNN | none | remixes old state and new input every step | simplest, fewest parameters, weakest long-range memory | |
| GRU | update, reset | chooses between keeping the old state and writing the candidate | simpler than LSTM, usually trains faster | |
| LSTM | forget, input, output | manages long-term memory and current output separately | finer control, more parameters and compute |
Neither GRU nor LSTM fully solves long sequences. They add a path close to the identity, which eases vanishing gradients and overwriting, but the history is still squeezed into a fixed-size state.
Bidirectional RNN
A one-way RNN at position only sees . A bidirectional RNN adds a second path running right to left, so every position gets context from both sides:
The two directions usually have their own parameters; it isn't the same run twice. It suits inputs you can see in full, such as a Seq2Seq encoder, sequence labeling, or feature extraction for speech and text. It can't serve directly as a real-time decoder, because when the current token is being generated, the later tokens don't exist yet. That's why Seq2Seq usually pairs a bidirectional encoder with a one-way decoder.
Encoder–Decoder: read one sentence, write another
Encoder–Decoder splits "understand the input" and "generate the output step by step" into two modules. It's especially good for tasks where input and output lengths differ, such as machine translation.
The encoder reads the whole input sequence step by step, using an RNN, GRU, LSTM or bidirectional RNN. Early Seq2Seq models simply took the encoder's last state as the context vector:
This vector is a compressed version of the whole input, and it initializes the decoder.
The decoder starts from and generates one token at a time, from <bos> until <eos>. Training usually uses teacher forcing: at each step the decoder is fed the true previous token. At inference there's no answer key, so the model feeds back the token it just generated, which makes generation autoregressive.
The biggest problem with the original Seq2Seq is asking one fixed-size vector to hold the entire input. A bidirectional encoder sees context on both sides, but as long as only one vector is passed on, long inputs still hit that bottleneck. This is exactly what attention changes: at every step the decoder can look back at all the encoder states and take what it needs, instead of relying only on the last one.