Go to Map Courses Lab Glossary How it works Troubleshooting Start →
ITEN
← Course 05 · Understanding language (Lesson 05.02 · 22 minutes · Keyboard · First model)

Classifying and searching text on the phone.

Turn on the light, what's the weather tomorrow, put on some music. Here you'll teach the phone to understand what a sentence is asking for, with a ready-made model and a few examples of your own, and you'll use the same trick to search your notes by meaning.

What you learn
  1. How to build a sentence classifier with just a few examples
  2. When finding the closest example sentence is enough
  3. How search by meaning works, and where it goes wrong
How do you want to follow it
Preparing the scene…
  1. 01 The question

    Understanding what a sentence asks for, among a few answers.

    You want an app that understands written commands. Turn on the light, what's the weather tomorrow, put on some music. The possible sentences are endless, but the intents are few, light, weather, music and other. Recognizing the intent is a kind of text classification01.04. Each sentence goes into one of your classes.

    In practice. Choose intents that are clearly separate. If you mix up two classes yourself when you read the sentences, the model will mix them up even more.

01 / 07
(Go deeper)

A few more numbers.


(01)

Why the vocabulary weighs so much

A multilingual model like paraphrase-multilingual-MiniLM has a vocabulary of about 250,000 tokens, to cover dozens of languages. Each token has an embedding of 384 numbers. That makes 96 million weights just for the vocabulary, out of about 118 million in total.

So the part that understands sentences, twelve small layers, weighs less than a fifth of the total. That's why a model for a single language, with a vocabulary of 30,000 tokens, can weigh three or four times less and stay almost as good in that language.

  • 250,000tokens in the vocabulary, roughly
  • 96 Mweights just for the vocabulary
  • 118 Mweights in total
(02)

The classifier in code

If you write code, all of this fits in a few lines. The examples get turned into embeddings once and saved. For each new sentence you compute its embedding, compare it with all the examples, and take the intent of the most similar one, if it's similar enough.

With a few hundred examples the comparison takes less than a millisecond. With tens of thousands of notes you use special structures called vector indexes, which find the neighbors without comparing them all.

const e = await body(sentence);                     // 384 numbers
let best = { sim: -1, intent: "other" };
for (const ex of examples) {
  const sim = cosine(e, ex.embedding);
  if (sim > best.sim) best = { sim, intent: ex.intent };
}
const answer = best.sim > 0.6 ? best.intent : "other";
(03)

Typical mistakes

Sentence embeddings mostly capture the topic. "Turn on the light" and "don't turn on the light" talk about the same thing, and they end up very close. A classifier built only on embeddings may turn on the light for someone who asked for the opposite.

If negations matter in your problem, put them in the examples as separate classes, or add a simple check for words like "not" and "never". The same goes for irony, which even big models often miss.

(Try it on your phone)

Your commands


In the lab you build a classifier for written commands, with four intents you choose. It takes ten minutes and, if you can, a second person to write test sentences.

  1. Open the lab with the button below. It starts with the Text input already chosen, and the first time it downloads the sentence model.
  2. Create four classes, for example light, weather, music and other.
  3. Write 10 sentences per class, all different. In the other class, put sentences that have nothing to do with the rest.
  4. Press Train. Then in Use, type new sentences and look at the class and the closest example sentence.
  5. Try a negation, like "don't turn on the light", and see what happens.
Open the lab →
(Quiz)

4 questions to close the lesson.

When you have answered them all, the lesson counts as done and its stone on the map turns yellow. Answering is what counts, not the score.


  1. (01)

    Why are ten or twenty sentences per intent enough?

  2. (02)

    You type "work meeting" and find "appointment with the boss at three". Why?

  3. (03)

    You type "don't turn on the light" and the model answers light. Why does that happen?

  4. (04)

    Where is most of the weight in a multilingual sentence model?

To close the lesson, answer the quiz

← Words become numbers Back to the map
(Next lesson · 05.03) A language model in your pocket