No history yet

Transformer Foundations

Beyond Sequential Thinking

For years, models designed to understand language, like Recurrent Neural Networks (RNNs), worked a bit like we read: one word at a time, in order. They would process the first word, remember a bit of it, then move to the second word, trying to keep the first word's context in mind. This worked well for short sentences, but it had a critical flaw. By the time the model reached the end of a long paragraph, it had often forgotten crucial details from the beginning. This is known as the long-range dependency problem.

Imagine trying to remember the first sentence of a chapter by the time you reach the last page. It’s a similar challenge for sequential models. Important context gets diluted over distance.

The Transformer architecture, introduced in a landmark 2017 paper titled Attention Is All You Need, changed everything. Instead of processing text word-by-word, it looks at the entire sequence at once. This allows it to directly compare and relate every word to every other word, no matter how far apart they are. This ability to see the 'big picture' is what gives modern AI its power.

The Self-Attention Mechanism

The core innovation of the Transformer is self-attention. It’s a mechanism that allows the model to weigh the importance of different words in a sentence when processing a specific word. Consider the sentence: “The cat didn’t cross the road because it was too tired.” For the model to understand this, it must figure out what “it” refers to. Does it refer to the road or the cat? Self-attention solves this by creating three vectors for each word in the input: a Query, a Key, and a Value.

  • Query (Q): A question from the current word. For “it,” the query is essentially, “What in this sentence could I be referring to?”
  • Key (K): A label or identifier for every other word. The word “cat” has a Key that says, “I am a noun, an animal, a potential subject.”
  • Value (V): The actual substance or meaning of each word.

The model calculates an attention score by matching the Query vector of the current word (“it”) with the Key vector of every other word in the sentence. A high score means a strong relevance. The Key for “cat” will produce a high score, while the Key for “road” will produce a low one. The model then uses these scores to create a weighted sum of all the Value vectors. The result is a new representation for “it” that is heavily influenced by the meaning of “cat,” effectively linking the two.

Multiple Perspectives

A single self-attention calculation gives the model one way of connecting words. But language is complex. Words can relate to each other grammatically, semantically, or structurally. To capture this richness, Transformers use Multi-Head Attention.

This means the model doesn’t just do the Query, Key, and Value process once. It does it multiple times in parallel, with each “head” learning to focus on a different type of relationship. One head might learn to identify subject-verb pairings. Another might track pronoun references. A third could focus on cause-and-effect relationships. The outputs from all these heads are then combined, giving the model a much deeper and more nuanced understanding of the text than any single attention mechanism could achieve alone.

Lesson image

This parallel approach is a key advantage of Transformers. Unlike RNNs that had to wait for the previous word to be processed, all attention heads can do their work simultaneously. This makes parallel processing on modern hardware, like GPUs, incredibly efficient and allows for the training of much larger and more powerful models.

Keeping Things in Order

If a Transformer looks at every word at once, how does it know the original order of the sentence? Without this information, “The dog chased the cat” and “The cat chased the dog” would look identical. The solution is Positional Encoding.

Before the words are fed into the attention mechanism, a piece of information—a vector representing each word's position—is added to its embedding. This vector is generated using a clever trick with sine and cosine functions, which gives each position a unique signature. This allows the model to learn the relative positions of words, preserving the crucial sequential information while still benefiting from parallel processing.

PE(pos,2i)=sin(pos/100002i/dmodel)PE(pos,2i+1)=cos(pos/100002i/dmodel)PE_{(pos, 2i)} = \sin(pos / 10000^{2i/d_{\text{model}}}) \\ PE_{(pos, 2i+1)} = \cos(pos / 10000^{2i/d_{\text{model}}})

Finally, for generative models like GPT, the goal is next-token prediction. The model is trained to predict the next word in a sentence based on the words that came before it. To do this during training, a technique called Causal Masking is used. The model is given a full sentence but is 'masked' from seeing future words. For example, when trying to predict the fourth word, it can only 'see' the first three. This forces the model to learn how to generate coherent sequences of text, one word at a time, based only on the preceding context.

Quiz Questions 1/6

What was the primary limitation of earlier models like Recurrent Neural Networks (RNNs) that the Transformer architecture was designed to solve?

Quiz Questions 2/6

In the self-attention mechanism, how are attention scores calculated to determine the relevance of words to each other?