Encoder-Decoder Architectures
Encoder-Decoder architectures are neural network designs that compress an input into a compact intermediate representation (the bottleneck or context vector) via an encoder, then reconstruct or generate a target output from that representation via a decoder. They matter because they underpin nearly every modern sequence-to-sequence system — machine translation, image segmentation, speech synthesis, and diffusion models all rely on this structural principle. The counterintuitive fact: the bottleneck, which appears to discard information, is precisely what forces the model to learn the most useful abstract representation, not the most faithful reconstruction.
The encoder-decoder split enforces an information bottleneck: the encoder must distill an arbitrarily complex input into a fixed or structured latent space, and the decoder must learn to generate coherent, high-dimensional outputs from that compressed code alone. This separation of understanding from generation is what makes these architectures so composable — you can swap encoders (e.g., replace an RNN with a Transformer) without redesigning the decoder, or pre-train each half independently. The "aha moment" is recognising that the two halves solve fundamentally different problems: the encoder is a lossy compressor, and the decoder is a conditional generative model.
How It Works
Notation and the Basic Formulation
Let the input sequence be with , and the target sequence be with .
Encoder. The encoder maps the full input to a latent representation:
Explanation: The encoder collapses the entire variable-length input into a single fixed-size vector , which must carry all information the decoder will need. Here denotes encoder parameters, is the bottleneck dimension, and can be any differentiable function (RNN, CNN, Transformer, etc.).
Decoder. The decoder produces each output token conditioned on and all previously generated tokens:
Explanation: The joint probability over the output sequence factorises autoregressively — each token depends on the latent code and the preceding tokens, making the decoder a conditional language model. The parameters are the decoder weights, is the decoding history, and the product reflects the chain rule of probability.
Training Objective
The standard training objective is the sequence cross-entropy loss (negative log-likelihood):
Explanation: We average the negative log-probability of each ground-truth token given the encoded input and all previous ground-truth tokens (teacher forcing), so both encoder and decoder are updated jointly through backpropagation. Here is the ground-truth target at position , and teacher forcing means we feed the gold reference rather than the model's own predictions during training.
RNN-based Context Vector (Seq2Seq)
In the original Sequence-to-Sequence Learning (Sutskever et al., 2014), the encoder is an LSTM whose final hidden state is the context vector:
Explanation: At each timestep , the LSTM updates its hidden state by combining the new input with the previous state ; the final state is handed to the decoder as the complete summary of the input. The hidden dimension is typically 256–1024; collapses all temporal context into a single vector, which is the classical bottleneck.
Attention-Augmented Decoding
To relieve the single-vector bottleneck, Attention Mechanism (Bahdanau et al., 2015) replaces the static with a dynamic context :
Explanation: At decoding step , the model computes a soft alignment score between encoder state and decoder state , normalises them into attention weights , and takes a weighted sum of all encoder states as the context — letting the decoder "look back" at any input position. Here , , are learned projection matrices, and is the decoder's hidden state at the previous step.
PyTorch Sketch
```python import torch import torch.nn as nn class EncoderRNN(nn.Module): def __init__(self, vocab_size, embed_dim, hidden_dim): super().__init__() self.embedding = nn.Embedding(vocab_size, embed_dim) self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True) def forward(self, x):
# x: (batch, src_len) emb = self.embedding(x) # (batch, src_len, embed_dim) outputs, (h_n, c_n) = self.lstm(emb) # outputs: (batch, src_len, hidden) return outputs, h_n, c_n # return all states for attention class DecoderRNN(nn.Module): def __init__(self, vocab_size, embed_dim, hidden_dim): super().__init__() self.embedding = nn.Embedding(vocab_size, embed_dim) self.lstm = nn.LSTM(embed_dim + hidden_dim, hidden_dim, batch_first=True) self.fc = nn.Linear(hidden_dim, vocab_size) def forward(self, y_t, h, c, context):
# y_t: (batch, 1) — one token at a time emb = self.embedding(y_t) # (batch, 1, embed_dim) lstm_in = torch.cat([emb, context.unsqueeze(1)], dim=-1) out, (h, c) = self.lstm(lstm_in, (h, c)) logits = self.fc(out.squeeze(1)) # (batch, vocab_size) return logits, h, c ```
Sutskever completed his PhD under Geoffrey Hinton at the University of Toronto and co-founded OpenAI. His 2014 paper "Sequence to Sequence Learning with Neural Networks" (with Vinyals and Le) established the foundational encoder-decoder paradigm for NLP, showing that reversing the source sentence before encoding significantly improved translation quality. Remarkably, the reversing trick — a simple preprocessing heuristic with no theoretical guarantee — was one of the key factors that made the original seq2seq model competitive with phrase-based SMT systems of the era.
Intuition
Think of an encoder-decoder like a professional interpreter at a United Nations summit. When a French diplomat begins speaking, the interpreter does not start translating word-by-word immediately; instead, they listen to the full sentence or clause, building a mental model of the meaning — the encoded representation. Only once they have internalised that meaning do they begin producing fluent speech in English — the decoding phase. The bottleneck is the interpreter's working memory: finite, precious, and forced to retain only meaning, not verbatim words. The attention mechanism extends this analogy: instead of relying solely on a single mental snapshot, the interpreter can glance back at their handwritten notes (the encoder states ) at any moment to recall specific details — a name, a number, a technical term. For image applications, imagine a radiologist (encoder) who studies an X-ray and distils their observations into a structured report template (latent code), which a medical writer (decoder) then expands into a full clinical report — neither needs to see the original X-ray again once the template is filled.