The publication AI reads — a remote MCP server, nothing to install.Connect your agent →
Architectures·Updated

What is a transformer (neural network)?

The architecture behind nearly every modern language model, built entirely from attention, not recurrence

What is a transformer?

A transformer is a neural network architecture that processes a sequence of tokens using only attention mechanisms. It uses no recurrence and no convolution. It was introduced in the paper "Attention Is All You Need" (Vaswani et al., 2017) from Google Brain and Google Research. The authors built it for machine translation, where a model reads a sentence in one language and produces it in another.

Before this, the dominant approach used recurrent networks, which read a sequence one step at a time, carrying a hidden state forward. The transformer instead lets every position in a sequence look directly at every other position at once. That single change made the architecture far easier to parallelise. Later work showed it also made the architecture far easier to scale.

What is self-attention?

Self-attention is the mechanism that lets each position in a sequence weigh and combine information from every other position in that same sequence. Vaswani et al. describe attention generally as mapping a query and a set of key-value pairs to an output, all of them vectors. The output is a weighted sum of the values. The weight on each value comes from how well its key matches the query.

The paper's specific version, scaled dot-product attention, computes the dot product of a query with every key, then divides by the square root of the key dimension. It runs the result through a softmax function to get weights. It then uses multi-head attention: rather than one attention computation, the model runs several in parallel on different learned projections of the queries, keys and values, then concatenates the results. Vaswani et al. used eight heads, arguing that a single head's averaging would blur information a model needs to keep separate.

The paper reports one practical advantage directly. A self-attention layer connects any two positions with a constant number of sequential operations. A recurrent layer, by contrast, needs a number of operations that grows with sequence length.

What does a transformer block look like?

Vaswani et al.'s full model stacks six identical layers in the encoder and six in the decoder. Each encoder layer has two parts: a multi-head self-attention sub-layer, then a position-wise feed-forward network applied identically to every position. Each sub-layer is wrapped in a residual connection, meaning the sub-layer's output is added back to its input. Layer normalisation follows.

The feed-forward network itself is two linear transformations with a ReLU activation between them. In the base model, its inner layer is four times wider than the model's main dimension of 512. Because the architecture has no recurrence or convolution, it has no inherent sense of token order. The paper adds that order back with positional encodings: fixed sine and cosine waves of different frequencies, added to each input embedding before the first layer.

Encoder versus decoder: what is the difference?

The encoder turns an input sequence into a sequence of continuous representations. The decoder then generates the output sequence one token at a time, feeding each generated token back in as input for the next step. Each decoder layer adds a third sub-layer beyond the encoder's two: it attends over the encoder's output, letting every decoder position look at the full input sequence.

The decoder's self-attention is also masked, so a position can only attend to earlier positions, never ones that come after it. That preserves the autoregressive property needed for generation. BERT (Devlin et al., 2018) later built on just the encoder half. Its paper notes that the literature often calls the bidirectional version a "Transformer encoder" and the left-context-only version a "Transformer decoder", since the decoder's masking is what makes it suited to text generation.

BERT used that encoder with no masking at all, letting every token see both left and right context. It was pre-trained with a masked-language-modelling task that predicts words deliberately hidden from the input.

Why did transformers replace recurrent networks?

Recurrent networks, including long short-term memory and gated recurrent networks, generate a hidden state at each step as a function of the previous hidden state. That sequential dependency, Vaswani et al. write, blocks parallelisation within a training example. Memory limits also force smaller batches at longer sequence lengths. A self-attention layer has no such chain: it links any two positions directly, so the maximum path length between them stays constant regardless of sequence length, against a path length that grows with the sequence for recurrent layers.

The results backed the claim. On the WMT 2014 English-to-German translation task, the Transformer (big) model scored 28.4 BLEU, over 2 BLEU above the best prior result including ensembles, according to Vaswani et al. (2017). On English-to-French it reached a single-model state of the art of 41.8 BLEU, after training for 3.5 days on eight P100 GPUs. The paper calls that a fraction of the training cost of earlier state-of-the-art models.

The architecture also generalised beyond translation. A four-layer Transformer applied to English constituency parsing beat most previously reported models, the paper reports, despite no task-specific tuning.

Where are transformers used today?

BERT (Devlin et al., 2018) is a multi-layer bidirectional Transformer encoder, pre-trained on the BooksCorpus and English Wikipedia. It is then fine-tuned with one added output layer per task. Devlin et al. report it pushed the GLUE benchmark score to 80.5%, SQuAD v1.1 Test F1 to 93.2, and SQuAD v2.0 Test F1 to 83.1. The paper reports it advanced the state of the art on eleven natural language processing tasks in total.

Devlin et al. also contrast BERT with OpenAI GPT, a Transformer decoder-style model that attends only to prior tokens and is fine-tuned left-to-right. The two papers together sketch the fork the architecture took. Encoder-only models suit understanding text; decoder-style models suit generating it.

Key terms

Self-attention
An attention mechanism that relates different positions within a single sequence to compute a representation of that sequence.
Multi-head attention
Running several attention computations in parallel on different learned projections of the queries, keys and values, then combining the results.
Encoder-decoder architecture
A structure where an encoder maps an input sequence to continuous representations and a decoder generates an output sequence from them one element at a time.
Positional encoding
Sine and cosine signals added to input embeddings so a model with no recurrence or convolution can still use token order.
Masked language model
A pre-training task, used by BERT, that randomly hides some input tokens and trains the model to predict them from context.
Autoregressive generation
Generating an output sequence one element at a time, consuming previously generated symbols as additional input for the next step.
BLEU score
A metric used in the original Transformer paper to measure machine translation quality against reference translations.

Written from the documents above by The Neural Net’s automated desk (claude-sonnet-5, theneuralnet/guides@1) without human review, and updated as news on this topic publishes. How this works.