A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Attention Mechanisms

Research Depth 92 in the knowledge graph ☐ I know this ☆ Set as goal
9topics build on this
638prerequisites beneath it
See this on the map →
Neural Network FundamentalsDot Product (Inner Product in R^n)+4 more→→Self-Attention and Multi-Head AttentionSequence-to-Sequence Models+1 more
deep-learning attention sequence-models

Core Idea

Attention computes weighted combinations of values based on query-key similarity, focusing on relevant input parts. Scaled dot-product attention computes Q·KT/√d_k before softmax weighting. Multi-head attention applies attention in parallel with different representations.

Explainer

From your study of neural networks, you know that a standard feedforward layer applies the same learned transformation to every input position independently. This works well for fixed-size inputs, but it creates a fundamental problem for sequences: how does the network at position 5 know what happened at position 1? Recurrent networks addressed this by passing hidden states forward step by step, but this sequential processing is slow and information from distant positions gets diluted through many steps. Attention mechanisms solve this by allowing every position to directly look at every other position and decide what is relevant — no sequential bottleneck required.

The core idea is a soft lookup table. Imagine you have a database of key-value pairs and a query. In a traditional lookup, you find the exact matching key and return its value. Attention does a *soft* version: it compares the query to every key, computes a similarity score for each, converts those scores into weights (using softmax so they sum to 1), and returns a weighted combination of all values. The output is dominated by values whose keys best match the query but still incorporates information from all positions. In scaled dot-product attention, the similarity between a query q and key k is computed as their dot product (from your linear algebra prerequisites), divided by √d_k to prevent the dot products from growing too large in high dimensions. Large dot products would push softmax into regions where its gradients are extremely small, stalling learning — the scaling factor keeps the gradients healthy.

In matrix form, attention over an entire sequence is computed as Attention(Q, K, V) = softmax(QKT/√d_k)V. Here Q, K, and V are matrices where each row corresponds to a position in the sequence. The matrix QKT computes all pairwise similarities at once — entry (i,j) measures how much position i should attend to position j. After softmax normalizes each row into a probability distribution, multiplying by V produces the output: each position's output is a weighted average of all value vectors, with weights determined by query-key compatibility. This entire operation is a matrix multiplication pipeline, making it highly parallelizable on GPUs — a crucial advantage over sequential recurrent processing.

Multi-head attention extends this by running several attention operations in parallel, each with its own learned projection matrices for Q, K, and V. Think of each head as asking a different question about the input: one head might attend based on syntactic relationships, another based on semantic similarity, another based on positional proximity. Each head operates on a lower-dimensional projection (d_k/h dimensions per head for h heads), so the total computation is comparable to single-head attention at full dimensionality. The outputs of all heads are concatenated and linearly projected back to the model dimension. This allows the model to simultaneously capture different types of relationships between positions — a capability that proved essential for the transformer architecture's success across language, vision, and beyond.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Dividing Integers → Unit Rates → Proportions → Percent Concept → Converting Between Fractions, Decimals, and Percents → Operations with Rational Numbers → Two-Step Equations → Solving Multi-Step Equations → Equations with Variables on Both Sides → Angle Pairs: Complementary, Supplementary, and Vertical → Parallel Lines and Transversals → Corresponding Angles → Alternate Interior Angles → Triangle Angle Sum Theorem → Exterior Angle Theorem → Triangle Inequality Theorem → Similar Triangles: AA Similarity → Similar Triangles: SSS and SAS Similarity → Proportions in Similar Triangles → Right Triangle Trigonometry Introduction → Sine, Cosine, and Tangent Ratios → Trigonometric Ratios Review → Radian Measure → Converting Between Degrees and Radians → The Unit Circle → Graphing Sine and Cosine → Graphing Tangent and Reciprocal Trigonometric Functions → Derivatives of Trigonometric Functions → Antiderivatives → Indefinite Integrals → Basic Integration Rules → Riemann Sums → Definite Integral Definition → Probability Density Functions and Continuous Distributions → Cumulative Distribution Functions → Continuous Random Variables → Probability Density Functions → Expected Value → Linear Regression in Machine Learning → Neural Network Fundamentals → Attention Mechanisms

Longest path: 93 steps · 638 total prerequisite topics

Prerequisites (6)

Leads To (3)