Skip to content
mentoor

Topic 3 of 5 · 5 min read

Encoders, decoders and vision transformers

Goal: Match a task such as classification, generation, translation or image tagging to an encoder, decoder or encoder-decoder design.

Transformers come in three layouts, and the layout follows the task: encoders understand a whole input, decoders write one token at a time, and encoder-decoders turn one sequence into another.

Check yourself · 1 of 3

Which layout is built to predict the next token, one at a time?

Choose one answer

Why it matters

When someone says "use a transformer", the first question is which kind. Picking the wrong layout wastes compute or fails outright: a decoder is awkward for tagging every word, and an encoder cannot write free text.

The three layouts

  • Encoder-only (BERT-style). Every token attends to every other token, in both directions. The output is a rich vector per token. Use it for classification, entity tagging, search embeddings and reranking. It does not generate text.
  • Decoder-only (GPT-style). A causal mask lets each token see only earlier tokens, and the model predicts the next one. Repeating that produces text, code or chat replies. The large chat models, Claude, GPT and Gemini among them, are decoder-style models. They can also classify by being asked to, at higher cost per call.
  • Encoder-decoder (T5-style). The encoder reads the full input. The decoder writes the output while attending to the encoder's result. It fits translation, summarisation and speech-to-text, where input and output are different sequences.

Vision transformers

A vision transformer (ViT) cuts an image into fixed patches, such as 16x16 pixels, flattens each patch into a vector and treats the patches as tokens. A 224x224 image gives 14 x 14 = 196 patch tokens. An encoder then runs over them with position embeddings added. The ViT paper reports strong results when pretrained on very large image sets, and weaker ones than CNNs when data is small. Patches also mean small objects can be missed if patches are too coarse.

Before and after

Weak: "Our task is tagging each word in a contract as party, date or amount. We will fine-tune a decoder chat model and parse its replies."

Better: use an encoder-only model with a token classification head. It sees context on both sides of each word, outputs one label per token, is cheaper to run, and gives a score you can threshold. Use the decoder when the answer must be free-form text.

What goes wrong

  • Using a decoder for a plain classification job and paying for generation you then parse.
  • Expecting an encoder-only model to write an answer.
  • Feeding a ViT an image resized so small that the patches no longer contain the detail needed.

Examples

Task to layout

Flagging toxic comments: encoder-only classifier. Drafting a reply to a customer: decoder-only model. Translating a manual from German to English: encoder-decoder or a decoder with a translation prompt. Tagging product photos: a ViT or CNN with a classification head.

Patch arithmetic

A 384x384 image with 16x16 patches gives 24 x 24 = 576 tokens, nearly three times the tokens of a 224x224 image. Attention cost grows with the square of that count, so higher resolution is expensive.

Key points

  • Encoder-only models read in both directions and output a vector per token, which suits classification, tagging and embeddings.
  • Decoder-only models predict the next token with a causal mask, which is why they generate text and code.
  • Encoder-decoder models map one sequence to a different one, as in translation or summarisation.
  • A vision transformer turns an image into patch tokens, so a 224x224 image with 16x16 patches becomes 196 tokens.
  • Choose the layout from the task's input and output shape before choosing a model name.

Common mistakes

  • Using a generative chat model for every task, including simple labelling that a small encoder does cheaper and more consistently.
  • Asking an encoder-only model to write free text.
  • Ignoring that raising image resolution multiplies the patch count and the attention cost.
  • Assuming ViT always beats a CNN, when it needs large pretraining to do so.

Check yourself · 2 of 3

You must label each word in 10,000 contracts as party, date or amount. Which design fits best?

Choose one answer

Check yourself · 3 of 3

A vision transformer uses 16x16 patches on a 224x224 image. How many patch tokens does the encoder see?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic