One space for two senses: a CLIP-style multimodal alignment from scratch

“Search images with a sentence.” “Describe this picture.” Those tricks all rest on one idea: putting
different kinds of data — pixels and words — into the same embedding space, so a picture and its
caption land near each other. That’s CLIP. This week I built the shape of it from scratch, on synthetic
two-modality data, to see the single most striking thing it does: zero-shot — it works on things it
was never shown.

What I built

Two modalities that look nothing alike but share hidden structure (each is a different random view of the
same latent — exactly the pixels-vs-tokens situation). I fit a map that lines them up in one space, then
asked it to retrieve the matching “text” for an “image.” The result:

aligned on 160 paired examples:
  train retrieval:                100.0%
  ZERO-SHOT (held-out) retrieval: 100.0%     (chance = 2.5%)

The second line is the one that matters. I fit the alignment on 160 concepts, then handed it 40 it had
never seen — and it matched every one. A shared space learned from some pairs just works on new pairs.

The concepts that made it work

  • Cross-modal is the same move as single-modal. Once both modalities live in one space, “find the
    text for this image” is the identical nearest-neighbour search from the vector-index and RAG weeks — now
    spanning two senses instead of one. Multimodal RAG (retrieve an image for a text query) is literally
    this plus Week 3’s index.
  • Zero-shot is geometry, not training. There’s no per-task fine-tuning. The shared space captures the
    relationship between the modalities, and that relationship holds for concepts outside the training set.
    Generalisation falls out of the structure, not out of more labels.
  • The alignment is the whole artifact. Build the space once; everything downstream — retrieval,
    classification, captioning-adjacent tricks — is cosine similarity on top of it.

The thing that struck me

I expected the held-out number to drop — that’s what “held-out” usually means. It didn’t move at all:
100% on concepts the alignment never touched. That’s when “zero-shot” stopped being a buzzword and became
a mechanical fact for me. The model isn’t memorising pairs; it’s learning the coordinate transform
between two views of the world, and a transform doesn’t care whether it’s seen a particular point before.
It reframed every “zero-shot classification” headline I’d read: it’s not magic, it’s a shared space doing
its job.

Tradeoffs & the road not taken

  • Closed-form, not contrastive. Real CLIP learns both encoders with a contrastive loss on ~400M
    image-caption pairs; I solved a least-squares alignment between two fixed views. Same payoff, none of
    the muscle — and the contrastive training at scale is exactly what buys CLIP its quality and breadth.
    Deliberately kept to the “see the mechanism without a training loop” thread of the whole sprint.
  • Synthetic modalities. Real pixels and tokens need real encoders (a vision transformer + a text
    one). My shared-latent setup is the honest minimal version of “two views of the same thing.”

Where this sits in the system

That closes the vision detour: last time, generating data from noise (diffusion); this time, joining two
modalities in one space (CLIP). Both built to the same standard as the core sprint — from scratch,
correctness-checked, shown with real output. All that’s left is the capstone: wiring the text stack —
inference, KV-cache, retrieval, generation — into one small service I can actually call. Same spine, last
stop: one system, a layer at a time.


Part of a series building a modern AI stack from scratch, told visually. (Last time: a diffusion model from scratch.)