0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Hook: refund vs money back
Two Brightcart customers write, "I want a refund" and "Can I get my money back?" Same request, completely different words. A third asks, "Where's my delivery?", a totally different request. If we just give every word an ID number, "refund" might be number four thousand eight hundred and twenty-one and "money" number one hundred and seventeen. The numbers say nothing about meaning. What we want is something richer: a way to place every word as a point in space, where words with similar meanings sit close together. "Refund" next to "money back", "delivery" somewhere else entirely.
These points are called embeddings, and they're the foundation of every language model. But first we need to decide what a "word" even is. That's tokenisation. Season Two, Module Ten: transformers, the summit of the season. First stop: tokens and embeddings.
1:11What should a token be?
A model reads text as a sequence of pieces called tokens. But what should a piece be? We could use single characters. The vocabulary is tiny, but sentences become very long, and each character carries almost no meaning on its own. We could use whole words. Each piece is meaningful, but the vocabulary becomes enormous, and any word the model hasn't seen, a new product name, a typo, a word like "unboxing", becomes "unknown". Modern language models use a middle path: subwords. Common words stay whole. Rare words are split into familiar pieces: "un", "box", "ing".
The model can read any text, even words it has never seen, by building them from parts.
1:59Byte-pair encoding (BPE) by hand
The most common way to build subword vocabularies is called byte-pair encoding, or B-P-E. Let's do it by hand. Here's a tiny corpus: "low" appears five times, "lower" twice, "newest" six times and "widest" three times. We start by splitting every word into characters, with a marker for the end of each word. Now count every pair of neighbouring symbols, weighted by how often each word appears, and merge the most frequent pair into a new symbol. "T" followed by end-of-word appears nine times, in "newest" and "widest", so it becomes one token.
Next, "s" plus that new token, also nine times. Then "e" plus that: now "est" at the end of a word is a single token. Then "o" with "w", seven times, and "l" with "ow": now "low" is a token. Then "w" with "est". After six merges, "low" is a single token, "newest" is n, e, west, and "widest" is w, i, d, est. And here's the payoff: a word the tokeniser has never seen, like "lowest", is simply read as "low" plus "est". Real tokenisers learn tens of thousands of merges from enormous amounts of text, and many work on raw bytes, so they can represent any character in any language.
3:31Token IDs and special tokens
Once text is split into tokens, each token is replaced by an integer ID from the vocabulary. The model never sees letters at all, only these numbers. Tokenisers also add a few special tokens: markers for the start and end of a sequence, padding to make sequences in a batch the same length, and, in chat models, markers that show who's speaking. Tokens become the unit of everything in language models. How much text a model can read at once is measured in tokens, and, as we'll see in Season Four, so is how much it costs to use.
4:12One-hot vectors vs embedding tables
How do we turn an ID into something a network can work with? The naive answer is a one-hot vector: a vector as long as the vocabulary, all zeros except a single one at that word's position. With fifty thousand tokens, that's fifty thousand numbers per word. Worse, every pair of different words is exactly as different as every other pair. "Refund" is no closer to "money" than it is to "kettle". The solution is an embedding table: a matrix with one row per token, and each row a short, dense vector, say seven hundred and sixty-eight numbers.
Turning a token into a vector is just looking up its row. Mathematically, it's the same as multiplying the one-hot vector by the table. And here's the key: the numbers in the table are learned, adjusted by backpropagation like any other weights, so that words used in similar ways end up with similar vectors. For a fifty-thousand-token vocabulary and seven hundred and sixty-eight dimensions, that's over thirty-eight million learned numbers, just to represent the words.
5:23Word2vec: meaning from context
How do embeddings learn meaning? From a simple observation, captured in a famous line by the linguist J R Firth: "You shall know a word by the company it keeps." Look at Brightcart's reviews. "I asked for a refund for the cracked mug." "They gave my money back for the cracked mug." "Refund" and "money back" appear surrounded by similar words. A word's neighbours reveal what it means. In two thousand thirteen, a method called word2vec turned this into training. It slides a window over huge amounts of text, and trains a small network either to predict a word's neighbours from the word, or the word from its neighbours.
The network's first layer is the embedding table, and to succeed, words that appear in similar contexts must get similar vectors. The result is a map of meaning. In Brightcart's example space, "refund" and "money back" point in almost the same direction, a cosine similarity of zero point nine nine six, while "delivery" points somewhere quite different, zero point two seven. Words about payments cluster together, as do words about shipping and about product quality.
6:38Directions have meaning (king − man + woman)
Embedding spaces have an even stranger property: directions can carry meaning. One of the most famous results reported for word2vec: take the vector for "king", subtract "man", add "woman", and the nearest word is "queen". The direction from "man" to "woman" runs roughly parallel to the direction from "king" to "queen". Similar regularities were found for countries and capitals, and for verb tenses. Two cautions. These analogies only work approximately, and not always. And embeddings learn from human-written text, so they also absorb its biases, for example stereotypes linking certain jobs with certain genders.
Measuring and reducing those biases is an important part of building responsible AI.
7:28One vector per word isn't enough
But there's a problem with one fixed vector per word. "I deposited cash at the bank." "We had a picnic on the river bank." Same word, completely different meanings, yet a static embedding gives "bank" exactly the same vector both times. At Brightcart: "the order was light", meaning not heavy, and "the lamp's light is warm". What we need is for each word's vector to be adjusted by the words around it, so that "bank" near "cash" and "bank" near "river" end up in different places. These are called contextual embeddings, and the mechanism that computes them is self-attention, next episode.
8:13Try it yourself: BPE on new words
Let's learn a tokeniser on a new corpus: "mug" five times, "mugs" three, "hug" four, "hugs" two, and "bug" once. Which pair of characters gets merged first? Pause and count. "U" followed by "g" appears in every single word: fifteen times in total. So "ug" becomes the first new token. Next, "m" plus "ug" appears eight times, making "mug". Then "h" plus "ug", six times, making "hug". Then "mug" plus "s", three times, making "mugs". Look at the result. The frequent words, "mug", "mugs" and "hug", are now single tokens.
"Hugs" is "hug" plus "s". And the rare word "bug" is built from pieces: "b" plus "ug". That's exactly the balance BPE is designed to strike.
9:17How many tokens? Counting in practice
Why should you care about tokens? Because models measure everything in them: how much text fits in their context, and, for many services, what you pay. For English, with many current tokenisers, a token is roughly four characters, or about three quarters of a word, though it varies. And here's a fairness issue: tokenisers trained mostly on English text often split other languages into more pieces. The same message in Hindi or Arabic can cost more tokens, which means more money and less room in the context. A quick note: names, prices and rankings in AI change fast.
Everything on screen was accurate when this video was made, but some of it may have changed by the time you're watching.
10:05Tokenizer quirks that surprise people
Tokenisation explains some of the strangest things language models do. In many tokenisers, a space belongs to the following word, so "mug" with a space in front is a different token from "mug" without one. Capital letters can change how a word is split. A model asked to count the letters in "strawberry" doesn't see ten letters; it sees a few chunks, which is why such simple questions used to trip them up. And long numbers get broken into uneven pieces, one reason arithmetic was hard for early models.
10:42Predict: nearest neighbours in embedding space
Here are three product words as vectors: kettle and teapot are nearly identical, with a cosine of zero point nine nine six, while refund is far away. Now a new word arrives: "cup". Which is its nearest neighbour? Pause. Teapot, at zero point eight six, just ahead of kettle at zero point eight two, with refund far behind at zero point five four. Finding the nearest vectors is how you search by meaning instead of exact words, and in Season Five, it's the heart of how AI assistants find the right documents.
11:28Where the parameters live (weight tying)
Here's a surprise. An embedding table for fifty thousand tokens, at seven hundred and sixty-eight numbers each, holds thirty-eight point four million parameters. That's more than twice the six transformer layers we'll build in two episodes' time. In small models, the vocabulary can be the biggest single part. That's why many models reuse the same matrix at the end, to turn their final vectors back into scores for every token, a trick called weight tying, which avoids storing it twice.
12:00Embeddings learn our biases too
Embeddings learn from text, and text reflects society, including its stereotypes. Researchers have shown that word embeddings can place certain jobs closer to one gender than another, simply because of patterns in the training data. Those biases can then flow into search results or recommendations. So responsible teams audit embeddings, balance their data where they can, and test outputs across different groups, topics we'll return to in Seasons Four and Five.
12:31Predict: how many tokens is "refunded"?
How many tokens is the word "refunded"? It depends on the tokeniser. Pause and think about all three. As characters, it's eight tokens: long sequences, but nothing is ever unknown. As whole words, it's one token, but every variation, like "refundable" or "refunding", needs its own entry, and a typo becomes unknown. As subwords, it might be "refund" plus "ed": two tokens, sharing "refund" with all its relatives. Subwords sit in the sweet spot, and choosing the vocabulary size is a trade-off between shorter sequences and a bigger embedding table.
13:17Myth vs Fact
Myth: language models read words the way we do. Fact: they see token IDs, turned into learned vectors. Words can be split in surprising ways, which is one reason language models sometimes stumble on tasks that seem trivial to us, like counting the letters in a word.
13:36Recap
Let's recap. Text is split into subword tokens, using methods like byte-pair encoding, which merges the most frequent pairs of symbols. Each token gets an ID. One-hot vectors are huge and treat all words as equally different, so instead we use an embedding table: a learned vector for every token. Trained on context, similar words end up close together, and directions can even carry meaning. But a single vector per word can't capture context, which is what attention will fix. One sentence to remember: chop text into tokens, and place each token in a space of meaning.
14:18Next: self-attention
Mission complete. Brightcart's messages are now tokens, and every token is a point in a space of meaning, where "refund" and "money back" sit side by side. Next, the mechanism at the very heart of modern AI. Every word gets to look at every other word in the sentence and update its meaning accordingly. Queries, keys and values: self-attention, built step by step, with real numbers. See you on the map.