Sub topic 6
The Text Encoder: A Transformer Over Captions
What the text encoder does
Assume you know basic neural-network layers, embeddings, softmax, and Transformer self-attention. You should also know that CLIP trains an image encoder and a text encoder to produce vectors in the same space.
The text encoder converts a caption into one fixed-size vector. For example, it maps “a dog playing in snow” to a vector that should be close to the image vector for that scene. It does not need to generate a caption. Its job is to represent the caption for comparison with images.
CLIP’s text encoder is a Transformer. A Transformer processes a sequence of token vectors while using attention to combine information across the sequence.
From caption to token vectors
The process begins with tokenization. A tokenizer splits text into tokens, which may be whole words, word parts, punctuation, or special markers. Each token ID is looked up in a learned embedding table.
If a caption has L tokens and the Transformer width is D, the token embedding tensor has shape:
(L, D)
The model adds a learned positional embedding to each token vector. This gives the Transformer information about order. Without positional information, “dog bites man” and “man bites dog” would contain the same token vectors in a different arrangement, but their meanings differ.
A special end-of-text token marks the caption’s useful boundary. After the final Transformer layer, CLIP selects the representation at this token position and applies a learned projection. The result is the text embedding used by the contrastive loss.
Attention and CLIP’s sequence direction
Each Transformer block usually contains multi-head self-attention, a feed-forward network, residual connections, and normalization. Self-attention lets the representation of “playing” use information from “dog” and “snow.”
CLIP’s original text Transformer uses a causal attention mask. A token can attend to earlier tokens and itself, but not later tokens. This mask is common in language models that generate text. CLIP does not generate text, but this design was retained in its text tower.
The model still obtains a whole-caption representation from the end-of-text token. Because that token appears last, it can attend to the earlier caption tokens and collect their information.
Output compatibility with images
The image and text encoders have separate internal parameters and may use different architectures. They become compatible only after their outputs are projected into the same dimension. CLIP then usually normalizes both vectors before calculating cosine similarity.
The text encoder therefore learns representations that are useful for visual matching, not representations judged only by ordinary language quality. A phrase can be linguistically valid yet receive a poor embedding if its visual meaning is unclear or underrepresented in the training data.
Common errors
- Treating token IDs as meaningful numerical measurements. Token IDs are indexes, not ordered quantities.
- Forgetting positional embeddings. Attention alone does not reliably encode word order.
- Pooling by an arbitrary average when reproducing CLIP. The original design uses the end-of-text representation followed by projection.
- Comparing unprojected image and text features from incompatible dimensions.
- Assuming the text embedding captures every detail in a long caption. A fixed-size vector compresses information and may lose details.
Exercise
Why can the end-of-text token summarize the complete caption when CLIP uses causal attention?
Show answer
It is placed after the caption tokens, so it can attend to every earlier token and combine their information into one representation.