Encoder-Decoder Framework › Worked Example: A Sequence Transformation
Language AI HandbookAll chapters Encoder-Decoder Framework › Worked Example: A Sequence TransformationOne section · Sections in this chapter9 / 16 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let us trace through a small example to make the mechanics concrete. Suppose we want to learn a mapping from phrases to their word-reversed versions: "the cat sat" maps to "sat cat the".
Encoding phase:
The source tokens are ["<SOS>", "the", "cat", "sat", "<EOS>"]. The encoder embedding layer maps each token index to a 64-dimensional vector. The LSTM encoder processes these embeddings left to right, updating its hidden state and cell state at each step.
After reading <SOS>, the hidden state contains information about the start of a sequence, which is fairly generic. After reading "the", begins to reflect that the sequence starts with a common function word. After "cat", knows about "the cat," a likely subject phrase. After "sat", reflects a complete verb phrase "the cat sat." After <EOS>, contains the full context of a short sentence describing a sitting cat.
The encoder produces hidden states . After processing the final <EOS> token, we take as the context vector pair.
Decoding phase:
The decoder starts with hidden state and first input . At each step:
- : The decoder computes , projects to logits, and predicts "sat" as the highest-probability token.
- : Feed the embedding of "sat", compute , and predict "cat".
- : Feed the embedding of "cat", predict "the".
- : Feed the embedding of "the", predict
<EOS>.
The final output is "sat cat the", which is the correct word-reversed version.
During training with teacher forcing, the decoder input at would be the correct token "cat" regardless of what the decoder predicted at .
What the context vector must encode:
For this word-reversal task, the context vector must capture the complete ordered sequence of content words in the source, so the decoder can emit them in reverse order. This is a particularly demanding test of the context vector because it requires retaining precise positional information about every word. In real translation, the context vector does not need to store verbatim source content; it needs to store the meaning, which is a more compressed representation. Even so, for real translation of moderately long sentences, the context vector quickly becomes the limiting factor.