Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding Text Embeddings

Introduction

Hello! Welcome to the fourth module of our course, "Embeddings and Vector Databases." In the previous modules, we explored the architecture of models like transformers. Now, we're going to dive into a concept that is fundamental to how these models truly understand and work with language: text embeddings.

This lesson will focus on explaining what text embeddings are and how they manage to capture the rich, nuanced meaning of text in a numerical format. Understanding this is crucial for nearly every advanced LLM application you're interested in, including RAG and AI agents, as embeddings are the mechanism that allows us to compare and retrieve information based on semantic meaning rather than just keywords.

Why Do We Need a Better Way to Represent Text?

Before we can appreciate what embeddings do, we need to understand the problem they solve. Computers operate on numbers, not words. So, how do we convert text into a numerical format that a machine learning model can process effectively?

Early approaches had significant limitations. To see why methods like simple token IDs, one-hot encoding, or bag-of-words are insufficient for capturing meaning, please watch the following segment of this video.

What Are Word Embeddings?

This video from Under The Hood will explain why simpler numerical representations of text fall short, setting the stage for why we need embeddings.

Watch the section from 00:56 to 04:50. Pay close attention to the drawbacks of each method mentioned: token numbers, one-hot encoding, and bag-of-words (n-grams).

As the video explains, these methods suffer from major drawbacks:

  • Simple token IDs create false numerical relationships (e.g., the number for "great" might be closer to "bad" than "good").
  • One-hot encoding creates extremely large and sparse vectors (mostly zeros), which are inefficient. More importantly, it treats every word as completely independent, capturing no relationship between, say, "cat" and "kitten."
  • Bag-of-words and n-grams start to capture some context but are still very high-dimensional and fail to grasp deeper semantic relationships.

The core problem is that none of these methods effectively represent meaning. We need a way to create numerical representations that are both efficient (dense, not sparse) and semantically rich.

Introducing Text Embeddings

This is where text embeddings come in. An embedding is a dense vector (an array of numbers) that represents a piece of text in a multi-dimensional space. The key idea is that the position and direction of this vector in the "embedding space" represent the text's semantic meaning.

For a comprehensive and intuitive overview, let's turn to an excellent article from the Stack Overflow Blog. It's geared toward a technical audience and uses helpful analogies.

An intuitive introduction to text embeddings

This article, 'An intuitive introduction to text embeddings,' provides a fantastic conceptual foundation for what embeddings are and why they are so powerful.

Please read the sections titled 'What’s an embedding?' and 'Distance.' Focus on the core definition of an embedding as a projection into a high-dimensional space and the idea that distance in this space measures similarity.

To summarize the key points from the reading:

  • An embedding is a vector of numbers (e.g., 1536 numbers for OpenAI's text-embedding-ada-002 model).
  • This vector acts as a set of coordinates, placing the text in a high-dimensional "latent space."
  • The defining principle of this space is that semantically similar texts are located closer to each other.

This property is what makes embeddings so useful for tasks like search, classification, and clustering.

How Embeddings Capture Meaning

Now for the crucial question: how do these vectors actually capture meaning? The magic lies in how the embedding space is structured during the model's training process. This structure allows us to reason about meaning using mathematical operations on the vectors.

The Distributional Hypothesis

The guiding principle behind training embeddings is the distributional hypothesis: words that appear in similar contexts tend to have similar meanings. For example, in a massive dataset of text, the words "dog" and "puppy" will often be surrounded by similar words like "walk," "fetch," "loyal," and "bark." An embedding model learns these associations and places the vectors for "dog" and "puppy" very close together in the embedding space.

The following image provides a great visual for this concept.

Text Embeddings Visualization
This image illustrates how an embedding space might be organized. Notice how related items, like different types of flowers or celestial bodies, are clustered together. Proximity in the space signifies semantic similarity.

Vector Arithmetic

This spatial organization isn't just about clustering; it also captures relationships. The most famous example of this is the "King - Man + Woman ≈ Queen" analogy.

Let's watch two short video clips that illustrate this concept beautifully.

What Are Word Embeddings?

First, let's return to the 'Under The Hood' video to see a visual demonstration of how vector math can capture semantic relationships.

Watch from 06:09 to 09:07. This part explains how embeddings are dense vectors and demonstrates the 'King/Queen' and 'capital city' analogies.

What are Word Embeddings?

Now, let's watch a clip from IBM Technology that reinforces this concept and introduces an important distinction between different types of embeddings.

Watch from 02:17 to 03:54 and then from 07:19 to 08:28. The first part provides another look at the vector space concept. The second part introduces 'contextual embeddings,' which is how modern transformers work—the embedding for a word like 'bank' changes depending on whether you're talking about a river bank or a financial bank.

The ability to perform this kind of "concept algebra" is profound. It means the model hasn't just memorized definitions; it has learned a structured representation of the world as described in the training data.

Word Embeddings and Semantic Relationships
This diagram shows the classic example of vector arithmetic. The relationship (vector) between "man" and "woman" is very similar to the vector between "king" and "queen". By subtracting the "man-ness" from "king" and adding "woman-ness," we arrive at a point in the embedding space very close to "queen."

Thought Experiment

Let's put this into practice. Imagine an embedding space where we have plotted the following sentences. Think about their relative positions.

  1. "The developer wrote an efficient algorithm."
  2. "The engineer coded a fast program."
  3. "The manager scheduled a team meeting."
  4. "The chef prepared a delicious pasta."

How would they be arranged?

  • Sentences 1 and 2 would be extremely close to each other, as they are semantically almost identical.
  • Sentence 3 would be relatively close to 1 and 2, as it exists in the same professional/office context, but it would be distinctly further away than 1 and 2 are from each other.
  • Sentence 4 would be very far away from the other three, as it belongs to a completely different semantic domain (cooking vs. software development).

This ability to measure semantic distance is the foundation of semantic search, which we will explore in the upcoming lessons.

Conclusion

In this lesson, we've established the foundational concepts of text embeddings. Let's summarize the key takeaways:

  • What they are: Text embeddings are dense numerical vectors that represent text (words, sentences, or documents) as points in a high-dimensional space.
  • How they capture meaning: They are trained on vast amounts of text, and the model learns to place semantically similar pieces of text close together in the embedding space. This structure allows for meaningful comparisons and even arithmetic operations that reflect real-world relationships (e.g., king - man + woman ≈ queen).
  • Why they are important: They solve the problem of representing text for computers in a way that is both computationally efficient and semantically rich, moving far beyond simple keyword matching.

In our next lesson, we will get practical. You will learn how to generate text embeddings using a dedicated model API and then compute the cosine similarity between them to quantitatively measure how related two pieces of text are.

Can't find a good explanation? Sign up and we'll make it for you

Sign up