No history yet

Transformer Architecture

Beyond Recurrence

For a long time, if you wanted a model to understand sequences like text, you reached for a Recurrent Neural Network (RNN). RNNs process information one piece at a time, carrying a memory of what came before. This sequential nature seems intuitive for language. The first word informs the second, the second informs the third, and so on.

But this step-by-step process has a major bottleneck. First, it's slow. You can't process the tenth word until you've processed the first nine. This makes it difficult to take advantage of modern parallel processors like GPUs. Second, RNNs struggle with long-term memory. By the time an RNN reaches the end of a long paragraph, it has often forgotten the nuances of the beginning. This is known as the vanishing gradient problem, where the influence of earlier inputs fades with each step, making it hard for the network to learn long-range dependencies.

The Transformer architecture solved these problems by getting rid of recurrence entirely. Instead of processing word by word, it looks at the entire sentence at once.

This parallel approach is made possible by a mechanism called self-attention. It allows the model to weigh the importance of different words in a sequence relative to each other, creating a rich, context-aware understanding of the text.

The Mechanics of Attention

Self-attention works by creating three special vectors for each word token in the input: a Query, a Key, and a Value. You can think of this process like searching for information in a library.

The Query is what you are looking for. It's the current word trying to understand its own context. The Key is like the label on a filing cabinet. It's a description of what each word in the sentence is. The Value is the actual content inside the filing cabinet. It's the meaningful information that each word holds.

For a single word, its Query vector is compared against the Key vector of every other word in the sentence (including itself). This comparison produces a score. A high score means the key-word is highly relevant to the query-word. These scores are then normalized (using a softmax function) so they add up to 1, effectively becoming weights. Finally, each word's Value vector is multiplied by these weights and summed up. The result is a new representation for the original word, infused with context from its most relevant neighbors.

But one perspective isn't always enough. A word can have multiple relationships. In "The bank on the river bank is closed," the first "bank" relates to finance while the second relates to geography. To handle this, Transformers use Multi-head attention. Instead of doing the self-attention process once, it does it multiple times in parallel. Each of these "heads" learns a different type of relationship. One head might focus on grammatical links, while another focuses on semantic ones. The outputs from all heads are then concatenated and combined, providing a much richer and more nuanced representation of the sentence.

Lesson image

Order in the Chaos

By processing all words at once, we solve the speed problem of RNNs. But we also create a new one: the model has no idea what order the words came in. To a basic self-attention mechanism, "the cat sat on the mat" looks exactly the same as "mat the on sat cat the".

This is where positional encoding comes in. It's a clever trick to give the model a sense of sequence. Before the words are fed into the attention layers, a vector representing each word's position is added to its embedding vector. These positional vectors are generated using sine and cosine functions of different frequencies.

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

This method allows the model to easily learn to attend to relative positions, since the distance between any two positions can be calculated from their encodings. The model doesn't just know a word is at position 5; it knows how position 5 relates to positions 2 and 8.

Architectural Flavors

The original Transformer model, introduced in the paper "Attention Is All You Need", had two main parts: an encoder and a decoder. The encoder's job is to read the input sentence and build a rich numerical representation of it. The decoder's job is to take that representation and generate an output sentence, one word at a time. This encoder-decoder structure is ideal for sequence-to-sequence tasks like machine translation.

Over time, different variations have emerged.

Encoder-only models (like BERT) are great at understanding context. They see the entire input at once (bidirectional context) and are often used for tasks like sentiment analysis or text classification, where you need a deep understanding of a piece of text rather than generating a new one.

Decoder-only models (like the GPT series) are designed for generation. They are auto-regressive, meaning they predict the next word based on all the previous words they have seen. They only look backward (causal attention), making them perfect for tasks like writing articles, composing emails, or powering chatbots.

Let's test your understanding of the Transformer architecture.

Quiz Questions 1/5

What is a primary bottleneck of Recurrent Neural Networks (RNNs) that the Transformer architecture was designed to overcome?

Quiz Questions 2/5

In the self-attention mechanism, three vectors are created for each input token: the _______ vector is compared against the _______ vector of every other token to compute a score, and these scores are then used to weight the _______ vectors.

The Transformer's ability to process information in parallel, combined with its powerful attention mechanism, is the foundation upon which modern AI is built.