← All posts

What architecture is BERT? Encoder-only Transformer

Grow through practice

No need to finish reading — test now

Short answer: BERT is an encoder-only Transformer. It uses only the Transformer encoder stack, with no decoder and no causal mask.

The confusion is understandable: the original Transformer is encoder-decoder, but BERT takes only the encoder half.

Three architecture families

ArchitectureExamplesAttentionTypical tasks
Encoder-onlyBERT, RoBERTaBidirectional, no causal maskClassification, extraction, embeddings, token labeling
Decoder-onlyGPT, LLaMACausal mask, left context onlyAutoregressive generation, chat
Encoder-decoderT5, BARTBidirectional encoder + causal decoder + cross-attentionTranslation, summarization, seq2seq

The name says it:

Bidirectional Encoder Representations from Transformers

  • Bidirectional: every position can see the whole sequence.
  • Encoder: only the encoder stack.
  • Representations: learned representations, not direct generation.

What is inside BERT

BERT-base:

12 Transformer encoder layers
hidden size = 768
attention heads = 12
about 110M parameters

There is no decoder and no cross-attention. Each encoder layer is still:

self-attention → Add & Norm → FFN → Add & Norm

Encoder self-attention has no causal mask, so every token sees both left and right context. That is why BERT is good at understanding and not suited to left-to-right generation.

Pretraining: MLM + NSP

BERT is not pretrained with next-token prediction. It uses:

  1. MLM (Masked Language Model)

Mask 15% of tokens and predict them from context. Predicting masked tokens in the middle requires bidirectional understanding.

  1. NSP (Next Sentence Prediction)

Predict whether sentence B follows sentence A. Later work such as RoBERTa questioned NSP, but the original BERT used it.

Special tokens:

  • [CLS]: aggregate sequence representation, often used for classification;
  • [SEP]: separates sentences;
  • [MASK]: replaced token in MLM.

Why BERT cannot generate freely like GPT

BERT is trained with full context and has no “only look left” constraint.

Using it for autoregressive generation creates two problems:

  1. Training and inference attention conditions differ.
  1. Without a causal mask, the model can see future tokens.

So BERT is typically used for:

  • sentence classification / sentiment;
  • named entity recognition and token classification;
  • semantic embeddings / retrieval;
  • extractive question answering (predict start/end positions);
  • as an encoder with a task head.

It is not the “give it a prompt and let it continue writing” model; that is decoder-only work such as GPT/LLaMA.

Relationship to GPT and T5

Original Transformer
├─ Encoder (bidirectional)
│   └─ BERT uses this half
├─ Decoder (causal + cross-attention)
│   └─ GPT uses the autoregressive decoder
└─ Encoder + Decoder
    └─ T5 / BART use both

In one line:

BERT = Transformer encoder; GPT = Transformer decoder; T5 = encoder + decoder.

Common misconceptions

  1. “BERT is encoder-decoder.”

No. BERT has no decoder and no cross-attention.

  1. “BERT is bidirectional, so it can generate.”

Bidirectional understanding is not autoregressive generation. Generation needs a causal mask, which BERT lacks.

  1. “BERT can only classify.”

No. It also supports token labeling, extractive QA, sentence embeddings and retrieval; it just does not do free-form generation.

  1. “BERT is obsolete.”

Decoder-only models dominate generation, but BERT-style encoders remain common for classification, extraction, retrieval and low-latency embeddings.

Verification

  • ✅ Architecture: BERT uses Transformer encoders (Devlin et al., 2019).
  • ✅ Pretraining objectives: MLM + NSP, per the original paper.
  • ✅ Comparison: GPT is decoder-only; T5/BART are encoder-decoder.
  • ⚠️ Not measured: this article did not train or fine-tune BERT locally; the structural claims follow the paper and official implementation.
  • ❓ Not covered: detailed differences between RoBERTa/ALBERT/DeBERTa.

One sentence

BERT is a read-only Transformer encoder: strong bidirectional understanding, no autoregressive generation.


Series: Understanding Attention and Transformers from Scratch Prerequisites: Attention as soft lookup · Multi-head and positional encoding · Where the FFN sits Test yourself: Transformer ranked quiz

Practice

Found this useful?

I write about AI agents × embedded / edge AI. Subscribe, or email me a concrete problem.

Guestbook →Subscribe (RSS)Support

Comments 0

No comments yet — be the first!

No login required; name is optional.