TL;DR
An encoder-decoder network has two halves joined at a middle point. The encoder reads the input and compresses it into a compact internal representation, often called the code or context. The decoder starts from that representation and produces the output one piece at a time. The split means the input and output can have different shapes and lengths, for example a French sentence in and an English sentence out.
The bottleneck in the middle is both the point and the cost. Because everything the decoder knows must fit through it, the network learns to keep the parts of the input that matter for the output and drop the rest.
Where you'll see it: in machine translation systems, in summarisation models, in autoencoders that learn compressed codes of images, and in U-Net style networks that label every pixel of a medical scan.
Intuition
A reader and a writer with a note in between
Picture a translator who has to pass a message between two rooms without speaking. The first person reads the whole source paragraph, then writes a short note on a card. The card is the only thing that crosses the door. The second person, who has never seen the original, reads the card and writes out a paragraph in the other language.
That is an encoder-decoder. The first person is the encoder, the card is the latent representation, and the second person is the decoder. The card has a fixed size, so the encoder has to decide what to keep. If the paragraph is long and the card is small, details get lost. If the card is large, the decoder has more to work with but the system is harder to train and less useful as a summary.
Why split the job at all? Because the input and the output often live in different spaces. A French sentence is a list of French words of one length. An English sentence is a list of English words of a different length, and the two lists have no direct position-by-position match. A single network that maps one list straight to the other has to learn that alignment inside its wiring. Splitting the work into "understand the input" and "generate the output" lets each half specialise, and gives the middle a meaning of its own: a representation of the input's content that does not depend on its surface language.
The same picture works for images. An encoder that shrinks a photo into a short vector of numbers is doing the same job as the first person: keeping the content and discarding the pixel-level noise. A decoder that turns that short vector back into a picture is the second person. If the two halves are trained together to reproduce the original, the middle becomes a compact code for the photo.
flowchart LR X[Input sequence or image] --> E[Encoder] E --> Z[Bottleneck: code or context vector] Z --> D[Decoder] D --> Y[Output sequence or image]
The math
Setting up the two functions
Write the input as and the output as . The encoder is a function with learnable weights . It maps the input to a representation :
Here is a vector of fixed length, say real numbers. The decoder with its own weights maps that vector to a distribution over outputs:
The vertical bar is read "given". The whole model is the composition of the two, and training adjusts and together so that the output matches the target.
Sequence version
Most encoder-decoder models handle sequences. Let the input be and the output , where and are lengths that can differ. A recurrent encoder, such as a recurrent neural network or a LSTM, reads the input one step at a time and updates a hidden state (a vector that summarises everything read so far). The final state becomes the context:
The decoder is a second recurrent network that starts from and emits one output symbol per step. At each step it reads the symbol it produced last and its own state, and predicts the next symbol with a Softmax over the vocabulary:
The notation means all the outputs before position . The full probability of an output sequence is the product of the per-step probabilities, which becomes a sum after taking logs:
Training maximises this sum over a dataset of input-output pairs. Equivalently it minimises the negative sum, which is the cross-entropy loss of the predicted distributions against the true next symbols. Gradients flow back through the decoder, into the context vector, and through the encoder, so the whole model is trained by Backpropagation with a Gradient Descent optimiser.
Image version
For images the encoder is usually a stack of convolutional layers that shrink the spatial size while increasing the number of channels. The decoder mirrors it with layers that grow the spatial size again, typically transposed convolutions. The output has the same spatial size as the input, but each pixel now carries a label or a value, and the loss is computed per pixel. The reconstruction loss for an autoencoder is the squared error between the output and the input:
If the bottleneck has fewer numbers than the input, minimising this loss forces the code to capture the structure that predicts the input best.
A tiny bottleneck by hand
Take a 4-number input and a 2-number bottleneck, with linear maps so the arithmetic is easy to follow. The encoder adds neighbouring pairs:
The decoder copies each bottleneck number to the two positions it came from. Input goes through these steps:
- Encode: .
- Decode: .
- Error: .
The reconstruction is wrong, and the reason is the bottleneck. The 2-number code only remembers the pair sums, so and can no longer be told apart from and . A learned encoder would pick the 2 numbers that best separate the likely inputs, and the error would fall. The example shows the trade-off that every encoder-decoder faces: the smaller the code, the more has to be thrown away.
Worked examples
A trained model is asked to translate a source sentence into the French target "le chat". Assume the decoder assigns these probabilities, one per output step:
The probability of the whole output is the product:
The training loss for this example is the negative log of that probability, in nats:
Perplexity turns the per-symbol average back into an intuitive number. With three output steps (including the end marker), the per-step loss is , and . The model is, on average, choosing between about 1.5 equally likely options at each step. A perfect model would score 1.0.
A grayscale photo of size 28 by 28 has 784 pixel values. Suppose the encoder compresses it to a code of 32 numbers. The compression ratio is . The decoder must produce 784 values from only 32.
Now compare two codes. A 32-number code trained on handwritten digits usually reconstructs a clear digit, because 32 numbers are enough to describe which digit it is, its slant and its stroke width. A 2-number code can still separate digits roughly, but blurs the stroke detail. A 784-number code, the same size as the input, can copy the image exactly, which means it has learned nothing about structure. This is why the size of the bottleneck is the main design choice: too small and the output is blurred, too large and the network just memorises pixels.
The same idea applies to text. A translation model with a 512-number context has to fit a whole sentence into 512 numbers. Short sentences fit easily. Very long documents do not, which is one reason the attention idea was introduced: to let the decoder look back at the input directly rather than rely on one vector.
Tradeoffs and failure modes
Encoder-decoders are the right tool when input and output differ in shape, length or meaning, or when there is something to generate. They are the wrong tool when there is nothing to decode, such as one label per input, where an encoder with a classifier head is simpler. Even for pixel labelling, where input and output share a size, a plain bottleneck loses detail, as the table shows.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Long input, one fixed-size context vector | Early details are forgotten by the time the decoder runs | Add [[attention-mechanism |
| Output the same length as the input (e.g. labelling each pixel) | The bottleneck throws away spatial detail the output needs | Add skip connections from encoder to decoder, as in U-Net |
| Need a smooth, structured code (generation, interpolation) | A plain autoencoder code has gaps; random codes decode to noise | A Variational Autoencoder with a probabilistic code |
| Single tag per input (e.g. sentiment) | Nothing to decode; the extra decoder is wasted | Use the encoder alone with a classifier head |
| Training data is small | A large encoder and decoder overfit the examples | Smaller bottleneck, weight decay, or a pretrained encoder |
Two failure modes come up repeatedly in practice. The first is exposure bias: during training the decoder is fed the true previous symbol, but at test time it is fed its own guess, which may be wrong, and errors compound. The usual fix is scheduled sampling, which mixes true and predicted symbols during training. The second is posterior collapse in variational models, where the decoder becomes so strong that it ignores the code and the encoder learns nothing useful. Annealing the weight on the regularisation term helps.
History
Autoencoders, the simplest encoder-decoder, were studied for decades as a way to reduce dimension. Hinton and Salakhutdinov showed in 2006 that deep networks trained this way could compress data better than principal components analysis, which put the idea back on the research agenda.
The sequence version arrived in 2014. Cho and colleagues introduced an RNN encoder-decoder for machine translation and used it to score phrase pairs. In the same year Sutskever, Vinyals and Le trained a large LSTM encoder-decoder end to end on English-to-French translation and showed it was competitive with phrase-based systems. Bahdanau, Cho and Bengio then pointed out that forcing the whole sentence through one vector was the main weakness, and added attention so the decoder could look back at every encoder state. Their work led directly to the Transformer in 2017, which keeps the encoder-decoder shape but builds both halves from attention alone.
In vision, the shape found a second home. Ronneberger and colleagues introduced U-Net in 2015 for segmenting biomedical images. Its skip connections carry fine detail past the bottleneck, and it became a default for pixel-level prediction. Diffusion and generative image models later reused the same encoder-decoder pattern inside their denoising networks.
Check yourself
QuestionRecall: what are the two parts of an encoder-decoder, and what joins them?Show answer
The encoder reads the input and produces a compact representation, and the decoder reads that representation and produces the output. They are joined by the bottleneck, the fixed-size code or context vector that carries everything the decoder knows about the input.
QuestionComputation: an encoder-decoder assigns probabilities 0.8, 0.6 and 0.9 to the three output steps of a target. What is the total probability and the negative log-likelihood in nats?Show answer
The total probability is the product: . The negative log-likelihood is . Since , the loss is about nats.
QuestionWhy: why does the bottleneck force the network to keep only the important parts of the input?Show answer
The code has fewer numbers than the input, so it cannot store every detail. Training minimises the output error, so the encoder learns to keep whatever features best predict the target and to discard what does not. The size of the bottleneck sets how much can be kept.
QuestionApplied: you are building a model that labels every pixel of a 512 by 512 medical scan as tissue or background. Why would a plain encoder-decoder blur the boundaries, and what would you add?Show answer
Pixel labels need precise edges, but the bottleneck has compressed the image down to a coarse grid, so fine boundary detail is lost before the decoder runs. Adding skip connections from each encoder stage to the matching decoder stage, as in U-Net, passes that detail across the bottleneck and restores sharp boundaries.
Sources
- Sutskever, Vinyals, Le (2014), Sequence to Sequence Learning with Neural Networks: arxiv.org/abs/1409.3215
- Cho et al. (2014), Learning Phrase Representations using RNN Encoder-Decoder: arxiv.org/abs/1406.1078
- Bahdanau, Cho, Bengio (2014), Neural Machine Translation by Jointly Learning to Align and Translate: arxiv.org/abs/1409.0473
- Ronneberger, Fischer, Brox (2015), U-Net: arxiv.org/abs/1505.04597
- Autoencoder (Wikipedia): en.wikipedia.org/wiki/Autoencoder