Sub topic 1
Embeddings: representing images and text as vectors
This chapter assumes you know Python, basic linear algebra (dot products and vector norms), and the idea of a neural network that maps inputs to vectors. You do not need prior experience with CLIP.
Prerequisites
You should be comfortable with Python lists, functions, and floating-point numbers. You should know what a dot product is and how to compute the length (norm) of a vector. Familiarity with convolutional networks or Transformers at a high level helps, but is not required.
Embeddings: representing images and text as vectors
An embedding is a fixed-length vector of numbers that represents an input. A model learns to place inputs with similar meaning near each other in this vector space. CLIP turns each image into a vector and each caption into a vector, so both can be compared with the same arithmetic.
Cosine similarity and why embeddings are normalized
Cosine similarity measures the angle between two vectors. It equals the dot product of the two vectors after each is scaled to length 1. Scaling removes the effect of vector length, so only direction matters.
import math
def normalize(v):
norm = math.sqrt(sum(x * x for x in v))
return [x / norm for x in v]
def cosine_similarity(a, b):
a_unit, b_unit = normalize(a), normalize(b)
return sum(x * y for x, y in zip(a_unit, b_unit))
image_vec = [0.9, 0.1, 0.3]
caption_a = [0.8, 0.2, 0.25]
caption_b = [-0.5, 0.7, 0.1]
print(round(cosine_similarity(image_vec, caption_a), 3))
print(round(cosine_similarity(image_vec, caption_b), 3))The first score is high, and the second is negative. The caption that points in a similar direction to the image scores higher.
The idea of a joint image-text embedding space
A joint space is one vector space that holds both image vectors and text vectors. Training pulls each matching image and caption together and pushes mismatched pairs apart. Once this holds, a text query can search images, and an image can search captions, without any extra mapping step.
CLIP's dual-encoder architecture
CLIP uses two separate networks: an image encoder and a text encoder. Each one runs on its own input type, and neither sees the other's input. Each encoder ends with a projection layer that maps its output to the same dimension, for example 512 values in the ViT-B/32 model. That shared size is what makes the two vectors comparable.
The image encoder: ResNet and Vision Transformer variants
The image encoder converts pixels into one vector. OpenAI released CLIP with two families. The ResNet variants use a modified convolutional network. The Vision Transformer (ViT) variants split the image into fixed-size patches, treat each patch as a token, and process the tokens with a Transformer. ViT-B/32 is a common default, where 32 is the patch size in pixels.
The text encoder: a Transformer over captions
The text encoder is a Transformer that reads a tokenized caption. It uses a causal attention mask, so each token only attends to earlier tokens. CLIP takes the output at the end-of-text token as the caption embedding, then projects it to the shared dimension.
Exercise
Exercise
In the cosine similarity example, why does the code call normalize() before taking the dot product?
Show answer
Normalizing scales each vector to length 1. The dot product of two unit vectors equals the cosine of the angle between them, so the result depends only on direction and not on vector length.