Applied Artificial Intelligence Systems
Transformer Mechanisms
The Transformer Revolution
Before 2017, models designed to understand language, like Recurrent Neural Networks (RNNs), processed text one word at a time, in order. This sequential process was logical—it's how we read—but it created two major bottlenecks. First, it was slow. A model couldn't process the tenth word until it had finished with the ninth. Second, it struggled with long-range dependencies. By the time the model reached the end of a long paragraph, it had often 'forgotten' the context from the beginning.
The Transformer architecture, introduced in a paper titled 'Attention Is All You Need', changed everything. It abandoned sequential processing entirely. Instead, it processes every word in a sentence at the same time, allowing for massive parallelisation and a much deeper understanding of context, no matter how far apart words are.
The Transformer's core innovation lies in its self-attention mechanism, which allows the model to weigh the importance of different parts of the input data relative to each other, enabling parallel processing and capturing long-range dependencies more effectively than previous architectures like recurrent neural networks (RNNs).
Self-Attention at the Core
The key mechanism behind the Transformer is self-attention. Imagine you're reading the sentence: "The robot picked up the screwdriver because it was heavy." To understand what 'it' refers to, you intuitively link 'it' to 'screwdriver', not 'robot'. Self-attention allows a model to do the same thing. For every word it processes, it looks at all the other words in the sentence and assigns a score, or weight, to each one, signifying its relevance.
To do this, the model creates three vectors for each input word: a Query (Q), a Key (K), and a Value (V).
- Query: This vector is like a question. For the word 'it', the query might be, "What thing in this sentence could be heavy?"
- Key: This vector is like a label on each word. For 'screwdriver', the key might say, "I am a physical object."
- Value: This vector contains the actual meaning or substance of the word.
The model compares the Query vector of one word with the Key vector of every other word. A high compatibility score (calculated using a dot product) means the words are highly relevant to each other. These scores, known as , determine how much of each word's Value vector contributes to the final representation of the word we're focusing on.
This process happens for every single word in the input, all at the same time. The result is that each word's final representation isn't just the word itself, but the word plus a rich, weighted blend of all the other words that provide context. The core calculation is known as Scaled Dot-Product Attention.
Multiple Perspectives
A single self-attention mechanism might learn to focus on one type of relationship, like how verbs relate to subjects. But language is more complex than that. Words can be linked by syntax, semantics, and many other subtle connections. This is where multi-head attention comes in.
Instead of doing one big attention calculation, the model runs several attention calculations in parallel. Each of these 'heads' gets its own set of Query, Key, and Value vectors. This allows each head to learn to focus on different types of relationships. One head might track pronoun references, another might follow syntactic dependencies, and a third could focus on semantic similarity. The outputs from all heads are then concatenated and combined, giving the model a much richer and more nuanced understanding of the text.
These attention mechanisms are the building blocks of the two main components of a Transformer: the encoder and the decoder.
The encoder's job is to understand the input text. The decoder's job is to generate the output text.
The is a stack of layers that processes the entire input sentence. Each layer applies self-attention and then passes the result through a feed-forward neural network. The output of the encoder is a set of contextual representations—one for each input word—that captures a deep understanding of the sentence's meaning and structure.
The also has self-attention layers, but it has an additional attention layer that pays attention to the output of the encoder. Its task is to generate an output sequence, one word at a time. At each step, it looks at the encoder's output (the full meaning of the input) and the words it has already generated to decide which word to produce next.
Knowing the Order
Since the Transformer processes all words simultaneously, it has no inherent sense of word order. To the base model, "The cat chased the dog" and "The dog chased the cat" are identical bags of words. To solve this, Transformers use positional encoding.
Before the words are fed into the first encoder layer, a unique vector representing each word's position is added to its embedding (the initial vector representing the word's meaning). This encoding gives the model a signal about the relative and absolute position of words in the sequence, allowing it to understand the crucial role of word order in language.
By combining parallel processing, self-attention, a dual encoder-decoder structure, and positional information, the Transformer architecture provides a powerful and efficient foundation for understanding and generating human language. It is the engine driving nearly all modern large language models.
What were the two primary bottlenecks of pre-2017 sequential language models like Recurrent Neural Networks (RNNs)?
In the Transformer architecture, what is the role of the Query (Q), Key (K), and Value (V) vectors?
