Skip to content
E
Educatum

Self Attention

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

Self-attention enables every token in a sequence to aggregate contextual information from all other tokens in parallel. Each token projects into Query (Q), Key (K), and Value (V) vectors, and softmax-normalized scaled dot products between Q and K weight the V vectors into updated token representations.

Reference Diagram & Materials
Self Attention matrix calculation diagram
Self Attention worked exercise table

Understanding Self-Attention Using a Classroom Meeting Analogy

Overview

Imagine a classroom having a meeting to discuss organizing class activities:

1. Each Student Plays Three Roles

  • Speaker (Query): Expressing what information or perspective they are looking for.
  • Listener (Key): Advertising what topic or expertise they represent so others know how relevant they are.
  • Contributor (Value): Supplying the actual information and suggestions to share.

2. Attention Scoring Process

Suppose Tom is trying to update his plan:

  • He broadcasts his question: “I think our class needs a fun activity.” (his Query).
  • Other students match against his Query based on their expertise (their Keys):
    • Lucy, the sports representative, has relevant experience organizing activities (high match).
    • Mike, the academic representative, is somewhat relevant for competitions (medium match).
    • John, who is dozing off, offers no relevant input (low match).

3. Information Integration

  • Tom forms a weighted blend of everyone’s suggestions (Values) according to the normalized attention weights:
    • Heavily incorporates Lucy’s specific sports ideas.
    • Moderately incorporates Mike’s competition ideas.
    • Virtually ignores John’s irrelevant input.

4. Parallel Processing

  • Unlike sequential recurrence, every student updates at once—simultaneously acting as a Query (seeking context), a Key (advertising relevance), and a Value (contributing content).

5. Final Effect

  • Each token obtains an updated representation enriched by context across the entire sequence.

Mathematical Formulation

Given an input sequence matrix X∈Rn×dmodelX \in \mathbb{R}^{n \times d_{\text{model}}} of nn tokens, learned projection matrices WQ,WK∈Rdmodel×dkW_Q, W_K \in \mathbb{R}^{d_{\text{model}} \times d_k} and WV∈Rdmodel×dvW_V \in \mathbb{R}^{d_{\text{model}} \times d_v} produce:

Q=XWQ,K=XWK,V=XWVQ = X W_Q, \qquad K = X W_K, \qquad V = X W_V

Scaled Dot-Product Self-Attention computes:

Attention(Q,K,V)=softmax ⁣(QK⊤dk)V\text{Attention}(Q, K, V) = \text{softmax}\!\left(\frac{Q K^\top}{\sqrt{d_k}}\right) V

  • Why divide by dk\sqrt{d_k}? If entries of qq and kk have zero mean and unit variance, their dot product q⋅kq \cdot k has variance dkd_k. Dividing by dk\sqrt{d_k} keeps logits at unit variance so softmax does not saturate into near-zero gradients for large dkd_k.
  • Complexity: Computing the n×nn \times n attention matrix takes O(n2dk)O(n^2 d_k) time and O(n2)O(n^2) memory in sequence length nn.

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.