What's the difference between encoder and decoder in a transformer?
Asked 02 Dec 2026 · 1 views · About: Transformers: Principles, Applications & Limitations
Transformers consist of an encoder and a decoder, each performing distinct roles in processing sequences. This question aims to clarify their respective functions within the overall architecture.
3 Answers
The encoder processes the input sequence to create a contextualized representation. It then passes this representation to the decoder, which generates the output sequence based on it. The encoder focuses on understanding the input; the decoder focuses on generating the output.
Answered 02 Dec 2026 ·
The encoder maps the input sequence into a higher-dimensional space, capturing relationships between elements. The decoder then uses this encoded information, along with previously generated outputs, to predict the next element in the target sequence. They operate in opposite directions – encoding and decoding.
Answered 02 Dec 2026 ·
The encoder is typically composed of multiple stacked layers, each containing self-attention and feedforward networks. The decoder also has stacked layers but includes masked self-attention to prevent it from "seeing" future tokens during training. This ensures the decoder only uses past information for prediction.
Answered 02 Dec 2026 ·