What Is Similarity Between Sentences
Similarity Between Words and Sentences
Sentence embeddings are the bread and butter of language models, as they associate each sentence with a particular list of numbers (a vector), in a way that similar sentences give similar vectors. We can think of embeddings as a way to locate each sentence in space (a high dimensional space, but a space nonetheless), in a way that similar sentences are located close by. Once we have each sentence somewhere in space, it’s natural to think of distances between them. And an even more intuitive way to think of distances is to think of similarities, i.e., a score assigned to each pair of sentences, which is high when these sentences are similar, and low when they are different. The similarity is a very useful concept in large language models, as it can be used for search, for translation, for summarization, and in many other applications.
In this notebook, we understand the intuition behind similarities between sentences, including dot product and cosine similarity.
Read the accompanying blog post here.
Overview
We'll do the following steps:
- Step 1: Turn Text into Embeddings - Use Cohere's Embed endpoint to get sentence embeddings.
- Step 2: Calculate Dot Products - Calculate the dot products between each pair of sentence embeddings to understand similarity between them.
- Step 3: Calculate Cosine Similarities - Use scikit-learn to get the cosine similarity for each pair of sentence embeddings.
Setup
We'll start by installing the tools we'll need and then importing them.
Fill in your Cohere API key in the next cell. To do this, begin by signing up to Cohere (for free!) if you haven't yet. Then get your API key here.
Step 1: Turn Text into Embeddings
In this notebook, we'll work with three sentences and store them in a Python list texts.
To get the corresponding sentence embeddings, we call the Embed endpoint with co.embed(). We supply three parameters:
texts- our list of sentencesmodel- we useembed-v4.0input_type- we usesearch_documentto indicate that we intend to use the embeddings for search use-cases
You'll learn about these parameters in more detail in the LLMU Module on Text Representation.
The embeddings are stored in the embeddings value of the response. After getting the embeddings, we separate them by sentence.
Embedding for sentence 1 [ 0.05075073 0.03753662 -0.02958679 ... -0.07366943 -0.00842285 -0.01834106] Embedding for sentence 2 [ 0.04333496 0.05407715 -0.02453613 ... -0.06182861 -0.01983643 -0.0096283 ] Embedding for sentence 3 [ 0.02383423 0.00695801 -0.04669189 ... -0.04006958 -0.02304077 0.01963806]
Note that the embeddings are vectors (lists) of 1024 numbers, so they are truncated here (thus the dots in between). One would expect that the vectors corresponding to sentences 1 and 2 are similar to each other and that both are different from the vector corresponding to sentence 3. However, from inspection, this is not very clear. We need to calculate some similarities to see if this is the case. We will do that in the following two sections.
Step 2: Calculate Dot Products
Let’s calculate the dot products between the three sentences to understand how similar they are to each other.
Similarity between sentences 1 and 2: 0.8187172323456494 Similarity between sentences 1 and 3: 0.1952170633623922 Similarity between sentences 2 and 3: 0.1982665457613819
The similarity between sentences 1 and 2 (0.8188) is much larger than the similarities between the other pairs. This confirms our predictions.
Just for consistency, let’s calculate the similarities between each sentence and itself, to confirm that a sentence and itself has the highest similarity score.
Similarity between sentences 1 and 1: 1.0001818411403132 Similarity between sentences 2 and 2: 0.9997857473557472 Similarity between sentences 3 and 3: 0.9998230785105999
This checks out—the similarity between a sentence and itself is around 1, which is higher than all the other similarities.
Step 3: Calculate Cosine Similarities
We use the cosine_similarity() function from scikit-learn to measure cosine similarity between the three sentences.
Cosine similarity between sentences 1 and 2: 0.8187305165459495 Cosine similarity between sentences 1 and 3: 0.19521658630446206 Cosine similarity between sentences 2 and 3: 0.19830533175410783
Now let’s check the similarity between each sentence and itself.
Cosine similarity between sentences 1 and 1: 1.0000000000000004 Cosine similarity between sentences 2 and 2: 0.9999999999999996 Cosine similarity between sentences 3 and 3: 0.9999999999999996
We also plot the results in a grid.
[Text(0.5, 0, 'I like to be in my house'), , Text(1.5, 0, 'I enjoy staying home'), , Text(2.5, 0, 'the isotope 238u decays to 206pb')]
Notice that the dot product and cosine distance give nearly identical values. The reason for this is that the embedding is normalized (meaning each vector has norm equal to 1). When the embedding is not normalized, the dot product and cosine distance would give different values.