Recently I've been building a rec system (for what it feels like...

@ZhaiAndrew
Andrew Zhai@ZhaiAndrew
44 views Jan 31, 2024 ~1 min read
Advertisement
1
Recently I've been building a rec system (for what it feels like the 10th time at least...). Open source embeddings are great but the high dimensionality (e.g. 768 floats for CLIP) sucks for efficiency.

In my past life we found 128 bytes per embedding (32 floats, 64 half floats, or 1024 bits) to be enough. Two tricks I've found really effective:
2
(1) Matryoshka embeddings (arxiv.org/abs/2205.13147): TLDR in training given an embedding of full dimensionality M (e.g. 2048), learn N different distance functions for each prefix of the embedding (e.g. l2_norm(embedding[:32]), l2_norm(embedding[:64]), l2_norm(embedding[:128]), etc).

Outcome are lower dim representations that are quite performant, with 64 dim (half floats) being a decent tradeoff of performance and efficiency.

Note: we've observed this method to be more performance empirically than just training 64 dimensional embeddings directly.
Media image
3
(2) Binary embeddings w/ hamming distance ( arxiv.org/pdf/1811.12649…). This method is more of a hack that we started first using in 2014, but TLDR is that during training learn float embeddings of high dimensionality (e.g. 1024). But in the architecture have the final layers be a zero-centering method (e.g. Layernorm) with optionally a relu.

What this encourages the model to learn are zero centered embeddings where negative values don't matter. In practice during inference you can then threshold each dimension value at zero (1 if > 0) to product binary embeddings (e.g. 1024 bits) where hamming distance is nearly as performant as the float counterpart.
Media image
4
Overall, both methods are used in applications indexing billions of documents with substantial queries per second. If I had to choose I would pick method (1) to build in open source given the lack of popularity for hamming distance in available vector DB solutions.
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement