liveBook · Manning

Contrastive learning

As explained in Build a Text-to-Image Generator (from Scratch)

Contrastive learning is a representation-learning technique that brings related examples closer and separates nonmatching examples in an embedding space.

Contrastive learning learns an embedding space by pulling related examples together and pushing nonmatching examples apart. In CLIP, text and images are projected into the same latent space so their semantic similarity can be measured directly. section 8.0 section 8.1

A CLIP-style contrastive-learning pipeline proceeds through these stages:

  • Collect a batch of N matching text–image pairs. section 8.1
  • Encode each caption and image into embeddings with the same dimension. section 8.1
  • Increase similarity for matching pairs while reducing similarity for nonmatching pairs, represented by diagonal and off-diagonal batch relationships. section 8.1.1
  • Optimize both retrieval directions with text-to-image and image-to-text cross-entropy losses, then average them into a symmetric objective. section 8.5

Selecting an image from a prompt

After training, encode the prompt “a dog walks on the beach,” compare its embedding with image embeddings from Flickr 8k, and select the image with the highest similarity. section 8.0 section 8.5

Figure 8.2 How the CLIP model is trained

Use the training diagram to trace paired embeddings, matching similarities, and nonmatching similarities across a batch. section 8.1.1

How Build a Text-to-Image Generator (from Scratch) covers it

How deep it goes
Explains encoders, batch similarities, and bidirectional losses, then stops short of deriving the loss equations.
Examples
Demonstrates Flickr 8k image–caption pairs, a beach-dog prompt, and batch diagonal versus off-diagonal similarities.
What you can do after
Apply it via CLIP to select images matching text descriptions and evaluate image–text compatibility.
How it connects
Connects embeddings with CLIP, cross-modal retrieval, cosine similarity, cross-entropy loss, DistilBERT, and ResNet50.

Also explained in

Compare how 2 books explain contrastive learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Text-to-Image Generator (from Scratch). The section links point to the parts of the book it draws on.