Code
Code
Two encoders and one embedding space
CLIP has an image encoder and a text encoder . The image encoder is a ResNet or a vision transformer, and the text encoder is a transformer that reads the caption. Each output passes through a linear projection to a shared dimension (512 for the model used here) and is normalized to unit length. Image becomes , caption becomes , and the cosine similarity of the two is their dot product.
Code
Code
The loss on a pretrained model
The cell loads the CLIP model with a ViT-B/32 image encoder, released by OpenAI, and builds a batch of ten pairs from CIFAR-10: one test image of each class, with the captiona photo of a \{class\}. The images are and are enlarged to the that the encoder expects, so they are harder to recognize than the photos CLIP was trained on.
Code
Code
Temperature
The scale is the inverse of a temperature, . Cosine similarities lie between and , and in practice the similarities of one image with a set of captions differ by a few hundredths. Fed to a softmax as they are, they give nearly equal probabilities to every caption, and the cross-entropy cannot fall much below however good the embedding is. Multiplying by stretches these small differences. CLIP learns along with the encoders. It is stored as , initialized to , and clipped so that never exceeds 100. The model used here ended training at that limit. The cell shows the probabilities that image 0 (an airplane) assigns to the ten captions at three values of .Code
Code
Negatives and batch size
Every other pair in the batch is a negative, so a batch of pairs gives each image negative captions. The loss of a model that has learned nothing is , and the more negatives there are, the more the loss rewards fine distinctions: telling a photo of a dog from 9 random captions is easy, telling it from 32,767 captions, some of which describe other dogs, is not. CLIP was trained with batches of 32,768 pairs on 400 million image and text pairs collected from the web. Such batches do not fit on one GPU. Each GPU embeds its share of the batch, and all embeddings are gathered on every GPU to build the full matrix. A large batch also has a cost that the CIFAR-10 example makes visible. Some negatives are not really negative. In a batch with two photos of dogs, the caption of one also describes the other, and the loss pushes them apart anyway. With 32,768 web captions this matters less, because captions are rarely identical, but it is the reason why class-label captions such as the ones above make poor training data for CLIP.What the embedding is used for
After training, the two encoders are used separately or together. Zero-shot classification embeds one caption per class and assigns an image to the class whose caption is most similar; the zero-shot section builds this classifier and measures it. A linear probe trains a logistic regression on the frozen image embeddings with labeled examples, and measures how much information the embedding holds about a task. Retrieval ranks a collection of images by their similarity to a text query, or the reverse. Vision language models such as LLaVA keep only the image encoder and feed its patch features, not the single pooled embedding, to a language model.Failure modes
CLIP’s embedding inherits the strengths and the gaps of its training signal. A caption names what is in a photo far more often than how many there are or where they are, and the loss rewards a caption embedding that identifies the right image among candidates, which a bag of the right words often does. Radford et al. report weak zero-shot accuracy on counting objects and on estimating distances, and on handwritten digits, which are rare on the web. Three more failures are easy to show with a few lines of code. Word order. Yuksekgonul et al. found that CLIP and similar models often cannot tell “the horse is eating the grass” from “the grass is eating the horse”, and behave like bags of words (arXiv:2210.01936). The cell compares pairs of captions with the same words in a different order.Code
Code
Code
Code
Code
Code
SigLIP: a sigmoid loss
The softmax in the CLIP loss couples all the pairs of a batch: the probability of the true caption depends on the scores of all captions, so the loss of one pair cannot be computed without the whole matrix, and the result depends on the batch size. SigLIP replaces it with an independent binary decision for every entry of the matrix (Zhai et al., 2023). Entry is a positive example if and a negative one otherwise, with label or : There is a learned scale , as in CLIP, and a learned bias . Because of every entries are negatives, the loss would be dominated by them at the start of training, so is initialized to , which makes every pair start as a confident negative. Each entry is a separate term, so each GPU can compute the loss for its own block of the matrix and exchange embeddings with its neighbors, without a softmax over the whole batch. Zhai et al. found that the sigmoid loss beats the softmax loss at batch sizes below about 16,000, and that the benefit of larger batches levels off around 32,000. The cell measures the loss with SigLIP 2, which keeps the sigmoid loss and adds further training objectives (arXiv:2502.14786). The same change shows up at inference. The scores of a SigLIP model are independent probabilities, one per image and caption, and they do not have to sum to one. An image can match several captions or none.Code
Code
Exercises
- Compute the CLIP loss of the CIFAR-10 batch with two images of the same class (for example two cats, with two identical captions). What happens to the loss, and why is this a false negative?
- Train the contrastive encoder of the representation learning section with the temperature fixed at 1, 0.2 and 0.05. Plot the final loss and the embedding for each, and explain the role of the temperature.
- Repeat the word-order test with SigLIP. Does the sigmoid loss help? Explain why the loss alone should not be expected to fix it.
- Measure the typographic attack as a function of the size of the written word.
Key references
Radford et al. introduced CLIP and evaluated it on more than 30 data sets, including its failures and biases (Radford et al., 2021). Zhai et al. introduced the sigmoid loss of SigLIP (Zhai et al., 2023). Chapter 51 of Torralba, Isola and Freeman covers vision and language (Foundations of Computer Vision, n.d.).References
- Radford, A., Kim, J., Hallacy, C., Ramesh, A., Goh, G., et al. (2021). Learning transferable visual models from natural language supervision. arXiv [cs.CV]. Link
- (n.d.). Foundations of Computer Vision. Link
- Zhai, X., Mustafa, B., Kolesnikov, A., Beyer, L. (2023). Sigmoid Loss for Language Image Pre-Training. arXiv [cs.CV]. Link
Found an error on this page? Report an issue.

