A fake embedder: text to numbers
A hand-made, deterministic stand-in so every number can be checked. Real embedding models are learned and far better.
An embedding turns text into a list of numbers so that texts with similar meaning get similar lists. Cosine similarity measures how close two lists point, and top-k returns the best few. Here the embedder is a small fake so every number can be checked by hand.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write normalise(vec) that scales a list of numbers to length 1 (return the list unchanged if its length is 0). Print the result for [3, 4] with 4 decimals.
Write cosine(a, b) and print it to 4 decimals for [1, 1, 0] and [1, 0, 0], and for [1, 0] and [0, 1].
Explain it without notes
What is an embedding and why does it help find a document that shares no words with the question?
Compute the cosine similarity of [1, 0] and [0.6, 0.8], and explain what the number means.
Why must the documents and the question be embedded with the same model?
Why is a tie-breaker needed in top-k, and what would you choose?
Expected output
dimensions: refund, shipping, warranty, repair, hours, helmet, payment, battery, cost
get my money back
[1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
length 1.0000
how much does delivery cost
[0.0000, 0.4472, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.8944]
length 1.0000
tell me a joke
[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]
length 0.0000