Implement CLIP Contrastive Loss
UberMachine Learning Engineer · Technical ScreenLast updated
This question evaluates understanding and implementation of contrastive representation learning concepts—specifically similarity matrices, symmetric image-text contrastive loss, normalization, temperature scaling, and label construction—testing competency in building losses for embedding alignment in the Machine Learning domain.
Given a minibatch of paired image and text embeddings, implement the symmetric contrastive loss used in CLIP-style image-text representation learning.
You are given:
image_embeddings: a tensor of shape(batch_size, embedding_dim).text_embeddings: a tensor of shape(batch_size, embedding_dim).- The
i-th image corresponds to thei-th text.
Compute a similarity matrix between every image embedding and every text embedding. Then compute:
- Image-to-text loss: cross entropy over each image row, where the correct class for row
iis texti. - Text-to-image loss: cross entropy over each text row, equivalently cross entropy on the transposed similarity matrix, where the correct class for row
iis imagei. - Final loss: the average of the two losses.
Implement this loss function and explain any important details such as normalization, temperature scaling, and label construction.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.