Cosine similarity is a powerful tool when working with LLM embeddings (embeddings generated by Large Language Models like GPT, BERT, etc.), as it allows you to measure the semantic similarity between text snippets, sentences, or documents based on their vector representations. Here's how it works in this context:
1. LLM Embeddings
LLMs map text (words, sentences, or documents) into high-dimensional vector spaces, where each vector (embedding) captures the semantic meaning of the input text. These embeddings are typically dense vectors (e.g., 768 dimensions for BERT, 512 dimensions for DistilBERT, etc.) and are learned during the model's pre-training phase.
Key properties of LLM embeddings:
- Semantically similar texts are mapped to nearby points in the vector space.
- The direction (orientation) of the vector encodes the meaning, while the magnitude may vary.
2. Role of Cosine Similarity
Cosine similarity is used to quantify how similar two embeddings are in terms of their orientation (direction), ignoring their magnitude (length). This is particularly useful for text embeddings because:
- The magnitude of an embedding can be influenced by factors like text length or frequency of words, which may not reflect semantic similarity.
- The orientation of the embedding captures the underlying meaning, making it a better measure of similarity.
3. How Cosine Similarity Works with Embeddings
Given two embeddings vectors, ( \mathbf{A} ) and ( \mathbf{B} ), cosine similarity computes the cosine of the angle between them. Here’s the process:
-
Obtain Embeddings:
Use an LLM to generate embeddings for the text snippets you want to compare. For example:
- Let ( \mathbf{A} ) be the embedding for the sentence "I love eating pizza."
- Let ( \mathbf{B} ) be the embedding for the sentence "Pizza is my favorite food."
-
Compute Cosine Similarity:
Use the formula:
[
\text{cosine similarity}(\mathbf{A}, \mathbf{B}) = \frac{\mathbf{A} \cdot \mathbf{B}}{|\mathbf{A}| |\mathbf{B}|}
]
- ( \mathbf{A} \cdot \mathbf{B} ) is the dot product of the embeddings.
- ( |\mathbf{A}| ) and ( |\mathbf{B}| ) are the magnitudes (L2 norms) of the embeddings.
-
Interpret the Result:
The cosine similarity score ranges from -1 to 1:
- A score close to 1 indicates high semantic similarity (e.g., "I love pizza" and "Pizza is great").
- A score close to 0 indicates neutrality or orthogonality (e.g., "I love pizza" and "The weather is sunny").
- A score close to -1 indicates opposite meanings (rare in practice for text embeddings).
4. Why Cosine Similarity is Preferred for Embeddings
- Magnitude Invariance: Embeddings may have different magnitudes due to factors unrelated to semantics (e.g., text length). Cosine similarity focuses on the angle between vectors, which is a better indicator of semantic similarity.
- Efficiency: Computing cosine similarity is efficient, even for high-dimensional vectors, as it involves simple dot products and norms.
- Interpretability: The score is easy to interpret, with 1 indicating identical meaning and 0 indicating no relationship.
5. Applications in LLMs
Cosine similarity is widely used with LLM embeddings for tasks such as:
- Semantic Search: Finding the most relevant documents or sentences for a query.
- Clustering: Grouping similar texts together.
- Recommendation Systems: Suggesting similar items based on text descriptions.
- Plagiarism Detection: Identifying similar passages of text.
- Chatbots and QA Systems: Matching user queries to pre-defined responses or knowledge base entries.
Example
Suppose you have two embeddings:
- ( \mathbf{A} = [0.2, 0.5, 0.3, 0.1] ) (embedding for "I enjoy reading books.")
- ( \mathbf{B} = [0.3, 0.6, 0.4, 0.2] ) (embedding for "Reading novels is my hobby.")
- Dot product: ( \mathbf{A} \cdot \mathbf{B} = 0.20.3 + 0.50.6 + 0.30.4 + 0.10.2 = 0.5 )
- Magnitudes: ( |\mathbf{A}| = \sqrt{0.2^2 + 0.5^2 + 0.3^2 + 0.1^2} = \sqrt{0.39} ), ( |\mathbf{B}| = \sqrt{0.3^2 + 0.6^2 + 0.4^2 + 0.2^2} = \sqrt{0.65} )
- Cosine similarity: ( \frac{0.5}{\sqrt{0.39} * \sqrt{0.65}} \approx 0.97 )
The high score (0.97) indicates that the two sentences are semantically very similar.
6. Limitations
- Cosine similarity assumes that the embeddings are normalized or that magnitude does not carry meaningful information.
- It may not capture complex semantic relationships that require deeper contextual understanding.
In summary, cosine similarity is a simple yet effective way to measure the semantic similarity between text embeddings generated by LLMs, making it a cornerstone in many NLP applications.