No history yet

Introduction to Transformer Models

What Are Transformers?

Transformer models are a type of neural network architecture that has reshaped the field of artificial intelligence, particularly in how machines understand and generate human language. Before transformers, models like Recurrent Neural Networks (RNNs) processed text one word at a time, in order. This sequential approach made it difficult to capture long-range relationships in the text and was slow to train.

We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely.

Transformers changed the game by processing all words in a sequence at the same time. This parallel processing allows them to learn complex patterns and context across an entire document much more effectively. The key innovation that makes this possible is called the self-attention mechanism.

The Core Architecture

The original Transformer model consists of two main parts: an encoder and a decoder. Think of them as two people collaborating on a translation task.

  • The Encoder's job is to read and understand the input sequence, like a sentence in English. It processes the entire sentence and builds a rich numerical representation that captures its meaning and context.
  • The Decoder's job is to take that representation from the encoder and generate the output sequence, like the same sentence translated into French. It generates the output one word at a time, looking at what the encoder understood and what it has already written.
Lesson image

Both the encoder and decoder are built from a stack of identical layers. Stacking these layers allows the model to learn progressively more complex patterns in the data. While this two-part structure is common, some of the most famous models today use only one part. For example, models like BERT are encoder-only, while models like GPT are decoder-only.

The Secret Sauce Self-Attention

Self-attention is the mechanism that allows the model to weigh the importance of different words in a sequence when processing a specific word. It helps the model build context.

Consider the sentence: "The animal didn't cross the street because it was too tired." What does "it" refer to? A human instantly knows "it" is the animal, not the street. Self-attention gives the model a similar ability. When processing the word "it," the model can pay more attention to "animal" and less to other words, correctly understanding the relationship.

Self-attention works by comparing every word in a sentence with every other word, creating a score of how related they are.

To do this, the model creates three vectors for each input word: a Query (Q), a Key (K), and a Value (V).

  • Query: Represents the current word's request for information.
  • Key: Represents a word's label or identifier, which the Query can match against.
  • Value: Represents the actual content of the word.

The model calculates attention scores by taking the dot product of the current word's Query vector with the Key vectors of all other words in the sentence. These scores are then scaled and passed through a softmax function, which turns them into weights that sum to one. Finally, these weights are used to create a weighted sum of all the Value vectors, producing a new representation for the current word that is infused with context from the entire sentence.

Lesson image

Transformers take this a step further with Multi-Head Attention. Instead of calculating attention just once, it performs the Q, K, V process multiple times in parallel. Each "head" can learn different types of relationships. One head might focus on grammatical links, while another focuses on semantic meaning. The outputs from all heads are then combined, giving the model a more nuanced understanding of the text.

Lesson image

Keeping Things in Order

Because self-attention processes all words simultaneously, the model loses the original word order. "The cat chased the dog" and "The dog chased the cat" would look the same to the model without some help. This is where positional encoding comes in.

Positional encoding is a vector added to each word's initial representation (its embedding). This vector provides information about the word's position in the sequence. It's like adding a unique timestamp to each word so the model knows where it appeared.

These encodings are generated using sine and cosine functions of different frequencies. This clever mathematical trick allows the model to easily learn the relative positions of words.

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}

Here, pospos is the position of the word in the sentence and ii is the dimension of the encoding. This provides a unique and consistent signal about word order that the model can use throughout its calculations.

Putting It All Together

Within each encoder and decoder layer, a few other components play important roles.

After the multi-head attention mechanism creates its context-rich word representations, each one is passed through a Feed-Forward Neural Network (FFN). This network is a standard, simple neural network that processes each word's representation independently. It adds another layer of computation, allowing the model to learn more complex transformations.

Lesson image

Each layer also includes residual connections and layer normalization steps. A residual connection takes the input of a sub-layer (like the attention block) and adds it to the output. This helps prevent information from being lost as it passes through deep networks. Layer normalization then standardizes the outputs to keep the training process stable.

By combining self-attention, positional encodings, and feed-forward networks within a stacked encoder-decoder architecture, Transformers can process language with unprecedented depth and efficiency. This design is what has enabled the massive leap forward in AI's ability to work with text, code, and even images.

Time to check your understanding of the Transformer architecture.

Quiz Questions 1/5

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

Quiz Questions 2/5

In the context of the self-attention mechanism, what are the three vectors created for each input word?

This foundational architecture is the basis for most of today's large language models, making it a cornerstone of modern AI.