Guide

What is a transformer?

Updated 10 July 2026 Part of Artificial Intelligence

When you ask a chatbot something, it doesn’t read your words one at a time — it looks at the whole sentence together to decide what matters. That ability comes from the transformer, a neural network architecture built around a mechanism called self-attention. Instead of processing text sequentially, a transformer weighs every word’s connection to every other word in one pass. The result is a model that trains far faster than older recurrent neural networks (RNNs) and stays sharp about context even when relevant words sit far apart. Almost every language tool you use today — translation, autocomplete, conversational AI — rests on this architecture.

How self-attention works

In a transformer, the self-attention layer scores every pair of words in a sentence: how much should “bank” pay attention to “river” versus “money”? These scores turn into weights that combine all the words, so the representation of any single word carries context from the entire sequence. Because that computation happens for all positions at the same time, there is no bottleneck. RNNs had to shuttle information step by step, which meant a signal from early in the text could fade out before reaching the end. Self-attention lets the model look directly at any earlier word — no matter how far back — and decide on the spot how much it matters.

Multi-head attention and positional encoding

Rather than running one attention calculation, a transformer runs several in parallel, each with its own set of learned parameters. This multi-head design lets the model capture different kinds of relationship at once: one head might follow subject–verb agreement, another might pick up on sentiment, a third might track where a name first appeared. It is the difference between listening to a conversation with one ear and listening with three tuned to different frequencies.

But self-attention, on its own, has no sense of word order; it sees a bag of words, not a sequence. Transformers fix that with positional encoding — a mathematical signal added to the input that tells the model where each word sits. Without it, “the cat chased the dog” and “the dog chased the cat” would look identical to the attention layers. With positional encoding, the order matters again.

Why transformers overtook RNNs

Recurrent neural networks read text the way a person reads a long paragraph aloud — one token after the other, updating a hidden state as they go. That sequential flow made training slow, because you could not parallelise across positions, and it created the vanishing-gradient problem: a hint from the first sentence could get washed out by the time the model reached the last. Transformers sidestep both weaknesses. Every word attends to every other word in a single operation, so the whole sentence can be processed at once, and long-distance links are captured directly. The trade‑off is appetite — the attention matrix grows quadratically with sequence length, so transformers need more memory and compute — but the jump in speed and quality has made them the default starting point for natural language work.

The same attention pattern has since spread into other domains: image generation, protein folding, reinforcement learning. Understanding the transformer means understanding the engine behind a great deal of the AI you meet every day.