Multimodal Foundations › Cross-Modal Alignment
Language AI HandbookAll chapters Multimodal Foundations › Cross-Modal AlignmentOne section · Sections in this chapter5 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Learning a joint embedding space requires a training objective that enforces alignment between modalities. The dominant approach in modern multimodal learning is contrastive learning with a contrastive loss applied over batches of paired data.
Contrastive Learning Basics
Contrastive learning operates on matched pairs (positives) and unmatched pairs (negatives). Given a dataset of matched image-text pairs , the training objective rewards the model for placing matched pairs close together in embedding space while pushing unmatched pairs apart.
The key insight that makes contrastive learning efficient is the in-batch negatives strategy. Within a single training batch of pairs, there are positive pairs: the matched for each . There are also negative pairs: every combination of image with text where . A single batch of pairs thus generates training signal, making contrastive learning very data-efficient. A batch of 256 pairs gives 256 positives and 65,280 negatives. A batch of 32,768 pairs gives over a billion negatives.
This in-batch negative structure is efficient but introduces a subtle assumption: the negatives are truly unmatched. If the training data contains near-duplicate images or texts, some of the "negative" pairs within a batch may be semantically matched despite being labeled as negatives. This false negative problem degrades training quality and is an active area of research.
InfoNCE and Symmetric Cross-Entropy
The standard loss used in CLIP and most modern multimodal contrastive models is InfoNCE (Noise-Contrastive Estimation), applied symmetrically across both modalities. InfoNCE frames the problem as classification: given an image embedding , classify which of the text embeddings in the batch is the matched one.
The loss for the image-to-text direction treats each image as a query and the texts as classes:
This is a cross-entropy loss where the correct class is index (the matched text). The denominator sums over all texts in the batch, making the loss equivalent to classification over classes.
Symmetrically, the text-to-image loss treats each text as a query:
The full symmetric InfoNCE loss averages both directions:
where:
- is the L2-normalized visual embedding of image
- is the L2-normalized text embedding of text
- is a learnable temperature parameter controlling softmax sharpness
- is the batch size
- The inner sums run over all texts or images in the batch
The symmetry is important: the first term trains the visual encoder to produce embeddings that are discriminable from the text side, and the second term trains the text encoder to produce embeddings that are discriminable from the visual side. Neither encoder is treated as the "anchor"; both are jointly optimized.
The Role of Temperature
The temperature parameter controls the sharpness of the softmax distribution over negatives, and its effect on training is substantial.
When is large (close to 1 or above), the softmax distribution is nearly uniform: all pairs receive similar probability mass and the loss treats positives and negatives almost equally. The gradients are small and training is slow.
When is small (close to 0), the softmax distribution is very peaked: the pair with the highest similarity receives nearly all probability mass. This sharpens the learning signal but can also cause instability if the embeddings are poorly organized early in training, since the softmax saturates quickly and produces vanishing gradients.
In practice, is learned jointly with the encoders. CLIP initializes as a learnable scalar with initial value and clips it at to prevent instability. The model learns to set to a value that produces useful gradient signal given the current quality of the embeddings.
Intuitively, as training progresses and the embeddings improve, positive pairs become much more similar than negative pairs, and a smaller is needed to maintain strong learning signal. Early in training when embeddings are near-random, a larger prevents the loss from exploding by keeping the distribution less peaked. The ability to learn allows the training to self-regulate this tradeoff automatically.
Connection to Mutual Information
InfoNCE has a principled probabilistic interpretation: maximizing the InfoNCE objective is equivalent to maximizing a lower bound on the mutual information between the visual and textual representations. Specifically, if we denote the visual and textual representations as random variables and , then:
where is the mutual information between the representations and is the InfoNCE loss value. Minimizing the InfoNCE loss therefore drives the representations to share as much information as possible, creating the mutual information bottleneck that enforces semantic alignment. This connection motivates why contrastive objectives produce semantically rich representations: they are explicitly optimized to capture shared information between modalities.
Hard Negatives
A key challenge in contrastive training is that random negatives are often too easy to distinguish. If you pair a picture of a sunset with a caption about "a factory in winter," the model can trivially distinguish positive from negative based on superficial features without learning any fine-grained semantics. Easy negatives provide weak gradient signal because the loss is already near-minimal even with poor embeddings.
Hard negatives are pairs that are semantically close but factually mismatched. A picture of a golden retriever paired with a caption about a labrador retriever is a hard negative: both involve dogs, both have similar visual and textual features, but they are different pairs. Learning to distinguish hard negatives pushes the model to encode fine-grained semantic detail rather than coarse categorical features.
Several approaches address hard negative mining:
- Large batch sizes: Larger batches contain more total pairs, increasing the probability that some off-diagonal pairs are semantically similar to the positive pair. CLIP uses batches of up to 32,768 pairs precisely because this greatly increases the density of hard negatives.
- Explicit hard negative sampling: Mining specifically difficult negative pairs based on current embedding similarity before each training step. Pairs where the model assigns high similarity but no ground-truth match constitute informative negatives.
- Data augmentation: Creating pairs that share partial content, forcing the model to attend to specific discriminative details to correctly identify the match.
- Curriculum learning: Starting with easy negatives and gradually increasing difficulty as training progresses, mirroring how humans learn by scaffolding difficulty.
The tradeoff with hard negatives is that if negatives are too hard, false negatives (semantically matched pairs that are labeled as negatives) become more likely. At very large batch sizes, some "negative" image-text pairs in the batch may describe the same concept. These false negatives degrade training by penalizing correct associations.