Cosine similarity, top-k and a tie
Scores are rounded to four decimals before sorting. Equal scores keep document order.
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
query: get my money back
1.0000 returns
query: how much does it cost
0.7071 shipping
0.7071 helmets
0.2425 payment
query: what time do you open
1.0000 hours
0.3162 battery
query: tell me a joke
no concept words, nothing to compare