An encoder processes input tokens into contextual vector representations. It is the understanding-focused half of a transformer, making it useful when the goal is to compare meaning, classify text, or engineer features rather than generate text. section 2.3.1 section 2.3.3
The encoder’s core workflow combines several ideas:
- Embeds tokens as vectors, then uses self-attention to improve their contextual and semantic representations. section 2.3.1
- Adds positional encoding so identical tokens can be distinguished by their positions while preserving useful similarity relationships. section 2.3.1
- Supports natural language understanding tasks such as classification and named entity recognition. section 2.3.1
- Passes its understanding to a decoder in a full transformer, where the combined architecture supports conditional generation such as translation or summarization. section 2.3.3
Why position matters
In “The cat in the hat rapidly leapt above the red fox and the brown unmotivated dog,” red and brown can begin with similar embeddings. Positional encoding separates them because they occur at different positions, while sine and cosine transformations restore much of their semantic similarity. BERT provides a production-oriented example of an encoder-only transformer. section 2.3.1
Figure 2.5 An encoder visualized. Encoders are the first half of the full transformer architecture and excel in natural language understanding tasks like classification or named entity recognition. Encoder models improve upon previous designs by not requiring priors or recurrence and using clever positional encoding and multihead attention to create a vector embedding of each token.
Use the encoder visualization to trace how positional encoding and multihead attention create token-level vector embeddings for language-understanding tasks. section 2.3.1




















