跳转至

Transformer Explained Decoder

Absolutely. This is the perfect way to complete our understanding of the architecture. The output half, or the Decoder, has a fascinating and slightly more complex job than the Encoder.

Let's use the same method.

The Grand Analogy: The Bilingual Translator

The first half of the Transformer, the Encoder, was like a librarian reading a source text (e.g., a sentence in German) and creating a set of highly detailed, context-rich summary notes.

The second half, the Decoder, is like a skilled Translator whose job is to write a new sentence in a different language (e.g., English), one word at a time. The translator has two crucial resources: 1. The Encoder's rich summary notes of the German sentence. 2. The English sentence they have written so far.

The Decoder's entire process is designed to answer one question, over and over: "Based on the German source text and the English words I've already written, what is the single most likely next word?"

Let's follow the data flow for generating one word.


Step 1: The Decoder's Input ("What I've Written So Far")

  • Theatrical Explanation: The translator begins with a blank page, except for a special <start> symbol. To generate the first English word, their only input is this <start> symbol. To generate the second word, their input is <start> The. To generate the third word, their input is <start> The cat. This is called an autoregressive process—it feeds its own output back into its input for the next step. The diagram's "Output (shifted right)" label refers to this process: to predict word i, you are only allowed to see words 1 to i-1.

  • Mathematical Explanation: The sequence of tokens generated so far (e.g., [<start>, "The", "cat"]) is fed into an Output Embedding matrix and combined with Positional Encoding, just like in the Encoder. This converts the words into context-free, position-aware vectors.


Step 2: Masked Multi-Head Attention ("Reviewing My Own Work, With Blinders")

  • Theatrical Explanation: This is the translator's first step: they re-read the English sentence they have written so far to gather context. However, they must follow a strict rule: No cheating by looking into the future. When they are processing the word "The," they must pretend "cat" hasn't been written yet. This is a self-attention mechanism, but with blinders on. The "mask" is the blinder. It ensures that the model's prediction for a position can only depend on the known outputs at previous positions.

  • Mathematical Explanation: This is a standard Multi-Head Self-Attention mechanism, identical to the one in the Encoder, with one critical difference: the Look-Ahead Mask.

    • Before the softmax step, a mask is applied to the score matrix (Q * K^T).
    • This mask is a matrix that has -infinity in all the positions that correspond to future words. For the row corresponding to "The," the columns for "cat" and all subsequent words would be set to -infinity.
    • When you apply softmax, e^(-infinity) becomes 0.
    • This means the attention weights for all future words are forced to be zero. A word is physically prevented from gathering information from any word that comes after it.

Checkpoint A: Add & Norm (Stabilize the Self-Review)

  • This is identical to the Encoder. The output from the masked attention is added to its input (residual connection), and the result is normalized. This preserves the information and stabilizes the network.

Step 3: Cross-Attention ("Consulting the Source Text")

  • This is the most important step of the Decoder and the bridge between the two halves of the Transformer.
  • Theatrical Explanation: The translator has just finished reviewing their own work (" The cat"). Now, they take this refined understanding and consult the Encoder's summary notes of the original German sentence. The translator's vector for "cat" essentially asks the German notes, "Okay, I've just written 'The cat.' Which part of the German sentence is most relevant for figuring out what comes next?" The German notes might highlight the word "jagte" (chased), providing the crucial context.

  • Mathematical Explanation: This is a Multi-Head Attention layer, but it's different from self-attention. It's called Cross-Attention.

    • The Query (Q) vectors come from the Decoder's previous layer (the output of the first Add & Norm). This is the translator's current state.
    • The Key (K) and Value (V) vectors come from the final output of the Encoder. This is the complete set of summary notes for the German sentence.
    • The process is the same: Attention(Q, K, V). The Decoder's query is compared against the Encoder's keys. The resulting attention weights are then used to create a weighted sum of the Encoder's values.
    • This step is how the Decoder injects the meaning of the source sentence into its generation process.

Checkpoint B & Step 4 & Checkpoint C (Deep Thinking)

  • These three steps are identical in structure and purpose to their counterparts in the Encoder.
    • Add & Norm: Stabilize the output of the cross-attention.
    • Feed-Forward Network: Perform deep, individual processing on this newly combined vector (which now contains information from both the English it has written and the German source text).
    • Add & Norm: A final stabilization.

Step 5 & 6: The Final Prediction (Linear & Softmax)

  • Theatrical Explanation: The translator has done all their work. They've reviewed their own writing, consulted the source text, and synthesized everything. Now, they walk over to a giant dictionary containing every possible word in the English language and point to the one they believe is the most likely to come next.

  • Mathematical Explanation:

    1. Linear Layer (The "Logits" Projector):

      • The final, polished 512-dimension vector from the last Add & Norm block is fed into one last, fully-connected linear layer.
      • The job of this layer is to project the vector from the d_model dimension (512) up to the vocabulary size dimension (e.g., 50,000).
      • The output is a huge vector called the logits vector. Each element in this vector corresponds to a word in the vocabulary and holds a raw, un-normalized score for that word.
    2. Softmax (The "Probability" Converter):

      • The softmax function is applied to the entire logits vector.
      • It converts the raw scores into a proper probability distribution, where all values are between 0 and 1 and they all sum to 1.
      • The word with the highest probability is the model's final prediction for the next word in the sentence. This word is then appended to the input for the next generation step, and the entire process repeats.