liveBook · Manning

Encoder

As explained in Natural Language Processing in Action, Second Edition

An encoder is the transformer component that transforms an input sequence into contextual representations through stacked attention and feedforward layers.

An encoder transforms a token sequence into contextual representations by passing embeddings through a stack of reusable transformer layers. section 9.2.2

The input receives positional encoding before entering the encoder. Each encoder layer combines multi-head attention with a positionwise fully connected feedforward network, while linear projections and layer normalization help organize the outputs. section 9.2.2

Its place in transformer designs is easiest to see through these contrasts:

  • The original Transformer used six encoder layers alongside six decoder layers. section 9.2.2
  • Decoder layers share the encoder’s attention, normalization, and feedforward structure but add mechanisms for generated-token input. section 9.2.2
  • Later GPT architectures removed encoder layers and instead stacked many decoder layers. section 9.2.2

BERT as a bidirectional encoder

BERT stacks encoder transformers and trains them with masked language modeling: randomly masked tokens are predicted from both left and right context. Fine-tuning then adapts the resulting representations to sentence-level and token-level tasks. section 9.3

Figure 9.5 Transformer architecture
figure

Use the transformer architecture diagram to trace positional encoding, encoder layers, decoder layers, normalization, and output-token flow. section 9.2.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines stacked attention and feedforward layers with linear projections and normalization, but stops at architectural description.
Examples
Uses the original six-layer Transformer and BERT’s bidirectional masked-token prediction as concrete architectural examples.
What it assumes
Assumes familiarity with BPE, embeddings, positional encoding, and self-attention before assembling them into transformer blocks.
What you can do after
Evaluate encoder placement, layer composition, and its contrast with decoder-only GPT architectures.
How it connects
Links token embeddings, positional encoding, multi-head attention, feedforward networks, layer normalization, decoders, and BERT.

Also explained in

Compare how 21 books explain encoder →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.