In this lesson: how an embedding model turns a whole sentence into one list of numbers, how two of those lists are compared, and why "close" does not mean "the same meaning", let alone "true". It is the step behind semantic search: searching notes by meaning, not by exact words. Read the lesson and take the 5-question quiz: each question has a hint if you get stuck, and its explanation shows once you get it right (all of them at full marks). Pass the quiz to finish the lesson and earn its badge.
1. Which sentence is closest?
We compared "I forgot my maths homework at home." with four other sentences, using a small embedding model called MiniLM. Guess first: which of these four will score highest against the homework sentence? "My maths homework is still sitting on my desk at home.", "I did not forget my maths homework.", "I forgot my PE kit at home." or "The canteen sells noodles on Fridays."
MiniLM turned each of the five sentences into a list of 384 numbers, then scored the four against the homework sentence:
| Sentence | Score |
|---|
| "My maths homework is still sitting on my desk at home." | 0.76 |
| "I did not forget my maths homework." | 0.82 |
| "I forgot my PE kit at home." | 0.26 |
| "The canteen sells noodles on Fridays." | 0.05 |
In order: the same meaning in other words; the opposite; a forgotten PE kit; the canteen. The surprise: "I did not forget my maths homework.", which says the opposite, scored 0.82, higher than the sentence that really means the same thing (0.76). The rest of the lesson explains why.
The opposite sentence scored highest: 0.82
2. A sentence becomes 384 numbers
An embedding is a list of numbers, a vector, that stands for a piece of text. MiniLM reads the tokens (see the lesson on tokens, H1) and gives every token a vector of 384 numbers that depends on the words around it. To get one vector for the whole sentence, we take the average of the token vectors. That is mean pooling: add the token vectors up, then divide by how many there are. Sentences given to the model together must have the same length, so short ones get padding, empty places at the end. A mask marks which tokens are real, so the padding is left out of the average. A sentence of 5 words and one of 50 both end up as 384 numbers.
Think of each vector as an arrow pointing somewhere in a 384-dimensional space. The model was fine-tuned (trained further) on over a billion sentence pairs that belong together, such as a question and its answer, so that sentences used in similar ways point in similar directions.
A note on the model: MiniLM was trained on English. Its tokenizer turns most Chinese characters into [UNK] ("unknown"): "我的水瓶丢了" (I lost my water bottle) becomes 我, 的, 水 and three [UNK]s. So it can't compare Chinese sentences. That is why every sentence in this lesson is in English. Models trained on many languages work the same way, in each language they learned.
Average the token vectors: one 384-number vector
3. Comparing directions: cosine similarity
Cosine similarity measures the angle between two arrows: 1 means the same direction, 0 means at right angles (the canteen sentence, at 0.05, is nearly there), -1 means pointing the opposite way. Pointing the opposite way is not the same as meaning the opposite: section 4 explains why a sentence that says the opposite still points close by.
In words: multiply the two lists number by number and add the results up (the dot product), then divide by the two arrows' lengths (how long each arrow is). In maths, cos(a,b)=∥a∥∥b∥a⋅b. For the homework sentence and "I did not forget my maths homework." this gives the 0.82 of section 1. Every MiniLM vector here was first scaled to length 1 (normalised), so dividing by the lengths changes nothing: the cosine of two sentences is just their dot product.
Cosine compares the angle between two arrows
4. Similar is not the same
Why did "did not forget" score so high? It shares the topic, nearly all the words and the sentence shape: "maths homework", "forget", "my". The vector is a summary of what a sentence is about and how it is said, and the "not" that flips the meaning is one small word in all of that. Two more pairs show the same thing:
- "The PE test is on Monday." against "The PE test is on Friday." scores 0.94: one word apart, a different fact, almost the same arrow.
- Against "We run the fitness test at the start of the week.", the same fact in other words, it scores only 0.53.
A vector records what a sentence is about, not whether it is true. Nothing in 384 numbers checks facts.
So two explanations you might read are wrong. "A score of 0.82 means the two sentences are 82 % the same in meaning." The opposite sentence scored 0.82, so the score can't be a percentage of meaning; it is how close two directions are. "Because vectors capture meaning, the notes a vector search finds are always correct." The next section finds a note that contradicts the question. To test any claim like these, swap one word or add a "not" and see how far the score moves.
Close in direction, different in fact
5. A search, and why scores are relative
A search is then simple: embed the notes once (turn each into its vector), embed the question, score it against every note with cosine similarity, sort. We tried it on six short school notes: "The PE test is on Monday morning.", "Bring a calculator for the physics exam.", "Maths homework: page 42, questions 1 to 10.", "Lost: a blue water bottle near the gym.", "The canteen has a new vegetarian menu." and "Choir practice moved to the music room." For three questions, the two notes that scored highest:
| Question | Top match | Second |
|---|
| "Where can I find my bottle?" | 0.50 "Lost: a blue water bottle near the gym." | 0.24 "The canteen has a new vegetarian menu." |
| "Is there homework for maths?" | 0.38 "Bring a calculator for the physics exam." | 0.32 "Maths homework: page 42, questions 1 to 10." |
| "When is the PE test? It is on Friday." | 0.89 "The PE test is on Monday morning." | 0.33 "Bring a calculator for the physics exam." |
Three cases: a good match (the bottle); a miss, where the physics calculator beats the maths homework by a few hundredths; and a match that contradicts the question (Friday asked, Monday found, at 0.89). Even the right bottle note scores only 0.50, so one score means little on its own. Compare scores with each other, on your own data, and look at the matches.
Here is what that means when you build. A notice-board helper has "Bring your swimsuit tomorrow.", posted on Monday, and the newer "Swimming is cancelled tomorrow." Someone asks "Do I need my swimsuit tomorrow?" The old note scores 0.84 and the newer one only 0.48. A tool that answers from the top match alone gives the wrong answer. Show the person the top few notes with their dates, or put the newest first among the top few, and let a person draw the conclusion.
A search finds what is close. Whether it is right is still yours to check.
The top match isn't the answer: read them
Do it yourself in the ML Lab Lost and Found Search, step The sure-match line.