Lesson 2: Sparse and Dense Vectors / Learn

Chapters:

0:00 - Intro

1:02 - Sparse Vectors

6:08 - Dense Vectors

9:11 - Recap

Video Transcript (English)

So far we know that vectors are a numeric representation of data, like text, images, or audio.

But did you know that there are two types of vectors? Sparse and dense.

Maybe you’re thinking: Why does this matter? Fair question. Turns out sparse is better for text or lexical search while dense is more suitable for semantic search.

In this lesson, we’ll learn about sparse and dense vectors and where they're used in MongoDB products.

Sparse and dense vectors are both numeric representations of data, but they're calculated differently and have separate uses:

Sparse vectors are used for text searches like we perform in Atlas Search, while dense vectors are used for semantic searches in Atlas Vector Search.

But before we get started we need to redefine the word document.

In this context, a document refers to a discrete piece of data such as a PDF or a phrase within a large dataset or corpus; for example: a stack of PDFs.

We're not referring to a MongoDB document.

Alright, let’s focus on sparse vectors first.

Sparse vectors only hold a small amount of data.

Most of the dimensions of a sparse vector are zeros, which, when compared to other sparse vectors, usually represent the absence or presence of a similarity.

Let's look at an example of how a sparse vector is calculated to understand it better.

You'll never have to calculate this - the computer will do it for you when you perform a text search, but knowing how this works will help you understand what each dimension represents and how sparse differs from dense.

OK. To calculate a sparse vector, we'll use a statistical measure known as Term Frequency-Inverse Document Frequency or TF dash IDF.

This formula determines the frequency at which a word is used within a document. And how unique the word is across a large corpus of documents.

There are many different ways to create sparse vectors. In fact, Atlas Search uses BM25 which is similar to TF dash IDF but with a few extra steps involved. For the sake of simplicity, we’ll use TF dash IDF to learn about sparse vectors.

Let’s go through the TF dash IDF equation, then apply it to an example.

We'll start with TF, term frequency.

We determine a word's frequency by dividing the number of times the word appears, by the number of words present in the document.

Then, we determine the uniqueness of the word, also known as the Inverse Document Frequency or IDF, by figuring out how often the word appears in the entire corpus.

Those two values multiplied together become the TF dash IDF value.

Now let’s check out a quick example to see how this works.

Let’s say our corpus consists of the following three phrases: Atlas the platform, Atlas the titan, and Atlas the mountain.

We want to calculate the sparse vectors for the first phrase “Atlas the platform”.

We’ll use a chart to help us with this calculation with columns for the word, number of occurrences, total number of words, and the calculated term frequency.

The first word in the phrase is Atlas.

Atlas only occurs once in the phrase “Atlas the platform”.

And the phrase has three words.

Dividing 1 by 3 we get 0.33 repeating.

The next two words in the phrase have identical stats.

Keep 0.33 repeating in mind because we’ll need it in a moment.

Now let’s calculate the inverse document frequency or IDF.

Like before, we’ll use a chart with columns for the word, total number of documents in the corpus, number of documents that contain the word, and the IDF calculation.

We’ll start with the first two words, “Atlas the”, because they should have the same score.

We have a total of three phrases, or documents, in our corpus. Atlas the platform, Atlas the titan, and Atlas the mountain.

And we know “Atlas" and "the” appear in all three phrases.

Now recall that IDF is the log of total document count divided by the number of documents that contain the word.

So in this case, it is the log of 3 divided by 3 or log of 1; which, as you may recall from your school math days, is zero.

Both "Atlas" and "the" have an IDF of zero.

Let’s move on to “platform.” Across the three phrases, it only appears once.

And since the total document count doesn't change, we can calculate its IDF by solving log of three, which is 0.4771 rounded to 4 significant digits.

Now that we've calculated both the TF and IDF, we multiply them together to compute a dimension or attribute value in our new sparse vector.

Doing this for each word in the phrase “Atlas the platform” gives us a complete sparse vector for it.

For "Atlas", we determined that the TF was 0.33 repeating and the IDF was 0. When we multiply these together we get 0 for our first dimension.

Next, we have “the” which has the same scores as “Atlas”.

Finally, we have "platform". We determined the TF was 0.33 repeating and the IDF was 0.4771. When we multiply these together we get 0.159 for our last dimension.

This means the sparse vector for “Atlas the platform” is 0, 0, and 0.159. This makes sense because sparse vectors are primarily made up of dimensions that are zero.

And zero for TF dash IDF means the word is either not present in the document or is in all of them.

In this case it represents the presence of the same words across each phrase.

That was a lot of work but thankfully we have machines to calculate this for us.

Would the machines purposely misrepresent the calculations to lead us down the wrong path? Nah, never.

As you can see, sparse vectors are great for determining the presence of something in a document which makes them ideal for text search.

But what if text search isn’t enough and you need to know the context or meaning?

Enter the dense vector.

As the name suggests, dense vectors hold significantly more information than sparse vectors.

The added information is used to capture complex relationships, which is why dense vectors have many use cases, like natural language and image processing, with Atlas Vector Search.

The dimensions of a dense vector are rarely zero. For example, you'll see dense vectors with thousands of dimensions and very few, if any, will be zero.

With sparse vectors, we could use TF dash IDF to calculate each dimension but with dense vectors we need to use a neural network known as a transformer model.

The details of the transformer model are beyond the scope of this lesson but suffice it to say that it was a leap forward in deep learning.

You may have heard of a little known technology from OpenAI known as GPT? Unsurprisingly this stands for "Generative Pretrained Transformer."

For more information on transformers check out the paper that started it all: "Attention Is All You Need" by Vaswani et al.

The type of transformer model that generates dense vectors is called an embedding model.

Although we are not deep diving into the model details, we can still look at a high level example to help us get a sense of what constitutes a dense vector.

Say we have two phrases: “Atlas is a powerful developer data platform” and “Atlas is a titan from Greek mythology and serves as a symbol of endurance."

Both of these mention Atlas but one is referring to the MongoDB product while the other is referring to the titan from Greek mythology.

If we input these phrases into the embedding model it would generate numbers for these specific attributes to represent the different meanings.

For instance, in one sentence, it would generate numbers that represent Atlas as a product, used by developers, involving databases, along with a bunch of other related attributes.

The other sentence would generate numbers indicating that Atlas is a titan, a character from Greek mythology, a symbol of endurance, and the list could go on.

As we'll discuss in the next lesson, the embedding model is trained on a large corpus of data, and that training allows it to interpret the meaning and assign numerical values, generating a dense vector.

So the model identifies both meanings of the word Atlas and represents them as floating point values. These values make up the dimensions in the dense vector.

While both meanings of atlas will be close to each other in a highly dimensional, dense vector space, each will be surrounded by very different clusters.

MongoDB Atlas will be close to data about databases and tools developers use. While Atlas from Greek mythology will be surrounded by data about other figures and stories in Greek mythology.

The highly dimensional space of a dense vector allows us to use computers to identify the meaning of complex, highly contextualized data.

Now that we have learned about both sparse and dense vectors, let’s recap.

Sparse vectors are used for text searches like Atlas Search. They hold less information and their embedding arrays are primarily made up of zeros, indicating whether a similarity is present or not.

On the other hand, dense vectors hold significantly more information. They're used to capture complex relationships which is why we use them for Atlas Vector Search.

Next, we’ll show you how to generate dense vectors with an embedding model.