Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 9

Word Embeddings and Transformer Architecture in NLP

Recommended resources

Introduction

Welcome to our next lesson. In our previous session, we explored Convolutional Neural Networks (CNNs), a specialized architecture brilliantly designed for processing grid-like data such as images. We saw how CNNs use concepts like parameter sharing and a hierarchy of feature detectors to efficiently understand spatial information.

This lesson pivots from spatial data to sequential data, specifically the natural language that underpins law, international relations, and everyday communication. Processing text presents a unique set of challenges. How can a machine understand that "bank" means something different in "river bank" versus "investment bank"? How can it grasp long-range dependencies in a legal contract, where a clause on page 1 affects the interpretation of a term on page 20?

Today, we will answer these questions by exploring the foundational technologies of modern Natural Language Processing (NLP). Our learning outcome is to explain the concept of word embeddings and the Transformer architecture. These are the core components that power Large Language Models (LLMs) like ChatGPT, making this a critical step in our journey to understand the technical underpinnings of AI governance.

We will cover:

  1. Word Embeddings: How we convert words from abstract symbols into meaningful numerical vectors.
  2. The Transformer Architecture: The revolutionary model that uses a mechanism called "self-attention" to understand context in text.

1. From Words to Vectors: The Idea of an Embedding

Before a neural network can process text, the words must be converted into numbers. A naive approach, like one-hot encoding (representing each word as a long vector of zeros with a single '1'), is deeply flawed. It creates astronomically large vectors and, more importantly, treats every word as being equally different from every other. "Dog" is as distant from "canine" as it is from "democracy".

To solve this, NLP uses word embeddings. The core idea is to represent each word as a relatively short, dense vector (a list of numbers) in a high-dimensional space. The key principle is that words with similar meanings should have vectors that are close to each other in this space.

These embeddings are not hand-crafted; they are learned. During training on a vast corpus of text, the network adjusts the vector for each word, positioning it in this "semantic space" based on the contexts in which it appears. This process can capture surprisingly rich relationships. The most famous example is the vector arithmetic: vector('King') - vector('Man') + vector('Woman') results in a vector very close to vector('Queen').

The following video provides an excellent visual introduction to this concept.

Transformers, the tech behind LLMs | Deep Learning Chapter 5

This video from the 3Blue1Brown channel provides a beautifully intuitive explanation of how words are turned into vectors and how directions within this high-dimensional 'embedding space' can encode semantic meaning.

Watch the segment from 12:27 to 18:28. Focus on the core idea: representing words as points in a high-dimensional space where distance and direction have meaning.

The embedding matrix, which stores the vector for every word in the model's vocabulary, constitutes the very first set of learnable parameters in our model.


2. The Rise of Context: Introducing the Transformer

Static embeddings like the ones we just saw were a major breakthrough, but they have a crucial limitation: a word has the same embedding regardless of its context. As noted earlier, this fails to distinguish "river bank" from "investment bank". To truly understand language, a model must be able to create contextualized representations.

This is the problem that the Transformer architecture, introduced in the 2017 paper "Attention Is All You Need," was designed to solve. It has since become the foundation for virtually all state-of-the-art LLMs.

At a high level, a Transformer processes a sequence of token embeddings and, through a series of "Transformer Blocks," outputs a new sequence of embeddings where each vector has been enriched with contextual information from the entire input.

Transformer Model Architecture
The complete architecture of the original Transformer model from the paper \"Attention Is All You Need\". It consists of an Encoder (left) and a Decoder (right). For generative models like GPT, we are primarily interested in the \"decoder-only\" part, which uses \"masked\" self-attention.

Let's watch a high-level overview of how data flows through a generative Transformer.

Transformers, the tech behind LLMs | Deep Learning Chapter 5

Let's return to the 3Blue1Brown video for a top-down view of the Transformer architecture. This will introduce the key components—attention blocks and feed-forward layers—that we will dissect next.

Watch from 02:52 to 05:53. This segment outlines the entire journey of data, from token embeddings, through repeating blocks of 'attention' and 'multi-layer perceptrons', to the final output.


3. The Core Mechanism: Self-Attention

The magic of the Transformer lies in its self-attention mechanism. This is how the model weighs the relevance of all other words in the input when interpreting a specific word. For example, when processing the word it in the sentence "The car stalled because it ran out of fuel," the attention mechanism should learn to pay high attention to car and fuel.

To accomplish this, self-attention projects each input word's embedding into three distinct vectors:

  • Query (Q): Represents the current word's "question" or what it's looking for. Think of it as a search query.
  • Key (K): Represents each word's "label" or what kind of information it offers. Think of it as the title of a webpage.
  • Value (V): Represents the actual content or meaning of each word. Think of it as the content of the webpage.

The process then unfolds as follows:

  1. Calculate Attention Scores: For the current word, its Query vector is compared with every other word's Key vector using a dot product. A high score means a strong relevance.
  2. Normalize Scores: These raw scores are passed through a softmax function, which converts them into a set of positive weights that sum to 1. These are the attention weights. They represent a probability distribution of "focus" over the input sequence.
  3. Produce the Output: The final output for the current word is calculated as a weighted sum of all the Value vectors in the sequence, using the attention weights from the previous step.

In this way, a word's representation is updated to be a mix of its own meaning and the meanings of the words to which it "attends".

The next video in the 3Blue1Brown series provides an exceptional step-by-step visual walkthrough of this mechanism.

