Home / Insights / AI Foundations
How Transformers Work: Attention Without the Mathematics Overload
The architecture behind modern language models, explained through the idea of attention.
The transformer was introduced in a 2017 paper titled "Attention Is All You Need". It was designed for translation and became the basis for most large language models.
The problem it solved
Earlier recurrent networks read text one step at a time, carrying a summary forward. Information from the beginning of a long passage could fade, and the step-by-step order prevented efficient parallel computation. Attention mechanisms, introduced for translation by Bahdanau and colleagues, let a model look back at relevant words. The transformer removed recurrence and kept attention.
Diagram by O&O Consulting.
Self-attention in plain terms
Text is split into tokens, pieces of words, and each becomes a vector. For every token the model forms three vectors: a query (what am I looking for?), a key (what do I offer?) and a value (the information to pass on). Each token's query is compared with all the keys, producing weights. The token's new representation is a weighted mix of the values.
In the sentence "the trophy did not fit in the case because it was too large", attention gives the model a means of linking "it" to the relevant noun. Several attention "heads" run side by side, each free to track a different relationship.
The rest of the block
Attention is followed by a small feed-forward network applied to each position, with shortcut connections and normalisation. Dozens of such blocks are stacked. Because attention itself ignores order, information about position is added to the tokens.
A model that generates text is only allowed to attend to earlier tokens. It outputs a probability for each possible next token, one is chosen, and the process repeats.
Limitations and open questions
Standard attention compares every token with every other, so doubling the input length roughly quadruples that part of the work. Faster kernels and alternative architectures, including state space models, aim to reduce the cost. Attention weights are also not a dependable explanation of why a model produced an answer.
References
- Vaswani, A. et al. (2017). "Attention Is All You Need." arxiv.org/abs/1706.03762
- Bahdanau, D., Cho, K. and Bengio, Y. (2014). "Neural Machine Translation by Jointly Learning to Align and Translate." arxiv.org/abs/1409.0473
- Gu, A. and Dao, T. (2023). "Mamba: Linear-Time Sequence Modeling with Selective State Spaces." arxiv.org/abs/2312.00752
This article is general information, not financial, legal or investment advice. AI research and rules change quickly; check the cited sources for the current position.