No history yet

Transformer Core Mechanics

The Attention Mechanism

At the heart of a Transformer is a mechanism called attention. It allows the model to weigh the importance of different words in the input text when processing a specific word. Think of it like a search engine. When you type a query, the engine doesn't just look for exact matches; it tries to understand the context and relationships between words to find the most relevant documents.

To do this, the model creates three vectors for each input token: a Query (Q), a Key (K), and a Value (V). The Query is like the current word being processed. The Key is like a label for all the other words in the sequence. The model compares the Query of the current word to the Key of every other word to calculate a score. This score determines how much "attention" to pay to each word.

The final step is to use these scores to create a weighted sum of the Value vectors. The words with higher attention scores will contribute more to the final representation of the current word. This entire process is known as Scaled Dot-Product Attention and is the fundamental building block of how Transformers understand context.

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

Multiple Perspectives

A single attention mechanism is good, but it can only focus on one type of relationship at a time. To capture the rich, varied connections in language, Transformers use Multi-Head Attention. This is like having a team of specialists look at the same sentence. One might focus on subject-verb agreement, another on pronoun references, and a third on semantic similarity.

In practice, the model doesn't explicitly assign these roles. It simply runs several attention mechanisms, or "heads," in parallel. Each head has its own set of Q, K, and V weight matrices, allowing it to learn different aspects of the text. The outputs from all the heads are then concatenated and passed through a final linear layer to produce the final output for that layer.

Lesson image

After the attention mechanism has figured out the contextual relationships, the information is passed to a Feed-Forward Network (FFN). This is a standard, fully connected neural network applied independently to each position. Its job is to process the attention output and introduce non-linearity into the model.

This step is crucial. While the attention mechanism is great at gathering and weighting information based on relationships, the FFN is what allows the model to transform that information into a more complex, higher-level representation. It's where the model does its deeper "thinking," interpreting the contextual clues it just gathered.

Lesson image

Keeping the Signal Clear

As models get deeper, with hundreds of layers, a problem called the vanishing gradient can occur. Information from the initial layers can get lost or distorted as it passes through so many transformations. Transformers use two simple but powerful tricks to combat this: residual connections and layer normalization.

A residual connection, or skip connection, creates a direct path for the input of a layer to be added to its output. It’s like an information highway that bypasses the complex processing, ensuring that the original signal is never completely lost. This allows the model to learn modifications to the input, rather than having to learn the entire transformation from scratch.

Layer normalization is a standardization step. After the residual connection adds the input and output together, layer normalization rescales the data to have a mean of 0 and a standard deviation of 1. This keeps the numbers within a stable range, preventing them from exploding or shrinking as they pass through the network, which makes training faster and more reliable.

Finally, since the Transformer processes all tokens simultaneously, it has no inherent sense of word order. To fix this, we add positional embeddings to the input token embeddings. These are vectors that represent the position of a word in a sequence.

Early models used absolute positional embeddings, where each position (1st, 2nd, 3rd, etc.) had a unique vector. More recent models often use relative positional embeddings, which encode the distance between words. This approach is more flexible, as it helps the model generalize to sequences of different lengths than it saw during training.

Now, let's test your understanding of these core components.

Quiz Questions 1/5

In the context of a Transformer's attention mechanism, what role do the Query (Q), Key (K), and Value (V) vectors play?

Quiz Questions 2/5

Why do Transformers use Multi-Head Attention instead of a single attention mechanism?

By combining these mechanisms, the Transformer architecture can build a rich, context-aware understanding of language, transforming simple sequences of tokens into a complex map of meaning. This is the foundation that allows LLMs to perform sophisticated language tasks.