MLGuerrillaStart M1 →
Free · in beta·intermediate·M42·65 min read·Prereq: Fine-Tuning Fundamentals (M21), for LoRA adapters and choosing a checkpoint. RAG & Retrieval, Part 1 (M19-1) is recommended. This module recaps the encoder basics that M33 and M34 will cover in depth.

Fine-Tuning Embedding Models

The capability

What fine-tuning an embedding model is

As you already know from M19-1, an embedding model takes a piece of text and turns it into a fixed-length list of numbers, which we call a vector, and texts that mean similar things end up with vectors pointing in similar directions. Cosine similarity is how you measure how close two of those directions are. Orrin's help assistant in M19-1 used bge-small, which turns each piece of text into 384 numbers. The image encoder of SigLIP, the one Relist's "you might also like" row used in M21, turns every photo into 768 numbers.

Inside the model is an encoder, which is basically a transformer that reads the whole input at the same time. It produces an output for every token, or for every patch if the input is an image, and then a last step called pooling squeezes all of those down into one vector. Often that's just an average of them, and SigLIP's image encoder does it with a small attention layer of its own. After that, the vector usually gets scaled so its length is 1. M33 and M34 will go into encoders in depth, and for this module all you need to know is that the model ends in one vector of a fixed size.

What these models count as similar comes from how they were trained. They learn from pairs of things that belong together, like a question and its answer or a photo and its caption. Training pulls the two vectors in each pair closer together and pushes them away from every other item in the same batch. CLIP and SigLIP were trained that way with one encoder for images and another for text, and people usually call those the two towers. So a photo and the words that describe it end up near each other in one shared space, and that's how M13 was able to classify a photo by comparing it against the vectors of category names.

For SigLIP, "similar" pretty much means "the web would caption these two photos with the same words", because that's what its pretraining pairs told it, which is why M21's row put a brass vase right next to a brass lamp. Fine-tuning means you keep training the encoder, this time on your own pairs, so the pretraining pairs are no longer the only thing deciding what counts as similar.

Each training example is a pair, like two listings that one buyer clicked in the same session. You compute the loss over the whole batch at the same time, because the only way the model can learn "these two belong together" is as "these two are closer together than these other items". What you're changing is the encoder's weights, and that means every vector it produces moves too. The vector stays the same size, so your index keeps the same shape, but every vector already stored in it came from the old weights, and you have to compute all of them again before the new model can search them.

If you look at the listings around one brass floor lamp, training on those pairs moves the vase and the candle holder out of the five nearest and brings in mid-century lamps from $90 to $320.

A schematic of the embedding space around one query listing, a brass mid-century floor lamp at $140, drawn twice with a dashed ring around the five nearest. On the left, before fine-tuning, the five nearest by the base SigLIP encoder are a brass vase at $45, a brass candle holder at $30, a chrome modern lamp at $420, a chrome arc lamp at $460 and one mid-century lamp at $120. On the right, after fine-tuning on pairs of listings clicked in one session, the five nearest are mid-century lamps in teak, brass, walnut, a tripod and an arc, priced from $90 to $320, which are highlighted, and the vase and candle holder sit outside the ring, marked as moved out.
Listings outside the ring move too, which is why every stored vector has to be computed again.

Fine-tuning only changes where the encoder puts things, so it can't add information the encoder never sees. Price isn't in the pixels, so no amount of training will get it into a vector that's computed from a photo, and the index still only holds the listings you've put into it. If you want the query and an item read side by side, you need a different kind of model, which is the reranker from M19-1. That's a cross-encoder, and it reads a question and a passage together and gives them a score. A missing filter or a stale listing is a bug in the code around the model, and training won't fix it. And the model learns whatever the pairs carry, which includes the habits of whichever ranker decided what users got to click in the first place.

Some places it shows up

  • Similar items in e-commerce, where clicks or purchases in one session tell you which products buyers treat as alternatives, like Relist's row.
  • Site search on your own vocabulary, where buyers type model names and abbreviations that a general model has barely seen.
  • RAG retrieval on domain terms, like the fault codes in Orrin's documents that M19-1 found bge-small handled badly.
  • Matching the same thing photographed twice, like duplicate listings or the same part in two catalogues.

Keep reading

The rest of this lesson is free with an account

Create a free account to read the remaining 15 sections. It takes about twenty seconds and there's nothing to pay. The same account opens the slide decks and CV reviews.

  1. 02Relist's row matched by look, when buyers wanted the same style and price
  2. 03Score the cheap options before you train anything
  3. 04Build the pairs from your logs, and know what the logs can't tell you
  4. 05The negatives decide most of what the model learns
  5. 06Use the in-batch loss with a scale, and put a LoRA adapter on the image tower only
  6. and 10 more

That's the last one written so far

Pick your next module from the board.

All modules →