Dictionary of Applied Machine Learning
Typeset PDF version — the authoritative form of this entry
Python demo — a script that recomputes what this entry states and prints one line per check
A transformer is an artificial neural network (ANN) that is built by composing layers, each of which transforms the features of a data point that consists of tokens, such as the words of a sentence. The feature vectors of the tokens are stacked into a matrix, and each layer maps this matrix to a transformed matrix of the same shape, so that layers can be composed freely into a deep ANN. The layers alternate between token mixing, which combines information across tokens, and a position-wise multilayer perceptron (MLP), which transforms each token separately. In the original transformer, token mixing is implemented by multi-head attention. Alternative mixing transformations, such as a fixed Fourier transform or a state-space layer, can take the place of attention. Transformers underlie large language models (LLMs).
P-shapeIn the context of machine learning (ML), the term transformer refers to an artificial neural network (ANN) that is built by composing layers, each of which implements a transformation of the features of a data point that consists of tokens (Vaswani et al., 2017). The feature vectors (or embeddings) $\featurevec^{(1)}, \ldots, \featurevec^{(\contextlen)} \in \reals^{\featuredim}$ of the $\contextlen$ tokens are stacked into a matrix $\mX = \big(\featurevec^{(1)}, \ldots, \featurevec^{(\contextlen)}\big)^{\top} \in \reals^{\contextlen \times \featuredim}$. Each layer of a transformer maps such a matrix to a transformed matrix of the same shape. Since input and output have the same shape, the layers can be composed freely into a deep ANN. For example, a transformer that translates a sentence transforms the stacked word embeddings layer by layer into feature vectors from which the translated words are predicted (Vaswani et al., 2017); the same principle underlies large language models (LLMs).
P-blockThe layers of a transformer alternate between two types of
transformations of $\mX$ (see Fig. 1).
A token-mixing layer combines information across tokens,
i.e., across the rows of $\mX$. In the original transformer, token
mixing is implemented by multi-head attention; the
attention mechanism is what sets transformers apart from
previous models for sequential data such as
recurrent neural networks (RNNs). A position-wise multilayer perceptron (MLP) then
transforms each row of $\mX$ separately, so that tokens
interact only through the token-mixing layers. Residual
connections and layer normalization are interleaved with these
transformations.
See also: attention, artificial neural network, layer, token, embedding, recurrent neural network, natural language processing, large language model, multilayer perceptron.
@misc{dictml_transformer,
author = {Jung, Alexander},
title = {transformer},
howpublished = {Dictionary of Applied Machine Learning (course edition)},
year = {2026},
doi = {10.5281/zenodo.21569296},
note = {ISBN 978-952-64-3013-3, CC BY 4.0, retrieved 2026-08-21},
url = {https://dictionaryofml.org/terms/transformer.html}
}