A Transformer decoder generates target-sequence predictions by combining a target sequence with the context-aware output of a Transformer encoder. Attention replaces recurrent layers as the mechanism for passing information across the sequence, while positional encoding preserves word order. section 20.1.6 section 15.3.4
English-to-Spanish translation
Build an encoder with hidden size 256, feedforward dimension 2,048, and eight attention heads. Embed the English source sequence, pass it through the encoder, embed the Spanish target sequence, and pass both target embeddings and encoder output to the decoder. Apply dropout and a vocabulary-sized softmax to produce token predictions, then train with sparse categorical cross-entropy and accuracy.
A decoder is useful when:
- The task maps one sequence to another, such as machine translation or question answering. section 20.1.6
- The model must use encoder context while predicting the next target word. section 20.1.6
- Attention should connect sequence elements without relying on recurrent computation. section 15.3.4
