☆ Save 7.3 Self-Attention Mechanism — From Query–Key–Value to Matrix Computation

06/15/2026

This article explains how the Self-Attention Mechanism is computed, starting from the Query–Key–Value (QKV) formulation and extending to Matrix Computation. Self-Attention is not just an intuitive idea of “looking at relevant words”; it is a mathematical process where each token computes relationships with all other tokens, forming the core of the Transformer architecture.

This article also breaks down Self-Attention into a sequence of steps: vector generation → similarity computation → normalization → weighted sum, and shows how these steps can be unified into a single matrix operation.

Table of Contents

Self-Attention vs Cross Attention

Self-Attention is a mechanism where each token in a sequence attends to all other tokens within the same sequence. In other words, all words in a sentence compute relationships with each other to update their representations.

In contrast, Cross Attention computes relationships between two different sequences. For example, in machine translation, the output sequence attends to the input sequence. Self-Attention models intra-sequence relationships, while Cross Attention models inter-sequence relationships.

Query–Key–Value Vector Generation

The first step in Self-Attention is generating Query, Key, and Value vectors from input embeddings. Each of these vectors plays a distinct role in how attention selects and transforms information.

Each token represents: “what information it wants to find (Query)”, “what characteristics it has (Key)”, and “what information it will pass on (Value)”. These roles are separated using different projections.

For example, if the embedding dimension is 512 and the QKV dimension is 64, the input is projected into a lower-dimensional space used for attention computation. This is not simple compression but role-specific transformation.

Attention Score Computation

The second step computes similarity between Query and all Key vectors. This determines how much each token should attend to others.

For example, if q₁·k₁ = 112 and q₁·k₂ = 96, then the first token attends more strongly to the first key than the second. These scores are not probabilities yet but raw similarity values.

Scaling and Softmax

Dot-product values tend to grow with higher dimensionality. If Softmax is applied directly, it may produce overly peaked distributions, causing unstable gradients during training.

To address this, Scaling is applied by dividing by the square root of the key dimension.

After this, Softmax converts scores into attention weights. For example, scores [14, 12] become approximately [0.88, 0.12].

Output via Weighted Sum

The final step computes a weighted sum of Value vectors using attention weights. This produces the final output of Self-Attention.

For example, if Values are [10, 20] and weights are [0.88, 0.12], the output is 11.2, meaning the first token contributes more strongly.

Matrix Form of Self-Attention

In practice, all these operations are performed simultaneously using matrix operations. Instead of processing tokens individually, the entire sequence is computed in parallel as matrices.

This formulation enables the model to compute relationships between all tokens at once. Thanks to this matrix formulation, Transformers achieve efficient parallel processing compared to RNNs, significantly improving scalability and training speed.

※ This article was independently compiled and adapted from lectures by Professor Sungroh Yoon at Seoul National University.

Recommended prerequisite reading (3/5)

+2

Recommended next reading (5/20)

+5

Posts on the same topic (3/3)

Related concepts (2/2)

📍 Where this concept fits in the AI learning map

See where this concept sits within the full AI Universe.

📍 Current position in AI Universe

Reset Show completed · Login required Loading…

🌌 AI Universe

⭐ Concept

Select a star.

View the full AI Universe

« 7.2 Attention and Langua…|7.4 Multi-Head Attention… »

🔖 Tags: Attention Mechanism · Deep Learning · natural language processing · query-key-value · self-attention · Transformer