VIEW THIS AS

Auto mode follows the Route Engine until you choose a viewpoint.

YOU ARE HERE

ROUTE CHECK

CONNECTED TO

WHAT NEXT

Use the canonical route for this room, or HELP if you are unsure.

How Artificial Intelligence AI Works | How AI Learns by Comparing Similar and Different Examples

Did you know? “How do plants make food?” and “Explain photosynthesis” use different words but ask about the same idea. A search engine that only looks for identical text may overlook the connection. Some AI models learn to bring related examples closer together by comparing pairs.

This is called contrastive learning. Rather than only predicting a single category, a model can be trained to distinguish examples that belong together from examples that should be less similar for the task.

Quick answer: choose an anchor → identify a relevant match → choose appropriate contrasts → encode them as vectors → update the model so useful matches rank nearer → test on unseen pairs. The technique is used in several kinds of text, image and multimodal representation learning.

The 30-second guide

  • Anchor: the example whose relationships we are learning.
  • Positive: another example considered a relevant match to the anchor under the task definition.
  • Negative: an example that should be less similar for that particular comparison.
  • Embedding: a numerical vector learned to represent an item.
  • Contrastive loss: a training objective that rewards useful relative similarities or margins.
  • Hard negative: a difficult contrast that looks related but is not the intended match.

The positive and negative labels are about the training objective. A “negative” sentence is not necessarily bad, offensive or factually false. It simply is not the selected match in that learning example.

A two-minute example with school questions

Imagine an AI-powered library of revision notes. A student searches for “Why does ice float?” The library contains a chapter titled “Density of ice and liquid water” and another titled “How to make ice cubes in a freezer.” Both mention ice, but the density explanation answers the scientific question more directly.

The search question can be an anchor. The density chapter is a suitable positive. The freezer-instructions article can be a hard negative for this specific information need because it is topically close yet fails to answer the question.

A learning objective can encourage the question’s vector to be more similar to the helpful density passage than to the unrelated or less-relevant passage. That is more useful for retrieval than merely counting how many words overlap.

Step 1: Decide what counts as similar

Similarity is not an absolute property with one meaning for every AI system. For a paraphrase search task, two sentences may be positive if they convey the same idea. For a product finder, positive pairs might link a customer request to a suitable product description.

For image learning, two different views or suitable augmentations of the same underlying image can act as related examples under a chosen objective. For image-and-text alignment, an image and its matching description may be positive.

The definition of similarity must serve the real task. A model designed for matching chemical terms should not be evaluated only on casual conversational synonyms.

Step 2: Encode each example into a vector

A text encoder converts a question or passage into numerical features. An image encoder does the same for visual input. The aim is to create a representation where a mathematical similarity measure helps find relevant relationships.

For some training approaches, developers use cosine similarity, which compares vector directions. Other methods use dot products or Euclidean distances with suitable scaling and normalisation.

The individual vector coordinates do not necessarily correspond to simple English labels. The training process shapes the overall geometry of the representation space.

Step 3: Choose useful positive and negative pairs

A training dataset might contain question–answer-passage pairs. For each question, the relevant passage is positive; clearly unrelated passages are potential negatives. A system can also learn from labelled similarity scores or triplets.

Some contrastive objectives use examples from the same training batch as negatives for one another. That can save the cost of manually selecting every negative, but it introduces a risk: two different questions may genuinely match the same answer.

If a true match is accidentally treated as negative, the training objective may push related examples apart. Developers call this a false negative. Dataset design and batch selection need to account for such cases.

Step 4: Give the model a ranking objective

Imagine a deliberately simplified similarity score from zero to one. A model compares a question with its correct passage and produces 0.55, but gives an irrelevant passage 0.70. That ordering is undesirable for our retrieval task.

A contrastive training objective can supply gradient information encouraging the relevant passage to score higher relative to the contrast. After suitable updates, the model might give the helpful passage 0.83 and the unsuitable passage 0.28. These values are invented for illustration, not recorded model results or guaranteed probabilities.

The aim is not to memorise one particular pair. It is to create representations that help rank useful material for new questions and documents.

The best comparison is not always “same topic versus different topic.” Sometimes it is “actually answers this question versus merely sounds related.”

Step 5: Use hard negatives carefully

