23 Multimodal Learning: One Space, Two Views
We have learned vision and language in separate coordinate systems. Chapter 19 turned an image into patch tokens. Chapters 16 and 18 turned text into contextual token representations. Those encoders can each organize their own modality, but equal embedding dimensions do not imply a shared cross-modal representation.
In the synthetic example developed below, row 7 of an “image” table and row 7 of a “text” table describe the same event. Their measurement vectors have 14 and 11 coordinates. We do not need either raw coordinate system to imitate the other. We need two learned maps that preserve the pair relation:
\[ \text{image view} \;\longrightarrow\; \text{shared coordinates} \;\longleftarrow\; \text{text view}. \]
This is the central move in multimodal representation learning. Different modalities keep different encoders, while paired observations teach those encoders how to become comparable. The learned space can support cross-modal retrieval and, under a carefully bounded candidate construction, zero-shot classification. It does not by itself create a caption, synthesize an image, or prove that the representation has captured every meaning we care about.
The small matrix already tells us what the training problem must do. Each row is one image query. Each column is one text candidate. Row 1 should select column 1, row 2 should select column 2, and so on. We have seen this machine before.
23.1 Softmax from Chapter 2: scores become weights
Let a minibatch contain \(B\) declared pairs \((x_i^{(I)},x_i^{(T)})\). The image encoder \(f_\theta:\mathbb{R}^{d_I}\to\mathbb{R}^{d}\) and text encoder \(g_\phi:\mathbb{R}^{d_T}\to\mathbb{R}^{d}\) may have different internal architectures. Both encoders must return vectors of the same width. We normalize each output to unit length:
\[ u_i=\frac{f_\theta(x_i^{(I)})}{\lVert f_\theta(x_i^{(I)})\rVert_2}, \qquad v_i=\frac{g_\phi(x_i^{(T)})}{\lVert g_\phi(x_i^{(T)})\rVert_2}, \qquad u_i,v_i\in\mathbb{R}^{d}. \tag{23.1}\]
Now the dot product is cosine similarity. For every cross-modal pair in the batch, form
\[ S_{ij}=\frac{u_i^\top v_j}{\tau}, \qquad \matr{S}\in\mathbb{R}^{B\times B}, \tag{23.2}\]
where \(\tau>0\) is the temperature. Dividing by a small temperature spreads the logits farther apart, whereas a larger temperature softens them. Temperature does not determine which pair is correct: it determines how sharply the softmax reacts to score differences.
In Chapter 14, temperature acted as a bandwidth: for the Gaussian score with bandwidth \(h\), \(\tau=2h^2\). In Chapter 15, the comparison became learnable without a separately parameterized temperature. Here the temperature can be learned too. Write the inverse temperature as \(e^\gamma=1/\tau\):
\[ S_{ij}=e^\gamma u_i^\top v_j. \]
The encoders learn where the two modalities sit; \(\gamma\) learns how sharply a given angular gap should count. CLIP trained this log scale jointly with its two towers and initialized it at the equivalent of \(\tau=0.07\). The paper reports capping \(e^\gamma\) at \(100\) for stability.
This scalar belongs inside the training objective. It is not the post-hoc temperature scaling in Chapter 19, Exercise 6, where the model is frozen and validation labels calibrate its confidence after training.
Chapter 2 introduced softmax as the scores-to-weights machine. Apply it across row \(i\):
\[ p^{I\rightarrow T}_{ij} =\frac{\exp(S_{ij})}{\sum_{k=1}^{B}\exp(S_{ik})}. \tag{23.3}\]
The declared mate of image \(i\) is text \(i\), so the image-to-text loss is ordinary cross-entropy with labels \(0,1,\ldots,B-1\):
\[ \mathcal{L}_{I\rightarrow T} =-\frac{1}{B}\sum_{i=1}^{B}\log p^{I\rightarrow T}_{ii}. \tag{23.4}\]
This form is commonly called a contrastive or InfoNCE-style objective. The batch does two jobs at once: it supplies \(B\) declared matches along the diagonal, and for each query it supplies \(B-1\) alternative candidates off the diagonal. Without a separate binary loss for every pair, the score matrix organizes the whole comparison. In the taxonomy of Section 4.6.1, this objective belongs to Case 3. Here the batch defines the candidate competition: its \(B-1\) alternatives determine the denominator. Changing \(B\) therefore changes the objective being optimized, so batch size belongs to the protocol and is reported like any other design choice.
The preceding loss evaluates image queries against candidate texts. Transposing the score matrix evaluates text queries against candidate images:
\[ \mathcal{L}_{T\rightarrow I} =-\frac{1}{B}\sum_{j=1}^{B} \log\frac{\exp(S_{jj})}{\sum_{k=1}^{B}\exp(S_{kj})}. \tag{23.5}\]
Here text \(j\) is the query and the images \(k\) are its candidates.
The symmetric batch contrastive loss is
\[ \mathcal{L}_{\mathrm{sym}} =\frac{1}{2} \left(\mathcal{L}_{I\rightarrow T}+\mathcal{L}_{T\rightarrow I}\right). \tag{23.6}\]
Equation 23.3 reuses the multiclass classifier of Chapter 2, but the classes are the candidates currently in the batch. The class set changes with the batch; what remains fixed is the rule that the paired row and column share an index.
No manual object class is required, but the correspondence \((x_i^{(I)},x_i^{(T)})\) is still a training label. In web data that relation may be weak, incomplete, or wrong. Calling the procedure “self-supervised” must not hide the work behind those pairs, their noise, or questions of consent and provenance.
What counts as a negative?
The loss uses declared mates, not semantic truth. An off-diagonal caption may describe the same concept as the diagonal caption. Two photographs may show the same event. In those cases the standard one-positive loss treats a plausible match as an alternative to suppress. Larger batches provide more alternatives, but they can also provide more false negatives.
This distinction is easy to miss because the code is so compact:
- Encode both modalities and normalize them into one comparison space.
- Score every image against every text at the chosen temperature.
- Declare the batch diagonal as the supplied pair relation.
- Average image-to-text and text-to-image cross-entropy.
Experiment 23.A · print companion
def symmetric_contrastive_loss(
image: Tensor,
text: Tensor,
image_encoder: nn.Module,
text_encoder: nn.Module,
temperature: float,
) -> Tensor:
# [1]
image_embedding = F.normalize(image_encoder(image), dim=-1)
text_embedding = F.normalize(text_encoder(text), dim=-1)
# [2]
logits = image_embedding @ text_embedding.T / temperature
# [3]
labels = torch.arange(logits.shape[0], device=logits.device)
# [4]
return 0.5 * (
F.cross_entropy(logits, labels)
+ F.cross_entropy(logits.T, labels)
)
identity_batch = torch.eye(3)
loss_check = symmetric_contrastive_loss(
identity_batch, identity_batch, nn.Identity(), nn.Identity(), 0.1
)
assert torch.isfinite(loss_check)The two encoders end at the same width, normalization makes the dot product angular, the transpose reverses the retrieval direction, and the target indices assert that the batch diagonal contains the pair relation. If the data loader shuffles one modality without shuffling the other, the program still runs, but the supervision has become false.
23.2 Make the pair relation learnable
To test this claim, we generate two noisy nonlinear views from one hidden five-dimensional event: a 14-coordinate “image” measurement and an 11-coordinate “text” measurement. The raw features differ, but row \(i\) in one table shares a latent event with row \(i\) in the other. The study downloads no images and tokenizes no corpus.
We fix the protocol before we open the endpoint:
- We generate 1,200 unique pairs once, then split them into 720 fit, 180 validation, and 300 held-out endpoint rows. Standardization uses fit statistics only, so the validation and endpoint rows contribute no preprocessing estimates.
- Each tower is a two-layer MLP and ends at a normalized 16-dimensional embedding. The complete model has 2,392 parameters.
- Under the protocol of Chapter 7, each of five model seeds gives both conditions identical initial weights, the same minibatch schedule and the same fixed settings: AdamW, 160 epochs, batch size 120, and \(\tau=0.08\).
- In the control, we replace the fit-pair relation with one study-wide fixed derangement that assigns every fit image the wrong text, with no fixed points. Architecture, initialization, optimizer, update count, and schedules stay matched.
- In both conditions, we select checkpoints by the loss on the correct validation pairs and consult the 300-row endpoint once, after every choice is fixed.
The metric is Recall@\(k\): for each query, rank every candidate in the other modality. Recall@\(k\) is one for a query when its declared mate lies among the first \(k\) candidates and zero otherwise; the reported value is the average across queries. With 300 unique candidates, random ranking has expected Recall@1 of \(1/300=0.0033\) and Recall@5 of \(5/300=0.0167\).
- Define the reusable helpers:
PairedSplit,make_paired_data,TwoTower,symmetric_loss, andpair_loss. - Define the reusable helpers:
retrieval_metrics,derangement,train_tower,report, andreport_paired_contrast. - Generate the paired fit, validation, and held-out splits and start empty
modelsandrecords. - Run the five-seed paired-versus-shuffled retrieval study.
- Print split sizes, parameter count, recall metrics, paired Recall@1 contrasts, and random-ranking baselines.
Experiment 23.B · print companion
# [1]
@dataclass(frozen=True)
class PairedSplit:
image: Tensor
text: Tensor
def make_paired_data() -> tuple[PairedSplit, PairedSplit, PairedSplit]:
generator = torch.Generator().manual_seed(1206050)
latent_dim, image_dim, text_dim = 5, 14, 11
latent = torch.randn(1_200, latent_dim, generator=generator)
image_map = torch.randn(latent_dim, image_dim, generator=generator)
text_map = torch.randn(latent_dim, text_dim, generator=generator)
image = torch.tanh(latent @ image_map / latent_dim**0.5)
text = torch.tanh(latent @ text_map / latent_dim**0.5)
image += 0.045 * torch.randn(image.shape, generator=generator)
text += 0.045 * torch.randn(text.shape, generator=generator)
fit_end, validation_end = 720, 900
image_mean = image[:fit_end].mean(0, keepdim=True)
image_std = image[:fit_end].std(0, keepdim=True).clamp_min(1e-6)
text_mean = text[:fit_end].mean(0, keepdim=True)
text_std = text[:fit_end].std(0, keepdim=True).clamp_min(1e-6)
image = (image - image_mean) / image_std
text = (text - text_mean) / text_std
return (
PairedSplit(image[:fit_end], text[:fit_end]),
PairedSplit(image[fit_end:validation_end], text[fit_end:validation_end]),
PairedSplit(image[validation_end:], text[validation_end:]),
)
class TwoTower(nn.Module):
def __init__(
self, image_dim: int, text_dim: int,
width: int = 40, embed_dim: int = 16,
):
super().__init__()
self.image_encoder = nn.Sequential(
nn.Linear(image_dim, width), nn.ReLU(), nn.Linear(width, embed_dim)
)
self.text_encoder = nn.Sequential(
nn.Linear(text_dim, width), nn.ReLU(), nn.Linear(width, embed_dim)
)
def forward(self, image: Tensor, text: Tensor) -> tuple[Tensor, Tensor]:
image_embedding = F.normalize(self.image_encoder(image), dim=-1)
text_embedding = F.normalize(self.text_encoder(text), dim=-1)
return image_embedding, text_embedding
def symmetric_loss(
image_embedding: Tensor, text_embedding: Tensor, tau: float = 0.08,
) -> Tensor:
logits = image_embedding @ text_embedding.T / tau # (B, B)
labels = torch.arange(logits.shape[0])
return 0.5 * (
F.cross_entropy(logits, labels) + F.cross_entropy(logits.T, labels)
)
@torch.no_grad()
def pair_loss(model: TwoTower, split: PairedSplit) -> float:
model.eval()
image_embedding, text_embedding = model(split.image, split.text)
return float(symmetric_loss(image_embedding, text_embedding))
# [2]
@torch.no_grad()
def retrieval_metrics(model: TwoTower, split: PairedSplit) -> dict[str, float]:
model.eval()
image_embedding, text_embedding = model(split.image, split.text)
scores = image_embedding @ text_embedding.T
target = torch.arange(scores.shape[0])
def recall_at_k(matrix: Tensor, k: int) -> float:
candidates = matrix.topk(k, dim=1).indices
return float((candidates == target[:, None]).any(dim=1).float().mean())
return {
"I→T R@1": recall_at_k(scores, 1),
"T→I R@1": recall_at_k(scores.T, 1),
"I→T R@5": recall_at_k(scores, 5),
"T→I R@5": recall_at_k(scores.T, 5),
}
def derangement(size: int, generator: torch.Generator) -> Tensor:
target = torch.arange(size)
while True:
order = torch.randperm(size, generator=generator)
if not bool((order == target).any()):
return order
def train_tower(
seed: int,
fit_split: PairedSplit,
validation_split: PairedSplit,
shuffled_pairs: bool,
) -> TwoTower:
torch.manual_seed(seed)
model = TwoTower(fit_split.image.shape[1], fit_split.text.shape[1])
optimizer = torch.optim.AdamW(model.parameters(), lr=2e-3, weight_decay=1e-3)
pair_generator = torch.Generator().manual_seed(88_000)
text_order = (
derangement(len(fit_split.text), pair_generator)
if shuffled_pairs else torch.arange(len(fit_split.text))
)
schedule_generator = torch.Generator().manual_seed(99_000 + seed)
schedules = [
torch.randperm(len(fit_split.image), generator=schedule_generator)
for _ in range(160)
]
best_validation = float("inf")
best_state: dict[str, Tensor] | None = None
for epoch, order in enumerate(schedules, start=1):
model.train()
for rows in order.split(120):
image_embedding, text_embedding = model(
fit_split.image[rows], fit_split.text[text_order[rows]]
)
loss = symmetric_loss(image_embedding, text_embedding)
optimizer.zero_grad(set_to_none=True)
loss.backward()
optimizer.step()
if epoch % 5 == 0:
validation_loss = pair_loss(model, validation_split)
if validation_loss < best_validation:
best_validation = validation_loss
best_state = copy.deepcopy(model.state_dict())
assert best_state is not None
model.load_state_dict(best_state)
return model
# [3]
fit_split, validation_split, held_out_split = make_paired_data()
models: dict[tuple[int, str], TwoTower] = {}
records: list[dict[str, object]] = []
# [4]
for seed in range(6050, 6055):
for condition, shuffled_pairs in (("paired", False), ("shuffled", True)):
model = train_tower(seed, fit_split, validation_split, shuffled_pairs)
models[(seed, condition)] = model
records.append({
"seed": seed,
"condition": condition,
"validation": retrieval_metrics(model, validation_split),
})
# The endpoint is consulted after every design and checkpoint decision above.
for record in records:
model = models[(int(record["seed"]), str(record["condition"]))]
record["held_out"] = retrieval_metrics(model, held_out_split)
def report(split_name: str, key: str) -> None:
print(split_name)
for condition in ("paired", "shuffled"):
chosen = [record for record in records if record["condition"] == condition]
fields = list(chosen[0][key].keys())
summary = []
for field in fields:
values = [float(record[key][field]) for record in chosen]
summary.append(f"{field} {mean(values):.4f} (SD {stdev(values):.4f})")
print(f" {condition:8s}: " + "; ".join(summary))
def report_paired_contrast(field: str) -> None:
differences = []
for seed in range(6050, 6055):
paired = next(
record for record in records
if record["seed"] == seed and record["condition"] == "paired"
)
shuffled = next(
record for record in records
if record["seed"] == seed and record["condition"] == "shuffled"
)
differences.append(
float(paired["held_out"][field]) - float(shuffled["held_out"][field])
)
print(
f"paired−shuffled held-out {field}: "
f"{mean(differences):.4f} (paired SD {stdev(differences):.4f})"
)
reference_model = models[(6050, "paired")]
parameter_count = sum(p.numel() for p in reference_model.parameters())
# [5]
split_sizes = (
len(fit_split.image), len(validation_split.image), len(held_out_split.image)
)
print(
f"split sizes: fit={split_sizes[0]}, validation={split_sizes[1]}, "
f"held-out={split_sizes[2]}"
)
print(f"parameters: {parameter_count:,}; model seeds: 6050–6054")
report("validation", "validation")
report("held-out endpoint", "held_out")
report_paired_contrast("I→T R@1")
report_paired_contrast("T→I R@1")
print(
f"held-out random-ranking expectations: "
f"R@1={1 / 300:.4f}; R@5={5 / 300:.4f}"
)split sizes: fit=720, validation=180, held-out=300
parameters: 2,392; model seeds: 6050–6054
validation
paired : I→T R@1 0.9911 (SD 0.0084); T→I R@1 0.9900 (SD 0.0061); I→T R@5 1.0000 (SD 0.0000); T→I R@5 1.0000 (SD 0.0000)
shuffled: I→T R@1 0.0044 (SD 0.0046); T→I R@1 0.0078 (SD 0.0063); I→T R@5 0.0233 (SD 0.0133); T→I R@5 0.0222 (SD 0.0152)
held-out endpoint
paired : I→T R@1 0.9747 (SD 0.0038); T→I R@1 0.9660 (SD 0.0092); I→T R@5 1.0000 (SD 0.0000); T→I R@5 1.0000 (SD 0.0000)
shuffled: I→T R@1 0.0027 (SD 0.0028); T→I R@1 0.0027 (SD 0.0043); I→T R@5 0.0113 (SD 0.0084); T→I R@5 0.0100 (SD 0.0097)
paired−shuffled held-out I→T R@1: 0.9720 (paired SD 0.0038)
paired−shuffled held-out T→I R@1: 0.9633 (paired SD 0.0105)
held-out random-ranking expectations: R@1=0.0033; R@5=0.0167
Across the five seeds, the paired condition reaches validation Recall@1 of 0.9911 image-to-text and 0.9900 text-to-image. The shuffled control reaches only 0.0044 and 0.0078. That validation separation is the predeclared mechanism check: under this protocol, correctly paired training supports retrieval on unseen events, while the fixed derangement does not.
On the single held-out endpoint, paired image-to-text Recall@1 is 0.9747 (sample SD 0.0038 across model seeds), and paired text-to-image Recall@1 is 0.9660 (SD 0.0092). Both Recall@5 means are 1.0000. The shuffled condition stays at 0.0027 (SD 0.0028) and 0.0027 (SD 0.0043) for Recall@1, close to the 0.0033 random-ranking expectation. Its Recall@5 means, 0.0113 and 0.0100, remain in the same small-sample neighborhood as the 0.0167 random expectation.
Because the two conditions share seeds, the primary top-1 comparison is paired within seed rather than reconstructed from those marginal summaries. The paired-minus-shuffled held-out contrast is 0.9720 (paired SD 0.0038) image-to-text and 0.9633 (paired SD 0.0105) text-to-image.
These standard deviations describe optimization variation on one fixed synthetic dataset and one fixed derangement; they are not uncertainty intervals for a population of image–text tasks. The study establishes one finite mechanism: correct pair structure is sufficient for these small towers to learn cross-view retrieval, while the matched derangement removes that generalizable signal. It does not estimate what CLIP, ALIGN, or any natural-data model will achieve.
- Define the reusable helpers:
held_valuesandscore_window.
Experiment 23.C · print companion
# [1]
def held_values(condition: str, field: str) -> list[float]:
return [
float(record["held_out"][field])
for record in records
if record["condition"] == condition
]
@torch.no_grad()
def score_window(model: TwoTower, split: PairedSplit, size: int = 40) -> Tensor:
model.eval()
image_embedding, text_embedding = model(split.image[:size], split.text[:size])
return image_embedding @ text_embedding.T
The control is stronger than showing a falling training loss: with shuffled pairs, the model can spend capacity fitting accidental associations in the fit table. Validation on the correct pairs does not reward that memorization. The claim is that paired correspondence teaches alignment, so the ablation breaks the pairing and keeps everything else matched. That is the rule from the experimentation interlude: tune for performance, ablate to test the claim.
To see how training changed the geometry, project the two towers’ embeddings of the same held-out rows into two dimensions, before and after training:
- Define the reusable
two_dim_viewhelper. - Build an untrained
TwoTowerat the first seed and list it with that seed’s paired model as titled views.
Experiment 23.D · print companion
23.3 Retrieval is not generation
A two-tower model operates as a ranking function: given an image query and a finite text candidate set, it ranks the candidates. Given a text query and a finite image set, it ranks the images. This is cross-modal retrieval:
\[ \hat{j}(x^{(I)}) =\argmax_{j\in\{1,\ldots,M\}} \;u(x^{(I)})^\top v(x_j^{(T)}). \tag{23.7}\]
The answer must already be present among the \(M\) candidates. The model has no decoder over text tokens and no sampling distribution over images. Chapter 22 described what a generator needs: a random source, a conditional or marginal distribution, and a sampling rule. A similarity function alone does not specify how to generate new text or images beyond the supplied candidates.
This distinction prevents a common category error, treating every shared embedding as a generator:
| Task | Input | Output rule | What must already exist? |
|---|---|---|---|
| image-to-text retrieval | image query | rank text embeddings | text candidate pool |
| text-to-image retrieval | text query | rank image embeddings | image candidate pool |
| image classification by text | image query | rank class descriptions | chosen descriptions |
| image captioning | image | sample or decode text tokens | conditional language decoder |
| text-to-image generation | text | sample an image-valued process | conditional image generator |
A larger vision–language system may combine a vision encoder, an adapter, and a language decoder. It may train with contrastive, matching, and generative objectives. That composition can support generation, but the new capability comes from the decoder and its objective, not from renaming retrieval.
Zero-shot classification is retrieval over descriptions
Suppose a task supplies \(C\) class names. Write one description \(t_c\) for each class, encode those descriptions once, and retrieve the description closest to a new image:
\[ \hat{c} =\argmax_{c\in\{1,\ldots,C\}} \frac{f_\theta(x)^{\top}g_\phi(t_c)} {\lVert f_\theta(x)\rVert_2\lVert g_\phi(t_c)\rVert_2}. \tag{23.8}\]
This is zero-shot classification in the CLIP-style operational sense: no task-specific parameter update is made before this candidate-ranking step. The phrase does not mean that the underlying concept was absent from pretraining. It does not mean that no human chose the class set or wrote the descriptions. It does not mean that the same prediction survives a different prompt, candidate pool, language, or distribution.
Chapter 20 used “zero-shot” for prompting a fixed language model without demonstrations. Here it means classifying with fixed encoders and text prototypes. Both avoid a task-specific weight update, but they differ in inputs, outputs, and evaluation.
Add a near-synonym, remove a competing description, or change the prompt template, and the softmax denominator changes. A high weight within one candidate set is not a candidate-independent probability that the description is true. Report the candidate construction and prompt policy with the metric.
23.4 Shortcuts enter through the pair definition
Contrastive learning rewards whatever makes the diagonal distinguishable. Although the intended signal is semantic, with paired inputs describing the same scene, optimization can exploit non-semantic statistical shortcuts.
- A camera watermark and a caption-source tag may identify the same website. The towers can align sources rather than content.
- Near-duplicate images may cross the split with paraphrased captions. Retrieval then measures leakage, not transfer.
- A broad caption such as “an outdoor scene” may fit many images. The diagonal-only target assumes there is one correct mate.
- A candidate pool may make the task artificially easy. Retrieving a dog caption among unrelated equations is different from retrieving it among fine-grained dog breeds.
- Aggregate recall may hide poor behavior for a language, geographic region, image style, or social group that is rare in the paired data.
These are not side issues: the declared pairs define what the objective can teach. Natural-language supervision is attractive because paired text is abundant and rich. The same scale can carry noise, stereotypes, copyrighted material, private information, and uneven coverage. A responsible model card therefore describes how pairs were obtained, filtered, licensed, and audited, not only how many there were.
What a retrieval result must report
Before accepting a multimodal retrieval result, ask seven questions:
- What is the unit of pairing? One image–caption record, one entity with several views, one time-aligned segment, or something else?
- What can cross a split? Split by the underlying entity or source when row-level splitting would leak near duplicates.
- What is a valid match? If several candidates are correct, use multi-positive labels or a metric that does not mark valid matches wrong.
- What is the candidate pool? State its size, construction, duplicates, and difficulty. Recall@1 over 100 candidates is not Recall@1 over one million.
- Which direction is measured? Image-to-text and text-to-image can behave differently; the symmetric training loss does not guarantee identical retrieval.
- Which shortcuts were ablated? Shuffle pairs, mask a suspected nuisance, remove one modality, or construct a hard candidate set that defeats metadata matching.
- Where were decisions made? Report the seed panel, validation choices, final endpoint policy, subgroup slices, and qualitative failure audit.
Our synthetic study answers a narrow version of these questions. The unique latent row is the pairing unit; splits do not overlap; the candidate pool is the complete split; both directions are reported; the shuffled-pair ablation receives a matched budget; and the held-out endpoint plays no part in any model, hyperparameter, checkpoint, stopping, or analysis-design decision. Natural multimodal data makes every one of those questions harder to answer, but none of them optional.
Close the book for one minute and answer these questions from memory.
- Why do equal embedding widths fail to align two modalities by themselves?
- What experiment distinguishes a learned pair relation from accidental memorization?
- Why can a dual encoder retrieve a caption without being able to generate one?
23.5 Okay, so: what we pair is what we teach
Multimodal learning begins with a relation between views. Separate encoders map those views into normalized shared coordinates; cosine similarities fill a cross-modal score matrix; and the scores-to-weights machine of Chapter 2 turns each row and column into a contrastive classification problem. The symmetric objective teaches retrieval in both directions.
The shuffled-pair study shows what carries the supervision. With the true fit relation, small towers retrieve held-out mates far above random ranking. Break the relation while holding the training budget fixed, and the generalizable signal disappears. Matching embedding width was never sufficient. Paired rows also had to share a latent event.
From there, keep the capability boundary clear. Retrieval selects an existing candidate. Zero-shot classification retrieves among human-supplied descriptions. Generation needs a decoder and a sampling rule. In every case, the pair definition, candidate pool, split unit, and failure slices belong beside the metric. The declared pair relation determines which alignments the loss directly rewards, and retrieval on unseen pairs tests what generalizes.
Sources and further reading
- van den Oord, Li, and Vinyals, “Representation Learning with Contrastive Predictive Coding” (2018), introduced the InfoNCE name and probabilistic contrastive formulation in predictive representation learning.
- Radford et al., “Learning Transferable Visual Models From Natural Language Supervision” (2021), introduced CLIP’s paired image–text dual-encoder training and zero-shot transfer program; the reported study used 400 million internet image–text pairs. The paper optimized a log-parameterized score scale, initialized it at the equivalent of \(\tau=0.07\), and reported capping the scale at \(100\). The released reference model initializes and exponentiates that parameter but does not implement the cap.
- Jia et al., “Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision” (2021), developed ALIGN’s dual-encoder contrastive approach with a deliberately noisy large paired corpus.
- PyTorch’s documentation for normalization and cross-entropy specifies the two framework operations used in the executable study (
F.normalizeandF.cross_entropy).
Exercises
(Pencil.) Let a batch have score matrix \(\matr{S}\in\mathbb{R}^{4\times4}\). Write the image-to-text loss for row 2 and the text-to-image loss for column 2. Then explain why transposing the logits changes the competition even though both losses send gradients into both encoders.
(Code.) Rerun the synthetic study with only \(\mathcal{L}_{I\rightarrow T}\). Report both retrieval directions and use an open-ended conclusion: does one-sided training change the two directions equally in this finite regime? Keep the initialization and minibatch schedule paired with the symmetric run.
(Code.) Construct a validation variant in which 20% of the events have several valid text views, for example fresh noisy text measurements of the same latent event, and record every valid image–text match. Explain why diagonal Recall@1 can now mark a semantically acceptable retrieval wrong. Then compare diagonal-only Recall@1 with a group-aware retrieval metric, and define what counts as correct.
(Code.) Add a shared one-coordinate “source tag” to both fit modalities and train the paired model. At validation, randomize the tag while preserving the true latent pairs. Compare ordinary and tag-randomized retrieval, and diagnose whether the model used the intended semantic signal or the shortcut.
(Pencil.) A fixed image encoder chooses among “a photo of a crane,” “a photo of a bird,” and “a photo of construction equipment.” List at least three ambiguities in the candidate construction. Propose a prompt and evaluation policy that makes the zero-shot claim bounded and reproducible without claiming the resulting softmax is a universal probability of truth.
(Pencil.) (a) Let \(G_{ij}=u_i^\top v_j\), write \(S_{ij}=e^\gamma G_{ij}\), and derive \(\partial\mathcal{L}/\partial\gamma\) for the symmetric loss. Express each directional term as a softmax-weighted mean similarity minus the diagonal similarity. Determine the gradient’s sign when every diagonal similarity exceeds that weighted mean, predict how gradient descent moves \(\gamma\), and explain why the gradient fades as diagonal probabilities approach one.
(Code.) Add a learned
logit_scaleto the two-tower study. Initialize it at \(\log(1/0.08)\) and uselogit_scale.exp().clamp(max=100)in the score matrix. Keep initialization, minibatch order, checkpoint policy, and evaluation queries paired with the fixed-\(\tau\) run. Report Recall@1 in both directions, the final learned temperature, and fixed-temperature baselines for \(\tau\in\{0.03,0.08,0.2\}\). Does the learned value lie inside the sweep’s performance plateau?(Audit.) State why this jointly trained sharpness parameter is not the post-hoc calibration temperature in Chapter 19, Exercise 6. Name which parameters and data split each procedure is permitted to use.