No history yet

Transformer Architecture Explained

Beyond One Word at a Time

Older AI language models, like Recurrent Neural Networks or RNNs as they're known, had a fundamental limitation: they processed text sequentially. Think of it like reading a sentence one word at a time, trying to remember the beginning by the time you reach the end. This approach struggled with long-range dependencies, where the meaning of a word depends on another word far earlier in the text. It created a memory bottleneck that limited their understanding of complex context.

The Transformer architecture, introduced in a 2017 paper titled "Attention Is All You Need," solved this by processing all words in a sentence simultaneously. Instead of a one-by-one sequence, it looks at the entire input at once. This parallel processing allows it to draw connections between any two words, no matter how far apart. The mechanism that makes this possible is called self-attention.

Lesson image

How Self-Attention Works

Self-attention lets a model weigh the importance of different words in a sentence relative to a specific word. When processing the word "it" in the sentence "The robot picked up the ball because it was heavy," self-attention helps the model figure out that "it" refers to "ball," not "robot."

To do this, the model creates three special vectors for each word in the input: a Query, a Key, and a Value.

  • Query (Q): This represents the current word you're focusing on. It's like a question asking, "Who is relevant to me?"
  • Key (K): This is like a label for all the other words in the sentence. It responds to the Query's question.
  • Value (V): This contains the actual meaning or substance of each word.

The model calculates a score by taking the dot product of the Query vector for the current word with the Key vector of every other word. This score determines how much attention the current word should pay to each of the other words. These scores are then scaled down for numerical stability, passed through a softmax function to turn them into probabilities, and finally used to create a weighted sum of all the Value vectors. The result is an output vector that represents the word's meaning in its full context.

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

From Text to Vectors

Before a Transformer can perform self-attention, it needs to convert text into a format it can understand: numbers. This happens in two main steps.

First is tokenization. The input text is broken down into smaller pieces called tokens. A token can be a word, part of a word (a subword), or a punctuation mark. For example, "unbelievably" might become two tokens: "un" and "believably."

Next, each token is mapped to a numerical vector using an embedding layer. These aren't just arbitrary numbers. Vector embeddings are high-dimensional representations where tokens with similar meanings are located closer to each other in vector space. This process turns a sequence of words into a sequence of vectors, ready for the model to process. Since the model processes all tokens at once, it loses the original word order. To fix this, a 'positional encoding' vector is added to each word's embedding, giving the model information about each token's position in the sequence.

Architecture and Flow

A full Transformer consists of a stack of encoders and a stack of decoders. The encoder's job is to process the input sequence and build a rich contextual understanding. The decoder's job is to take that understanding and generate an output sequence, one token at a time.

Each encoder layer has two main components: a multi-head self-attention mechanism and a simple feed-forward neural network. The decoder layer has three: a self-attention mechanism, another attention mechanism that looks at the encoder's output, and a feed-forward network. Both layers use residual connections (or 'skip connections') and layer normalization to help information flow better through the deep network.

However, not all models use this full architecture. Many powerful models are 'decoder-only,' like the famous series. These models are particularly good at text generation because their entire structure is optimized for predicting the next token in a sequence. In contrast, 'encoder-only' models like BERT are excellent at tasks that require a deep understanding of the entire input, such as sentence classification or question answering.

The self-attention mechanism gives models a powerful way to understand context, and its parallel nature makes it highly efficient to train on modern hardware. This combination unlocked the ability to build much larger and more capable models, leading directly to the AI revolution we see today.