Skip to content
King Jin

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 tt:

ht=tanh⁡(Wxxt+Whht−1+b)h_t=\tanh(W_xx_t+W_hh_{t-1}+b)
  • xtx_t: the current input
  • ht−1h_{t-1}: the previous state
  • hth_t: the new state, mixing the past with the current input
  • Wx,Wh,bW_x,W_h,b: parameters shared by every time step

One thing puzzled me at first: if every step uses the same WhW_h, wouldn't every hth_t come out the same? It doesn't. WhW_h is a fixed rule; hth_t is data that keeps changing. Even simplified to ht+1=Whhth_{t+1}=W_hh_t, a different hth_t going in gives a different result.

During training, WhW_h is used at every step. BPTT (backpropagation through time) unrolls the network over time and adds up the gradient from each use:

∂J(t)∂Wh=∑i=1t∂J(t)∂Wh∣(i)\frac{\partial J^{(t)}}{\partial W_h} =\sum_{i=1}^{t} \left.\frac{\partial J^{(t)}}{\partial W_h}\right|_{(i)}

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:

  1. The hidden state has a fixed size, so the longer the history, the harder it gets squeezed.
  2. New information overwrites old information, and what you need later may already be gone.
  3. Gradients have to pass through a long recurrent chain, so they tend to vanish or explode.
  4. 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 ztz_t controls how the old state and the candidate state are mixed:

ht=(1−zt)⊙ht−1+zt⊙h~th_t=(1-z_t)\odot h_{t-1}+z_t\odot \tilde h_t

A small ztz_t copies more of the old state; a large ztz_t writes more of the candidate h~t\tilde h_t. This direct copy path allows ht≈ht−1h_t\approx h_{t-1}, which lets important information and gradients cross many steps more easily.

The reset gate rtr_t controls how much of the past is used when making the candidate:

h~t=tanh⁡(Wxxt+Wh(rt⊙ht−1))\tilde h_t=\tanh\left(W_xx_t+W_h(r_t\odot h_{t-1})\right)

A large rtr_t 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 ztz_t multiplies in the formula in front of you.

LSTM: a separate lane for long-term memory

Besides the hidden state hth_t, an LSTM keeps a cell state ctc_t as a steadier channel for long-term memory. ctc_t holds long-term information; hth_t 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:

ft=σ(Wf[xt,ht−1]+bf)f_t=\sigma(W_f[x_t,h_{t-1}]+b_f)

The input gate decides which parts of the candidate are worth writing:

it=σ(Wi[xt,ht−1]+bi),c~t=tanh⁡(Wc[xt,ht−1]+bc)i_t=\sigma(W_i[x_t,h_{t-1}]+b_i),\qquad \tilde c_t=\tanh(W_c[x_t,h_{t-1}]+b_c)

Long-term memory is updated by addition: new memory = the old memory that was kept + the new information allowed in:

ct=ft⊙ct−1+it⊙c~tc_t=f_t\odot c_{t-1}+i_t\odot\tilde c_t

The output gate decides which parts of long-term memory become the current output:

ot=σ(Wo[xt,ht−1]+bo),ht=ot⊙tanh⁡(ct)o_t=\sigma(W_o[x_t,h_{t-1}]+b_o),\qquad h_t=o_t\odot\tanh(c_t)

Side by side

ModelStateGatesHow it updatesTrade-offs
RNNhth_tnoneremixes old state and new input every stepsimplest, fewest parameters, weakest long-range memory
GRUhth_tupdate, resetchooses between keeping the old state and writing the candidatesimpler than LSTM, usually trains faster
LSTMht,cth_t,c_tforget, input, outputmanages long-term memory and current output separatelyfiner 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 tt only sees x1,…,xtx_1,\ldots,x_t. A bidirectional RNN adds a second path running right to left, so every position gets context from both sides:

h→t=RNN⁡→(xt,h→t−1),h←t=RNN⁡←(xt,h←t+1),ht=[h→t;h←t]\overrightarrow h_t=\operatorname{RNN}_{\rightarrow}(x_t,\overrightarrow h_{t-1}),\qquad \overleftarrow h_t=\operatorname{RNN}_{\leftarrow}(x_t,\overleftarrow h_{t+1}),\qquad h_t=[\overrightarrow h_t;\overleftarrow h_t]

The two directions usually have their own parameters; it isn't the same WhW_h 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.

Iliketea<eos><bos>我我喜欢喜欢茶茶<eos>cfixed sizeEncoderDecoder

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:

c=hTencc=h_T^{\mathrm{enc}}

This vector is a compressed version of the whole input, and it initializes the decoder.

The decoder starts from cc 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.

OlderNLP | Word Embedding →