Self-Attention: The Transformer's Core Idea

Self-attention lets a model weigh the importance of different words in a sequence to understand context. This core mechanism of the transformer architecture powers LLMs for translation and generation.
WHY IT EXISTS Before transformers, models like RNNs struggled to process long sequences of text. They had a 'short-term memory' problem, often forgetting the context from the beginning of a sentence by the time they reached the end. Self-attention was created to solve this by allowing every word to directly look at every other word, no matter how far apart they are.
THE MENTAL MODEL Think of self-attention as a database retrieval system for context. For each word in a sentence, you formulate a 'Query' about what context you need. You compare this Query to a 'Key' from every other word to see how relevant it is. You then pull a weighted average of the 'Values' from the most relevant words to enrich your original word's meaning.
HOW IT WORKS For every input token, the model generates three distinct vectors: a Query (Q), a Key (K), and a Value (V). The Query vector is the current word asking, 'Who has information relevant to me?' The Key vector of every other word acts as a label, saying, 'This is the kind of information I have.' The Value vector contains the actual information of that word. The model calculates attention scores by measuring the similarity (often via dot product) between one word's Q and every other word's K. These scores determine how much of each word's V should be blended into the final output for the current word.
WHEN TO USE IT This mechanism is the foundation of the transformer architecture and is essential for any task where understanding long-range dependencies in a sequence is critical. It's the engine behind modern large language models used for machine translation, text summarization, question answering, and code generation.
WHEN NOT TO USE IT The primary drawback is its computational cost, which scales quadratically with the length of the input sequence. This makes it prohibitively expensive for extremely long sequences (e.g., entire books). For such cases, more efficient attention variants or different architectures may be necessary.
ONE CANONICAL EXAMPLE The 2017 paper 'Attention Is All You Need' introduced the transformer, which used self-attention to set a new standard for machine translation. For instance, when translating 'The robot picked up the ball because it was heavy,' attention helps the model correctly associate 'it' with 'the ball' and not 'the robot', by scoring the relationship between those words highly.
Read the original → en.wikipedia.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.