Self-Attention
Self-attention is a mechanism that allows a neural network to weigh the importance of every element in a sequence relative to every other element — all at once, in a single pass. It is the foundational operation inside Transformer models, powering nearly every state-of-the-art system in language, vision, and beyond. The counterintuitive fact: self-attention has no built-in sense of order — it treats a sentence as an unordered set of tokens unless position information is explicitly added.
When a model reads the word "bank" in a sentence, it needs context to know whether it means a riverbank or a financial institution. Self-attention solves this by letting every word "look at" every other word and decide which ones are most relevant to its own meaning. The result is a rich, context-aware representation of each token — built not from a fixed window of neighbors, but from the entire sequence simultaneously. This is the leap that made modern large language models possible.
How It Works
Self-attention takes a sequence of token vectors as input and produces a new sequence of the same length, where each output vector is a weighted blend of all the input vectors. The weights are determined by how "relevant" each token is to each other token. The mechanism uses three learned linear projections called Query (Q), Key (K), and Value (V). Think of it like a soft search engine: the Query is what you're looking for, the Key is what each token advertises about itself, and the Value is what each token actually contributes if selected.
Explanation: The attention score between token i and token j is their dot product divided by the square root of the key dimension, preventing the scores from getting too large. Here, is the query vector for token , is the key vector for token , and is the dimensionality of the key vectors (a scaling factor for numerical stability).
Explanation: Stack all tokens' scores into a matrix, convert them to probabilities with softmax, then use those probabilities to take a weighted average of the Value vectors. The output for each token is a weighted sum of all Value vectors, where the weights come from the softmax. Tokens with high scores contribute more to the output. Here is a minimal Python implementation to make this concrete: ```python import numpy as np def softmax(x): e_x = np.exp(x - x.max(axis=-1, keepdims=True)) return e_x / e_x.sum(axis=-1, keepdims=True) def self_attention(X, W_Q, W_K, W_V): """ X: (seq_len, d_model) — input token embeddings W_Q, W_K, W_V: (d_model, d_k) — learned projection matrices """ Q = X @ W_Q # (seq_len, d_k) K = X @ W_K # (seq_len, d_k) V = X @ W_V # (seq_len, d_k) d_k = Q.shape[-1] scores = Q @ K.T / np.sqrt(d_k) # (seq_len, seq_len) weights = softmax(scores) # attention weights output = weights @ V # (seq_len, d_k) return output, weights
# Tiny example: 3 tokens, 4-dimensional embeddings, 4-dimensional keys np.random.seed(42) seq_len, d_model, d_k = 3, 4, 4 X = np.random.randn(seq_len, d_model) W_Q = np.random.randn(d_model, d_k) W_K = np.random.randn(d_model, d_k) W_V = np.random.randn(d_model, d_k) output, weights = self_attention(X, W_Q, W_K, W_V) print("Attention weights:\n", weights.round(3)) print("Output shape:", output.shape) ``` Running this prints a 3×3 matrix of attention weights — row shows how much token attends to each of the three tokens. Each row sums to 1.0.
Vaswani was a senior researcher at Google Brain when he led the team that wrote "Attention Is All You Need" (2017), the paper that introduced the Transformer and made self-attention the central operation in NLP. His key insight was that the recurrent connections in LSTMs could be dropped entirely if self-attention was allowed to operate across the full sequence in parallel. Remarkably, the paper was written by eight co-authors who nearly all left Google shortly after publication — scattering to found or join OpenAI, Cohere, Adept, and other major AI labs.
Intuition
Imagine you're at a crowded party trying to understand what someone said. You don't give equal attention to every person in the room — you focus most on the people whose words seem most relevant to the conversation thread you're following. Self-attention works the same way: for each word in a sentence, it "asks a question" (the Query), scans every other word's "name tag" (the Key), and decides how relevant each word is. The most relevant words get the highest weights, and the final answer for each word is a blend of everyone's contributions (the Values), weighted by those relevance scores. The brilliant part is that this happens for every word simultaneously, in one big parallel computation rather than one word at a time.
Historical Development
Origins · 1 minhow this was discovered
Why It Matters
Prerequisites
What This Leads To
See Also
Multi-Head Attention · Transformer Architecture · Positional Encoding · Scaled Dot-Product Attention · Flash Attention