Modality Gap Between Image and Text Embeddings
Problem Statement
Even after contrastive training, image and text embeddings occupy two separated cones on the hypersphere — the "modality gap." Quantify it as the Euclidean distance between the two centroids of the L2-normalized embeddings.
Background
Given a batch of image embeddings and text embeddings, first L2-normalize each vector (contrastive models operate on the unit sphere). The modality gap is the distance between the mean image vector and the mean text vector:
Δ=N1∑iu^i−M1∑jv^j2
Liang et al. (2022) showed this gap is created at initialization and persists through training; shrinking it can change downstream behavior.
Your Task
Implement:
def modality_gap(image_embs, text_embs):
Return the gap as a float rounded to 4 decimals.
Input Format
- image_embs: N x D nested list.
- text_embs: M x D nested list.
Output Format
- A float rounded to 4 decimals.
Sample
print(modality_gap([[1.0, 0.0]], [[0.0, 1.0]]))
Output:
1.4142
Example:
print(modality_gap([[1.0, 0.0]], [[0.0, 1.0]]))
1.4142
-
L2 Normalization: Each embedding vector is projected onto the unit sphere to simulate contrastive model behavior. The image vector [1.0,0.0] has a norm of 12+02=1, so it remains u^1=[1.0,0.0]. The text vector [0.0,1.0] has a norm of 02+12=1, so it remains v^1=[0.0,1.0].
-
Centroid Calculation: The mean vector for each modality is computed. With only one sample per modality, the image centroid is uˉ=[1.0,0.0] and the text centroid is vˉ=[0.0,1.0].
-
Difference Vector: The vector difference between the centroids is calculated to determine the separation: uˉ−vˉ=[1.0−0.0,0.0−1.0]=[1.0,−1.0].
-
Euclidean Distance: The modality gap is the L2 norm of this difference vector: Δ=1.02+(−1.0)2=1+1=2≈1.41421356.
-
The final output is 1.4142
Constraints:
1 <= N, M <= 2000,1 <= D <= 1024.- L2-normalize every vector before averaging (skip zero vectors: leave them as zeros).
- Return the Euclidean distance between the two centroids, rounded to 4 decimals.
1. Background Knowledge
In vision-language models like CLIP and SigLIP, images and text are encoded into a shared embedding space and trained with a contrastive objective (e.g., InfoNCE). A well-documented phenomenon, first characterized by Liang et al. (2022), is the modality gap: even after convergence, the image and text embeddings do not overlap on the hypersphere but instead occupy two distinct, separated cones. This gap is largely determined at initialization and persists throughout training, influencing retrieval accuracy and downstream transfer.
The standard way to quantify this gap is to compute the Euclidean distance between the two centroids (mean vectors) of the L2-normalized embeddings. Because contrastive models operate on the unit hypersphere, every embedding vector must first be normalized to have unit ℓ2 norm. The gap is then:
Δ=N1i=1∑Nu^i−M1j=1∑Mv^j2where u^i and v^j are the normalized image and text vectors, respectively. Note that N and M can differ; the means are computed independently for each modality.
2. Algorithm Approach
This is a straightforward vector arithmetic problem with three conceptual stages:
- L2-normalize every row vector in both input matrices.
- Compute the centroid (row-wise mean) of each normalized matrix.
- Take the Euclidean norm of the difference between the two centroids.
No iterative optimization or complex data structure is needed. The entire computation is a small number of element-wise operations over O((N+M)⋅D) elements.
3. Step-by-Step Strategy
- Normalize each vector: For a vector x∈RD, compute x^=x/∥x∥2. Guard against zero-norm vectors (rare but possible) to avoid division by zero.
- Compute the image centroid: Average all N normalized image vectors element-wise to get a single D-dimensional vector uˉ.
- Compute the text centroid: Similarly average all M normalized text vectors to get vˉ.
- Compute the gap: Calculate ∥uˉ−vˉ∥2=∑d=1D(uˉd−vˉd)2.
- Round the result to 4 decimal places and return as a float.
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.