Inferensys

Glossary

Contrastive Learning

A self-supervised training paradigm that learns representations by pulling positive mention-entity pairs together and pushing negative pairs apart in vector space.
Engineer reviewing vector database search results on laptop, embeddings visualization on screen, home office coding session.
SELF-SUPERVISED REPRESENTATION LEARNING

What is Contrastive Learning?

Contrastive learning is a self-supervised training paradigm that learns robust data representations by pulling semantically similar pairs together and pushing dissimilar pairs apart in a dense vector space.

Contrastive learning is a discriminative training approach where a model learns an embedding space by comparing sample pairs. The objective function explicitly minimizes the distance between positive pairs—such as a clinical mention and its correct UMLS concept—while maximizing the distance between negative pairs, like a mention and an incorrect entity. This is typically implemented using the InfoNCE loss or triplet loss, which computes similarity scores between anchor, positive, and negative samples to shape the representation manifold.

In clinical entity linking, contrastive learning is critical for disambiguation. Models like SapBERT are pre-trained by pulling synonymous biomedical concepts from the UMLS Metathesaurus together, creating a unified vector space where "MI" and "myocardial infarction" are proximal. The paradigm relies heavily on hard negative mining, where the most confusable incorrect candidates are selected during training to sharpen the decision boundary and prevent false grounding of ambiguous medical abbreviations.

REPRESENTATION LEARNING

Key Characteristics of Contrastive Learning

Contrastive learning is a self-supervised paradigm that learns effective representations by pulling semantically similar pairs (positive) together and pushing dissimilar pairs (negative) apart in embedding space.

01

Positive Pair Construction

The engine of contrastive learning relies on defining positive pairs—two views of the same underlying concept. In clinical entity linking, a positive pair consists of a mention (e.g., 'heart attack') and its canonical entity (e.g., UMLS C0027051: Myocardial Infarction). These pairs can be generated through:

  • Synonym substitution from ontologies like SNOMED CT
  • Dropout augmentation where the same input passes through a model twice
  • Contextual paraphrasing of the surrounding clinical text The model learns to map these distinct surface forms to identical vector coordinates.
Cosine Similarity → 1.0
Optimization Target
02

Negative Sampling Strategies

The discriminative power of contrastive learning comes from negative pairs—entities that the mention should not link to. Effective negative sampling is critical:

  • In-batch negatives: Other entities within the same training mini-batch serve as negatives, enabling efficient computation
  • Hard negative mining: Selecting highly confusable entities (e.g., 'myocardial infarction' vs. 'myocardial ischemia') that share semantic overlap with the mention
  • Random negatives: Uniformly sampled entities from the knowledge base to provide broad repulsive force Without hard negatives, the model fails to learn fine-grained disambiguation.
Cosine Similarity → 0.0
Optimization Target
03

InfoNCE Loss Function

The standard training objective is InfoNCE (Noise Contrastive Estimation), a categorical cross-entropy loss over similarity scores. For a given mention anchor m and a positive entity e+:

  • Compute cosine similarity between m and all candidates in the batch
  • Apply a temperature parameter (τ) to control the concentration of the distribution
  • Lower τ creates a sharper distribution, penalizing hard negatives more aggressively
  • The loss minimizes -log(exp(sim(m, e+)/τ) / Σ exp(sim(m, ei)/τ)) This formulation is a lower bound on mutual information between the mention and entity representations.
τ ∈ [0.05, 0.5]
Typical Temperature Range
04

Bi-Encoder Architecture

Contrastive learning in entity linking typically uses a dual-tower (bi-encoder) architecture:

  • Mention Encoder: A transformer (e.g., SapBERT, PubMedBERT) encodes the clinical mention and its surrounding context into a dense vector
  • Entity Encoder: A separate or shared transformer encodes the entity's canonical name, synonyms, and description from the knowledge base
  • Both towers project into a shared L2-normalized embedding space where inner product equals cosine similarity
  • At inference, entity embeddings are pre-computed and indexed using FAISS for fast approximate nearest neighbor search
< 10ms
Inference Latency per Mention
05

Supervised Contrastive Variant

Beyond self-supervised instance discrimination, supervised contrastive learning leverages label information to structure the embedding space more effectively:

  • Multiple mentions that link to the same UMLS concept are treated as positives for each other, not just their entity description
  • This pulls together diverse surface forms like 'MI', 'heart attack', and 'myocardial infarct' into a tight cluster
  • The loss function generalizes InfoNCE to handle multiple positives per anchor
  • This approach is particularly effective for high-variance clinical language where the same condition has dozens of synonymous expressions
+3-5%
Accuracy Gain over Self-Supervised
06

Momentum Contrast (MoCo)

Momentum Contrast addresses the challenge of maintaining a large and consistent dictionary of negative samples without massive batch sizes:

  • A momentum encoder (slowly updating copy of the main encoder) maintains a dynamic queue of recent entity embeddings
  • The queue acts as a large negative sample bank, decoupling batch size from negative set size
  • The momentum update rule θ_k ← m·θ_k + (1-m)·θ_q with a high momentum coefficient (e.g., 0.999) ensures consistency
  • This is critical for biomedical entity linking where the knowledge base may contain millions of concepts
65,536+
Typical Negative Queue Size
CONTRASTIVE LEARNING

Frequently Asked Questions

Explore the core mechanisms, training strategies, and architectural decisions behind contrastive learning for clinical entity linking. These answers target the precise technical queries of NLP scientists and bioinformatics engineers building high-precision medical grounding systems.

Contrastive learning is a self-supervised representation learning paradigm that trains models to pull positive mention-entity pairs together and push negative pairs apart in a shared dense vector space. For clinical entity linking, a bi-encoder architecture independently encodes a text mention (e.g., 'MI') and a candidate concept (e.g., UMLS C0027051: Myocardial Infarction) into embeddings. The training objective—typically InfoNCE loss—maximizes the cosine similarity between the mention and its true concept while minimizing similarity with all other in-batch negatives. This forces the model to learn semantic representations that cluster synonymous clinical terms (like 'heart attack' and 'myocardial infarction') near each other, while separating them from confusable entities such as 'mitral insufficiency'. The result is a model that can perform efficient approximate nearest neighbor search over millions of UMLS concepts without needing exhaustive pairwise scoring at inference time.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.