Articles
LLM (Similarity Functions) (7)
This guide covers similarity measures and contrastive learning for LLMs, including embedding spaces, similarity functions, contrastive loss…
Similarity Measures and Contrastive Learning
A learning-focused resource for understanding how models learn “what is similar” and why that idea generalizes so broadly
Why similarity is a core concept
Before talking about contrastive learning, it’s worth stepping back. A large fraction of modern machine learning problems - retrieval, recommendation, representation learning, clustering, metric learning - boil down to a single question:
How similar are two things, according to the model?
Similarity is not a single formula. It is a design choice that encodes assumptions about geometry, scale, and meaning.
1. Beyond cosine similarity: other similarity and distance measures
Cosine similarity is popular because it ignores vector magnitude and focuses purely on direction. That makes it ideal for embeddings where scale is arbitrary, such as text or image representations. But it is far from the only option.
Euclidean distance measures straight-line distance in vector space. It assumes absolute position and scale matter, which makes it more suitable for continuous features with physical meaning. In embedding spaces, it often behaves similarly to cosine distance once vectors are normalized.
Manhattan distance sums absolute coordinate differences. It is more robust to outliers and appears frequently in sparse or grid-like feature spaces.
Jaccard similarity compares overlap between sets rather than vectors. It is useful when features represent presence or absence - keywords, tags, neighbors in a graph - rather than dense numeric values.
Pearson correlation measures linear correlation after centering. Unlike cosine similarity, it removes both magnitude and mean, which can be useful when relative variation matters more than absolute levels.
The important idea is not memorizing formulas, but recognizing that each similarity measure implies a different notion of “closeness.”
2. What is contrastive learning?
Contrastive learning is a representation learning paradigm that does not rely on explicit labels. Instead, it learns by comparing samples.
The objective is simple in spirit:
- representations of similar samples should be close
- representations of dissimilar samples should be far apart
What makes it powerful is how “similar” and “dissimilar” are defined. In images, similarity might come from different augmentations of the same photo. In text, it might come from neighboring sentences or translations. In graphs, it might come from structural proximity.
Architecturally, contrastive learning often appears as:
- Siamese networks, where two encoders share weights
- Triplet setups with anchor, positive, and negative samples
But these are implementation details. Conceptually, contrastive learning is about learning a geometry where meaning corresponds to distance.
3. Why contrastive learning generalizes so well
Contrastive objectives do not force the model to predict a specific label. Instead, they force it to organize representations in a way that preserves semantic relationships.
This is why contrastive learning shows up in:
- self-supervised vision models
- sentence embedding models
- speech and audio representation learning
- recommendation and retrieval systems
Once learned, these representations transfer well to downstream tasks because they encode structure, not task-specific rules.
4. Are negative samples important?
Negative samples are what give contrastive learning its discriminative power. Without them, the model would only learn to collapse everything together.
Negatives define what something is not, which sharpens decision boundaries and improves generalization. However, their importance is task-dependent. Some modern methods reduce or even remove explicit negatives by relying on architectural or batch-level signals, but the underlying pressure to separate representations still exists.
The real challenge is not whether negatives matter - it’s how to obtain them efficiently.
5. Reducing the cost of negative sample construction
In many domains, constructing negatives is expensive. For example, deciding what is truly “not similar” can require domain knowledge or heavy computation.
Several strategies are commonly used.
One approach is to reduce construction cost by sampling negatives more intelligently. Data augmentation can generate synthetic negatives, while approximate nearest-neighbor search can identify candidates that are close enough to be informative without being positives.
Another approach is to focus on key negatives. Not all negatives are equally useful. “Hard negatives” - samples that are close to positives but still incorrect - carry much more learning signal than random ones. Prioritizing these avoids wasting compute on trivial negatives.
A third strategy relies on transfer learning and pretraining. Instead of rebuilding negative sets from scratch, pretrained representations can already encode much of the separation needed. This reduces redundancy and lowers the cost of contrastive training in new domains.
6. Where these ideas extend
Understanding similarity measures and contrastive learning pays off far beyond self-supervised learning.
The same ideas underpin:
- metric learning for face recognition
- retrieval-augmented generation
- clustering and topic modeling
- recommender systems
- representation alignment across modalities
Whenever a system ranks, retrieves, clusters, or matches, it is implicitly relying on a learned similarity space.
Final takeaway
Contrastive learning is not about a specific loss function or network architecture. It is about shaping representation space so that distance aligns with meaning.
Once you understand similarity as a modeling choice - and negatives as a tool rather than a requirement - you can apply these ideas almost anywhere.
How to Use This Page as a Prep Plan
Do not treat this as passive reading. Convert the ideas in this page into a short weekly loop: learn one idea, practice it under interview conditions, then write down what changed. That is the fastest way to turn advice into visible interview behavior.
| Prep area | What you need to prove | Practice artifact |
|---|---|---|
| Understand | Turn the prompt into a concrete goal. | Clarifying questions and success criteria. |
| Practice | Use realistic constraints and timed reps. | Worked examples with edge cases. |
| Explain | Make reasoning visible. | Tradeoffs, assumptions, and test strategy. |
| Improve | Review misses quickly. | A short feedback log and next action. |
For LLM (Similarity Functions) (7), the strongest candidates usually do three things well: they make their assumptions explicit, they use concrete examples instead of vague claims, and they review mistakes quickly enough that the next practice rep is better than the last one.
Video Walkthrough
This verified YouTube video gives a second pass on the same preparation area. Use it after reading the guide, then come back and turn the advice into a practice artifact.
Video: Transformers, the tech behind LLMs | Deep Learning Chapter 5
FAQ
How should I use this guide?
Read it once for the structure, then turn each section into a practice task with a visible artifact.
What should I do if I am short on time?
Prioritize the skills most likely to be tested, then do one mock or timed drill to expose the largest gap.
How do I know I am ready?
You can explain your approach clearly, recover from hints, and name tradeoffs without relying on memorized wording.