Easy contrasts help introduce a concept: a photosynthesis question is clearly less related to a recipe for chocolate cake. But a real search engine must often choose between several documents that all share important words.

A hard negative can make training more informative because the model has to learn a finer distinction. For the ice example, “How to freeze water quickly?” is far more challenging than an article about train timetables.

The danger is mislabelling. A passage that appears irrelevant at first glance may answer a related form of the question. Before making it a negative, check the actual source and the intended retrieval criterion.

Step 6: Learn from pairs without needing every category label

Contrastive methods can be used in supervised, self-supervised and related settings. Some rely on human-labelled pairs. Others construct positive examples automatically, for instance from alternative views of the same source image.

That flexibility helps models learn broad representations without requiring a conventional one-hot class label for every example. It does not remove the need for data quality, task definitions or rights to use the training material.

For an explanation of training targets created from data, read How AI Learns by Predicting Missing Words. The techniques share broad self-supervised ideas but use different objectives.

What about matching pictures with captions?

Contrastive learning can help align different modalities. Imagine a photograph of a red bicycle next to three text descriptions: “a red bicycle,” “a blue sailing boat,” and “a bowl of oranges.” A suitable model learns to rank the matching bicycle description more highly.

Some well-known vision-language systems learn from paired images and text. When successful, they can support tasks such as retrieving pictures using written descriptions. The result remains probabilistic and can fail on counts, unusual scenes and subtle relationships.

The model also needs to handle captions that are vague or wrong. A photograph and an incorrect caption should not become reliable ground truth simply because they appeared together in a dataset.

A search application can encode the user question and many document passages into embeddings, then compare them to find candidate matches. Contrastive training can make the vectors more useful for the specific notion of relevance.

However, the embedding search is only a candidate finder. A passage with high similarity might be outdated, incomplete or unsuitable for the user’s permissions. Retrieval needs a later evidence and authority check.

The companion guide How AI Finds Answers in Your Documents shows how suitable passages can be used to ground an answer.

How should a contrastively trained model be tested?

The final evaluation should use queries and relevant documents not merely memorised during training. For retrieval, a developer may measure whether a correct passage appears among the leading results, or how highly relevant results are ranked.

The dataset must distinguish near-duplicate documents and shared source material. Otherwise, apparent success may come from recognising an example already effectively present in training.

A reliable test should also include similar-looking but misleading passages. The system that reliably picks an exact topical answer over a convincing distractor is more useful than one that only separates unrelated subjects.

Try it: become a human contrastive retriever

Write four fictional article titles on cards: “Why Ice Floats,” “How to Make Ice Cubes,” “The Water Cycle,” and “How Clouds Form.” Now write the query “Why is solid ice less dense than liquid water?”

Choose the most relevant positive card and a hard negative that shares the word “ice” but answers a different question. Explain your choices in plain language.

Repeat with “How do clouds form?” Notice how the identity of the positive and negative changes with the query. The method is about relationships under a task, not labelling documents as universally good or bad.

Five mistakes to avoid

  • False negatives: pushing two genuinely related items apart due to mistaken labels.
  • Only easy examples: training on wildly unrelated pairs without testing subtle contrasts.
  • Biased coverage: missing languages, subject areas or writing styles.
  • Similarity treated as truth: a matching document can still be wrong or out of date.
  • Test contamination: near-duplicate passages make held-out results overly optimistic.

Frequently asked questions

Is contrastive learning the same as classification?

No. Classification predicts predefined labels, while contrastive objectives learn useful relationships among examples or their representations. Both can be combined.

What is a positive pair?

Two examples designated as relevant matches for the training task, such as a question and the passage that answers it.

Does a negative pair mean an example is factually false?

No. It is a contrast for the specific relationship being learned; it can be perfectly true but irrelevant.

Can contrastive learning work with images?

Yes. Image–image and image–text matching are important applications, subject to model design.

Does it guarantee better search results?

No. Better rankings depend on training quality, suitable negatives, independent evaluation and how retrieved sources are used.

Your one-minute takeaway

  • Contrastive learning teaches representations to rank relevant examples closer than unsuitable comparisons.
  • Hard negatives can improve discrimination but mistaken negatives can damage the model.
  • High similarity is a search clue, not proof of factual correctness.

Trusted resources and related guides

Discover more from eduKate Singapore

Subscribe now to keep reading and get access to the full archive.

Continue reading