Sub topic 3
The Idea of a Joint Image-Text Embedding Space
This chapter assumes you know basic Python and PyTorch tensors, neural-network embeddings, dot products, and vector norms. It also assumes you understand that an embedding is a numeric vector representing an input.
From separate representations to one space
An image model can turn an image into a vector. A text model can turn a sentence into another vector. Usually, these vectors belong to separate spaces. A vector from the image model cannot be compared directly with a vector from the text model.
CLIP changes this arrangement. It learns an image encoder and a text encoder whose outputs have the same size and meaning. Matching images and text are trained to produce nearby vectors. Unrelated images and text produce vectors that point in different directions.
This shared space is called a joint image-text embedding space. A point in the space does not represent only pixels or only words. Its position represents concepts that both modalities can describe.
For example, an image of a dog running on a beach and the sentence “a dog running near the ocean” should occupy nearby locations. The exact pixel arrangement and the exact wording can differ, while the shared meaning remains similar.
Similarity in the shared space
CLIP commonly compares vectors with cosine similarity. Cosine similarity measures the angle between two vectors:
- A value near 1 means the vectors point in similar directions.
- A value near 0 means little directional similarity.
- A value near -1 means opposite directions.
The vector lengths do not matter after normalization. Normalization scales each vector to length 1, so the dot product becomes cosine similarity.
import torch
import torch.nn.functional as F
image_embedding = torch.tensor([[2.0, 1.0, 0.0]])
text_embeddings = torch.tensor([
[2.0, 1.0, 0.0], # matching meaning
[0.0, 0.0, 3.0], # different meaning
])
image_embedding = F.normalize(image_embedding, dim=-1)
text_embeddings = F.normalize(text_embeddings, dim=-1)
similarities = image_embedding @ text_embeddings.T
print(similarities.squeeze(0))The first text vector points in the same direction as the image vector, so it receives the larger similarity score.
Why this enables open-vocabulary recognition
Traditional image classification uses a fixed list of output classes. If the model was trained with “cat” and “dog,” adding “red bicycle” usually requires changing or retraining the classifier.
A joint space supports open-vocabulary recognition. Open-vocabulary means that the user can describe candidate classes with text at inference time. The model embeds the image and several text prompts, then selects the prompt with the highest similarity.
The text prompts act as temporary class descriptions:
- Encode an image.
- Encode prompts such as “a photo of a cat” and “a photo of a dog.”
- Compare the image vector with every text vector.
- Choose the most similar prompt.
The model does not need a new output layer for each class. Its language encoder supplies the candidate labels.
A geometric view
The shared space does not store a human-readable label at every point. Instead, meaning is distributed across directions and neighborhoods. A query can therefore match many possible descriptions rather than only one predefined class.
Exercise
Why can CLIP compare an image with a text prompt even though one input contains pixels and the other contains words?
Show answer
Separate encoders map both inputs into vectors of the same joint space. Cosine similarity can then compare the directions of those vectors.