Skip to content
IT glossary
Data and MLUpdated December 3, 2026

Transformer

Трансформер

A type of neural network that uses attention to let each position in its input draw on information from other positions. Transformers power many language models and are also used for images and audio.

Also known as: attention, механизм внимания

What a Transformer does

A Transformer builds representations of tokens in context. A token might be a word, part of a word, or even a patch of an image. In “the crane lifted a beam” and “the crane spread its wings,” the same token can start with the same embedding and end up with different representations. Attention is the operation that lets information move between positions. The Transformer is the architecture that uses it, not another name for the operation.

How attention works

Think of attention as a lookup that can combine several results instead of returning one match. Each position in a self-attention layer produces three vectors through learned projections: a query (Q), a key (K), and a value (V). For one position, the layer takes the dot product of its query with each allowed key, divides the scores by the square root of the key dimension, and applies softmax. This gives weights that sum to one. The layer uses those weights to mix the value vectors.

Multi-head attention runs several such lookups with separate learned projections and combines their outputs. A typical Transformer block also includes a feed-forward network applied separately at each position, residual connections, and normalization. Unmasked self-attention on its own does not encode sequence order, so Transformers supply position information separately, for example through positional embeddings or by rotating queries and keys with RoPE.

A worked example

Suppose three allowed positions receive weights of 0.1, 0.7, and 0.2 from one attention head. Its output is 0.1 × V₁ + 0.7 × V₂ + 0.2 × V₃. The middle position contributes with the largest coefficient in this lookup. That does not establish that it was the most important token for the final prediction: other heads and later layers also affect the result.

Where the architecture is used

Encoder-only models such as BERT can use context on both sides of a token and are commonly used for tasks such as text classification. Decoder-only models such as GPT use causal self-attention: a position can attend to itself and earlier positions, but not later ones. Encoder–decoder models such as T5 use cross-attention to let the decoder read the encoder's output, a useful setup for tasks like translation.

What interviewers look for

Be ready to explain why full, dense attention gets expensive as context grows. With the head dimension held fixed, its compute scales as O(n²) in the sequence length n, because the number of query–key pairs grows quadratically. Also distinguish training from generation: when the target sequence is known during training, a causal mask lets the model compute predictions at all positions in parallel without seeing future tokens. In autoregressive generation, each new token still depends on what has already been generated.