Attention in transformers, step-by-step | Deep Learning Chapter 6

This video is dedicated entirely to the attention mechanism. It masterfully explains the roles of the Query, Key, and Value vectors and how they work together to create contextualized representations.

Please watch these three segments carefully: 6:09 - 8:22: Introduction to Query, Key, and Value vectors. 8:22 - 10:18: How dot products and softmax create the attention pattern (the weights). 13:04 - 15:44: How the attention pattern is used to create a weighted sum of Value vectors to update the original embedding. Focus on building an intuition for the purpose of Q, K, and V.

Multi-Head Attention

A Transformer doesn't just do this once. It uses Multi-Head Attention, which means it runs the self-attention process multiple times in parallel, each with its own set of learned Query, Key, and Value weight matrices. Each "head" can learn to focus on different types of relationships. For instance, one head might track syntactic dependencies (like subject-verb agreement), while another might track semantic relationships (like coreference).

The outputs from all heads are then concatenated and passed through another linear layer to produce the final output of the multi-head attention block.

Attention in transformers, step-by-step | Deep Learning Chapter 6

Let's conclude our study of attention by understanding the 'multi-head' aspect.

Watch from 19:20 to 22:16. The key takeaway is that running multiple attention heads in parallel allows the model to capture diverse types of contextual relationships simultaneously.

Test your understanding!

In the sentence, "The judges ruled against the appeal because they found no legal error," an ideal attention mechanism processing the word "they" would likely assign the highest attention weight to which other word? Why is the QKV mechanism well-suited for this?

Show answer

The highest attention weight for "they" should be assigned to "judges".

The QKV mechanism is well-suited because the model can learn weight matrices (WQ, WK, WV) that specialize in tasks like coreference resolution. In a simplified view, the Query vector for "they" would represent a search for a preceding plural noun. The Key vector for "judges" would advertise itself as being a plural noun. This would lead to a high dot-product score between Q_they and K_judges, resulting in a high attention weight. The model would then heavily incorporate the Value vector of "judges" when creating the contextualized representation of "they".


4. The Full Picture: Transformer Blocks and Prediction

The Multi-Head Attention mechanism is the heart of a Transformer Block. This block also contains two other key components that we've seen before:

  • A Feed-Forward Network (FFN): A standard two-layer fully-connected network that is applied independently to each token's representation after attention. Its role is to add further processing and representational capacity.
  • Residual Connections and Layer Normalization: These are crucial for enabling stable training of very deep networks (e.g., GPT-3 has 96 stacked Transformer blocks). The residual connection adds the input of a layer to its output, helping to prevent the "vanishing gradient" problem.

To generate text, the sequence of input tokens is processed through the entire stack of Transformer blocks. Finally, the output embedding of the very last token is taken and passed through a final linear layer (often called the "unembedding" layer) and a softmax function. This produces a probability distribution over the entire vocabulary for what the next token should be. The model then samples from this distribution to pick the next word, appends it to the sequence, and repeats the entire process.

For a comprehensive, formal text-based summary of everything we've covered—from embeddings to the full transformer block and the final language modeling head—the following resource is an excellent reference.

Chapter 8: Transformers

The 'Speech and Language Processing' textbook by Jurafsky and Martin is a standard academic text in NLP. This chapter provides a rigorous, step-by-step written explanation of the Transformer architecture.

Skim through these sections to consolidate your understanding. Pay attention to how the text formalizes the concepts we've seen visually: Section 8.1: Explains the attention mechanism, formalizing the Q, K, V computations and Multi-Head Attention. Section 8.2: Describes the full Transformer Block, including the FFN and layer norm. Section 8.4: Details the input, combining token and positional embeddings. Section 8.5: Covers the final Language Modeling Head for prediction.

Finally, to see all these pieces work together in a dynamic way, I highly recommend exploring this interactive tool.

LLM Transformer Model Visually Explained

This interactive 'Transformer Explainer' allows you to input text and visualize the entire process step-by-step, from embeddings to attention scores and final probabilities. It is an excellent tool for reinforcing your understanding.

Spend about 10 minutes exploring this resource. Type in a sentence and click through the different stages. Hover over the attention maps to see which words are attending to which others. This provides a tangible feel for the abstract concepts we've discussed.


Conclusion

In this lesson, we have journeyed into the core of modern natural language processing. We've moved beyond the static world of simple word vectors into the dynamic, context-aware realm of the Transformer.

Key Takeaways:

  • Word embeddings are dense vector representations of words that capture semantic relationships, with similar words being closer in the vector space.
  • The Transformer architecture was designed to create contextualized word representations by processing an entire sequence at once.
  • Its core innovation is the self-attention mechanism, which allows the model to weigh the importance of other words in the sequence when processing a given word.
  • This mechanism is powered by learnable Query, Key, and Value projections for each word.
  • Multi-Head Attention allows the model to learn different kinds of linguistic relationships in parallel.
  • A full Transformer consists of stacked Transformer Blocks (Attention + FFN), culminating in a prediction head that generates a probability distribution for the next word.

Preview of the Next Lesson:

We have now explained the 'T' in GPT (Generative Pre-trained Transformer). But what about the "Large" in Large Language Models? In our next lesson, we will describe the significance of the Transformer model for the development of LLMs. We will explore how the parallelizable nature of this architecture unlocked unprecedented scale, leading to the powerful and sometimes unpredictable models that are at the center of AI governance debates today.