No history yet

Core Attention Mechanics

The Attention Mechanism

The core of the Transformer architecture is its attention mechanism. Instead of processing data sequentially like an RNN, attention allows the model to weigh the importance of all other words in a sequence when encoding a specific word. This is accomplished through three key vectors for each input token: the Query, the Key, and the Value.

Think of it like searching for information in a library. Your query is the question you have. The books' titles are the keys you scan. The content of the book you choose is the value you get.

For every input token, we create a Query vector (Q), a Key vector (K), and a Value vector (V). These are generated by multiplying the token's embedding by three distinct weight matrices (WQW^Q, WKW^K, WVW^V) that are learned during training. These matrices project the input embeddings into different representation subspaces, allowing the model to calculate attention from different perspectives.

Lesson image

Scaled Dot-Product Attention

The model calculates an attention score by taking the dot product of a word's Query vector with the Key vectors of all other words in the sequence. A higher dot product signifies greater relevance between the query word and a key word.

A core innovation is the scaled dot-product attention mechanism, which computes attention scores by taking the dot product of query (Q) and key (K) matrices, scaling by the square root of the key dimension d_k to prevent vanishing gradients in softmax, and applying the resulting weights to the value (V) matrix.

This process produces raw attention scores. To stabilize the gradients during training, these scores are scaled down by dividing by the square root of the dimension of the key vectors, dkd_k. This scaling is crucial. Without it, for large values of dkd_k, the dot products can grow very large, pushing the softmax function into regions where it has extremely small gradients. This makes learning difficult.

After scaling, a softmax function is applied to the scores. This converts the scores into a probability distribution, where all values are between 0 and 1 and sum to 1. Each value represents the weight of attention one word should pay to another. Finally, these attention weights are multiplied by the Value vectors of each word in the sequence and summed up to produce the final output for the query word. This output is a weighted blend of all other words' values, with more relevant words contributing more to the final representation.

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

Multi-Head Attention

A single attention mechanism can only focus on one type of relationship between words at a time. To overcome this limitation, Transformers use Multi-Head Attention. Instead of performing a single attention function, this mechanism runs Scaled Dot-Product Attention multiple times in parallel.

Multi-head attention significantly expands transformer capabilities by running multiple attention operations in parallel—each operating in different representation subspaces.

The input Q, K, and V matrices are linearly projected into different, smaller-dimensional subspaces for each "head." Attention is then calculated for each head independently. This allows the model to jointly attend to information from different representation subspaces at different positions. For example, one head might learn to track syntactic relationships, while another tracks semantic ones.

Lesson image

After each head produces its output, the outputs are concatenated back together and passed through a final linear projection. This combines the knowledge learned from the different attention heads into a single, unified representation.

MultiHead(Q,K,V)=Concat(head1,...,headh)WOwhere headi=Attention(QWiQ,KWiK,VWiV)\text{MultiHead}(Q, K, V) = \text{Concat}(\text{head}_1, ..., \text{head}_h)W^O \\ \text{where head}_i = \text{Attention}(QW_i^Q, KW_i^K, VW_i^V)

Let's review the core concepts we've covered.

Now, check your understanding of these mechanisms.

Quiz Questions 1/6

In the Transformer attention mechanism, what are the three vectors created for each input token?

Quiz Questions 2/6

What is the primary reason for scaling the raw attention scores by the square root of the key vector's dimension (dk\sqrt{d_k})?

By running multiple attention mechanisms in parallel, Multi-Head Attention gives the Transformer model a more nuanced understanding of language, forming the foundation for its powerful capabilities.