§ Machine Learning · Jun 2026

The Intuition Behind Self-Attention

Attention has many distinct advantages over its RNN predecessor. This article focuses on the intuition behind attention, so you can understand why it's so powerful and widely used.

Published
June 21, 2026
Category
Machine Learning
Reading time
4 min
Tags
machine-learningtransformers

Attention is the successor to recurrent neural networks (RNNs) and is extremely popular. In fact, it’s the core mechanism that powers large language models (LLMs). Attention offers three distinct advantages compared to its RNN counterpart:

  1. Parallelism: RNNs process tokens sequentially. Attention has no such restriction, and each token can be processed in parallel.
  2. Constant Path Length: For an RNN to relate token 1 to token 100, information has to pass through 99 intermediate hidden states. In attention, any two tokens are connected directly in a single step.
  3. No Fixed-Size Information Bottleneck: In RNNs, you have to cram all the past information into a single fixed-size hidden vector. Attention has one vector per token which removes this bottleneck.

Queries, Keys, and Values

Attention is based on the core premise that the true meaning of a token is context-dependent. For example, assume we want to represent the meaning of “plane” in the following two sentences:

  1. The plane will be landing at 9:00 PM tonight.
  2. The 2D plane is defined by the set of all vectors in R2\mathbb{R}^2.

The word “plane” means something very different in each sentence. Attention allows us to update the representation of a token based on its surrounding context using queries, keys, and values:

  1. Query: A learned vector representation of a token that is used to “ask” every token in the sequence, itself included, whether they are important to the current token’s meaning.
  2. Key: A learned vector representation of a token that is used to “answer” a query (from any token, including its own) about whether it’s important to the asking token’s meaning.
  3. Value: A learned vector representation of a token that holds the actual content it contributes. After relevance has been determined from the queries and keys, each token’s value is blended together, weighted by that relevance, to form the new representation.

Having three different representations is intentional. We want the token to behave differently based on its current role: asking questions, answering them, or generating a new representation.

querykeyweightvalueoutputqk1k2k3v1v2v30:10:70:2z

The above visual describes how the attention mechanism works for a single token. Let’s take our “plane” example from earlier. Each token in the sequence has a query/key/value representation. For determining the meaning of plane, we take the dot product of plane’s query vector with the key vectors of all the tokens in the sequence. That produces a real-valued score for each token in the sequence which answers the question: How relevant is this token for determining the meaning of plane? We take the softmax of all the scores so that the scores form a probability distribution.

The final step is to take each token, multiply its value vector by its score, and sum up all the results. This produces a new representation of the word “plane” that incorporates the surrounding context.

Matrix Representation

One of the major benefits of attention is that it can be parallelized. We can see this in practice by looking at the matrix form for attention:

Z=Attention(Q,K,V)=softmax(QKTdk)VZ = \operatorname{Attention}(Q, K, V) = \operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V

where dkd_k is the size of the query/key vectors, dvd_v is the size of the value vector, and QRn×dk,KRn×dk,VRn×dvQ \in \mathbb{R}^{n \times d_k}, K \in \mathbb{R}^{n \times d_k}, V \in \mathbb{R}^{n \times d_v} are row-matrices containing the queries, keys, and values for each token in the sequence. This illustration represents the effect of doing the operation listed above:

0:70:10:10:10:20:50:20:10:10:20:60:10:10:10:30:5k1k2k3k4keys(attendedto)q11:0q21:0q31:0q41:0queries(attending)§z3v1v2v3v4

This matrix form is the exact same operation we listed above! To convince yourself of why, take a look at the matrix we drew in the illustration. Let’s call A=softmax(QKTdk)A = \operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right). Every position (i,j)(i, j) in AA is the result of taking the i-th query vector and dotting it with the j-th key vector.

Our goal is for the output to be a row-matrix ZRn×dvZ \in \mathbb{R}^{n \times d_v} where the i-th row corresponds to the meaning of the i-th token. Multiplying our n×nn \times n matrix in the illustration with VV produces exactly that. To see why, remember that matrix multiplication essentially produces:

Z[i][j]=kA[i][k]V[k][j]    Z[i][:]=kA[i][k]V[k][:]Z[i][j] = \sum_k A[i][k] \cdot V[k][j] \implies Z[i][:] = \sum_k A[i][k] \cdot V[k][:]

This is saying that the value of row i in ZZ is given by taking the sum over all k of token i’s attention on token k times token k’s value vector. This is exactly what we wanted.

An important detail we glossed over earlier is the dk\sqrt{d_k} factor. This is intentionally used to normalize the variance of the attention scores. If the variance is too high, softmax converges to one-hot vectors, which produces vanishing gradients during backpropagation.