Encoder-Decoder Framework › Worked Example: A Sequence Transformation

Michael BrenndoerferMay 15, 20252 min read
Language AI HandbookAll chapters
Part 12 of 67Sequence-to-Sequence
Chapter 83Encoder-Decoder Framework › Worked Example: A Sequence Transformation
Reading section 9 of 16 · Worked Example: A Sequence Transformation · 2 minSection 9 of 16 · 2 min

Book contents

Section 9 of 16 · 2 min read

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 h1h_1 contains information about the start of a sequence, which is fairly generic. After reading "the", h2h_2 begins to reflect that the sequence starts with a common function word. After "cat", h3h_3 knows about "the cat," a likely subject phrase. After "sat", h4h_4 reflects a complete verb phrase "the cat sat." After <EOS>, h5h_5 contains the full context of a short sentence describing a sitting cat.

The encoder produces hidden states h1,h2,h3,h4,h5h_1, h_2, h_3, h_4, h_5. After processing the final <EOS> token, we take c=(h5,s5)c = (h_5, s_5) as the context vector pair.

Decoding phase:

The decoder starts with hidden state s0=cs_0 = c and first input y0=embedding(<SOS>)y_0 = \text{embedding}(\text{<SOS>}). At each step:

  • t=1t = 1: The decoder computes s1=LSTM(y0,s0)s_1 = \text{LSTM}(y_0, s_0), projects to logits, and predicts "sat" as the highest-probability token.
  • t=2t = 2: Feed the embedding of "sat", compute s2s_2, and predict "cat".
  • t=3t = 3: Feed the embedding of "cat", predict "the".
  • t=4t = 4: 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 t=2t = 2 would be the correct token "cat" regardless of what the decoder predicted at t=1t = 1.

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.