Skip to content
tezvyn:
Menu

Transformer Architecture

HardHow cards are made

The Transformer replaces recurrence with self-attention, letting every token directly attend to every other token in parallel. This enables long-range context and fast training on GPUs, making it the backbone of modern large language models and much of…

Why it exists

Recurrent networks process sequences one step at a time, which is slow to train and struggles to carry information across long distances because signals must pass through many steps. The Transformer was introduced to remove recurrence entirely, enabling parallel computation over a sequence and direct connections between distant tokens, which solved both the speed and long-range dependency problems.

The mental model

Imagine each word in a sentence asking every other word how relevant it is, then building its own meaning as a weighted blend of the most relevant words. That weighted lookup is self-attention. Because every word can consult every other word in one operation, context is gathered globally and simultaneously rather than passed down a chain.

How it works

Each token is projected into query, key, and value vectors. Attention scores come from comparing a token's query with every token's key; those scores are normalized and used to take a weighted sum of value vectors, producing a context-aware representation. Multi-head attention runs several such attention computations in parallel to capture different relationships. Since attention is order-agnostic, positional encodings are added so the model knows token order. Each layer combines attention with a position-wise feed-forward network, residual connections, and normalization, and many layers are stacked. Variants are encoder-only like BERT, decoder-only like GPT, or encoder-decoder for translation.

When it matters

Transformers dominate natural language processing and increasingly vision, audio, and multimodal tasks. They matter most when you have large data and compute and need to model long-range structure. The main cost to know is that standard self-attention scales quadratically with sequence length, which motivates efficient-attention variants for very long inputs.

A concrete example

Consider the sentence the animal did not cross the street because it was too tired. To resolve what it refers to, a Transformer's attention lets the token it place high weight on animal rather than street, directly linking the two words despite the distance between them. This single global attention step captures the dependency that a recurrent model would have to carry across many sequential steps, illustrating why Transformers handle long-range context so effectively.

Interview question

What core mechanism lets Transformers capture long-range dependencies more effectively than recurrent networks?

  • a.Pooling layers that downsample the sequence length
  • b.Sequential recurrence that carries a hidden state across time steps
  • c.Self-attention, where every token directly attends to every other tokenCorrect
  • d.Convolutions that slide local filters across the sequence
Why?

Self-attention connects any two tokens directly in a single step, regardless of distance, enabling parallel global context. Recurrence (option B) is exactly what the Transformer replaced because long-range signals degrade across many steps.

Just read this? Test yourself on what you have been reading.

Read the original → en.wikipedia.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles