← 返回 uber 的题目列表Implement CLIP-Style Symmetric Contrastive Loss (Image-Text)
类型:online_judge
Problem
Given a batch of image and text embeddings paired one-to-one as positives, implement the CLIP-style symmetric contrastive loss.
Let batch size be B. Image embeddings I ∈ R^{B×D} and text embeddings T ∈ R^{B×D}.
Compute the similarity matrix:
S = I @ T^T, where S[i][j] is the similarity between image i and text j.
Build labels:
labels = [0, 1, 2, ..., B-1] (image i matches text i).
Compute bidirectional cross-entropy and average:
loss_i = cross_entropy(S, labels) (image→text)
loss_t = cross_entropy(S^T, labels) (text→image)
loss = (loss_i + loss_t) / 2
Implement this loss (PyTorch or NumPy-style pseudocode is fine). cross_entropy(logits, labels) should apply softmax row-wise, take the negative log-likelihood at the given label per row, and average over the batch.
I/O (for implementation/self-test)
Input: I (float matrix B×D), T (float matrix B×D)
Output: scalar float loss
Constraints
1 ≤ B ≤ 2048
1 ≤ D ≤ 4096
Must be numerically stable (e.g., subtract row-wise max before softmax).
Example test cases (illustrative)
Case 1
Input: B=1, any I,T
Expected: labels=[0], loss=0.0 (single class => prob 1).
Case 2
Input: B=2, D=2
I = [[1,0],[0,1]]
T = [[1,0],[0,1]]
Expected: diagonal similarities are highest, loss should be small.
Case 3
Input: B=2, D=2
I = [[1,0],[0,1]]
T = [[0,1],[1,0]]
Expected: diagonal similarities are lowest, loss should be large.
Case 4
Input: B=3, D=3
I = identity(3)
T = identity(3)
Expected: loss_i == loss_t, and loss should be small.
Case 5
Input: random I,T (e.g., standard normal)
Expected: finite loss (no NaN/Inf).
Example
Input
B=1, D=3
I=[[0.2, -0.1, 0.5]]
T=[[0.4, 0.0, -0.3]]
Output
0.0