Skip to content

Embeddings as a lookup step

Add a pretrained sentence embedding of a text column in Prepare, and see why it's a lookup, not SQL.

Applied ~10 min8 steps

You will be able to

  • Add 384 meaning-based numbers per text with Add embeddings
  • Explain why a frozen encoder needs no per-fold refit, while a learned vocabulary does
  • Say what the lookup cache holds and when the app fills it

Read first

01

Meaning, not shared words

Read

"The striker scored late" and "a last-minute goal" share no words, so term columns and TF-IDF see two unrelated texts. A sentence embedding places them close together: a pretrained model (all-MiniLM-L6-v2) turns each text into 384 numbers, and texts that say similar things get similar numbers.

In Prepare (⌘3), Add embeddings adds those numbers as ordinary columns, review_emb_1 to review_emb_384. Grids show them as one collapsed column, and Predict's feature picker treats them as one block.

02

Why it isn't SQL

Read

Every other Prepare step compiles to SQL. A transformer can't run in SQL, so Add embeddings is a lookup step:

  1. The model runs on this Mac, in the Python or R engine. Your texts aren't sent anywhere.
  2. Each vector is cached in the project, keyed by the text's fingerprint (its SHA-256).
  3. The step's SQL joins that cache: LEFT JOIN … ON key = sha256(text).

Preview and Apply first compute any texts the cache doesn't have yet, so the first run on a big column takes a while and later runs are quick. The step form says "Lookup step: not pure SQL", and exported code reads the same cache (or recomputes it with the helpers).

03

Images and sounds

Read

Add media features (the op picker's Media group) is the same kind of step for a folder of images or recordings added in Data. A frozen encoder, such as a small thumbnail, ResNet-50, CLIP or the loudness envelope, turns each file into numbers. The cache key is the file's media_id, the hash of its bytes, plus the encoder, its weights and how files are decoded (size, crop, sample rate). A changed file or a new setting never reuses old numbers, and a file that can't be read stays missing.

04

Does it leak?

Predict

Predict first

You add embeddings to a dataset, then cross-validate a classifier on it. Should the embedding step be refit on each fold's training rows, as a learned vocabulary should?

Yes, refit it per foldNo, the model learns nothing from your rowsOnly if the text mentions the outcome
Reveal

The answer: No, the model learns nothing from your rows

The encoder is frozen: its weights were fixed before it saw your data, and the same text always gets the same vector. Nothing is learned from your rows, so test rows can't leak into it. A text that states the outcome still leaks, but that's true of any feature built from it.

05

Try it

In the app

Add the customer list and start a recipe with Add embeddings on the notes column.

Messy customer list

The step form shows the model's card: download it once (about 91 MB, it asks first). Then preview the step and look at the new columns. Missing notes stay missing.

06

In code

Read
Python
from codesplash.text.lookup import add_embeddings

df = add_embeddings(df, "notes", "notes_emb_")  # 384 columns, computed on this computer
R
df <- codesplashr::cs_text_add_embeddings(df, "notes", "notes_emb_")
07

Check your understanding

Checkpoint

Check yourself

You apply a recipe with Add embeddings, add 50 new rows to the dataset and apply it again. What does the app compute?

Show the answer

Answer: a

The cache is keyed by each text's fingerprint, so only unseen texts run through the model. Repeated texts share one cached vector.

Check yourself

When is TF-IDF + SVD (Text embeddings) the better choice?

Show the answer

Answer: a

TF-IDF compares shared words and learns from your data (so learn it on training rows). A pretrained sentence embedding compares meaning and learns nothing from your rows.

08

Recap

Recap
  • Add embeddings gives 384 meaning-based numbers per text, computed on this Mac.
  • It's a lookup step: cached by text fingerprint and joined in SQL.
  • A frozen encoder needs no per-fold refit; a learned vocabulary does.

Concepts in this lesson