0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Hook: a reply written word by word
Sana's team answers hundreds of messages a day. "Where is my parcel? I ordered a kettle on Monday." Could a network read the message and write a reply, word by word? That needs two skills: reading a whole sequence, and writing a new one, of a different length. An influential early neural design for this, from two thousand fourteen, was elegant but had a hidden weakness. Its fix, a mechanism that lets the writer look back at exactly the right words at every step, turned out to be one of the most important ideas in the history of AI.
It's called attention. Watch the beams of light: as each reply word is written, the model focuses on the question words it needs. Today we build it from scratch, with real numbers. Season Two, Module Nine, final stop: sequence-to-sequence models, and the birth of attention.
1:11The encoder-decoder (seq2seq)
The sequence-to-sequence design uses two recurrent networks, usually LSTMs. The first, the encoder, reads the customer's message one word at a time. Its final hidden state is a single vector that is meant to summarise the whole message: the context vector. The second network, the decoder, starts from that context vector and writes the reply one word at a time. At each step, it outputs a probability for every word in its vocabulary, using softmax, picks one, and feeds that word back in as its next input. It keeps going until it produces a special end-of-sentence token.
This design, introduced in two thousand fourteen, quickly became a leading approach to machine translation.
2:00The bottleneck problem
But there's a weakness. Everything the decoder will ever know about the message has to pass through that one context vector, a fixed list of maybe a few hundred numbers. A five-word question fits comfortably. A forty-word complaint with an order number, a product, two dates and a request has to be crammed into the same small space. Researchers found that these models handled short sentences well but got noticeably worse as sentences grew longer. The context vector was a bottleneck.
2:34Teacher forcing
How is it trained? We show it real messages paired with good replies. At each step of the reply, the decoder predicts the next word, and the loss is the cross-entropy of the correct word, summed across the reply. One practical trick: during training, we feed the decoder the correct previous word at each step, not its own guess. This is called teacher forcing, and it makes training much faster and more stable. The catch is that at prediction time there's no teacher. The model reads its own previous words, and an early mistake can throw off everything after it.
3:14Greedy vs beam search
When writing a reply, how should the decoder choose each word? The simplest way is greedy: always take the single most likely word. Here's a tiny example. For the first word, "Your" has probability zero point five, "The" zero point four. Greedy takes "Your". The best word after "Your" is "parcel", at zero point four, so the reply starts "Your parcel", with a combined probability of zero point five times zero point four: zero point two. Pause and predict: is that the most likely two-word start?
No. If we had started with "The", the next word "parcel" is almost certain, zero point nine, giving zero point four times zero point nine: zero point three six, much higher. Beam search catches this. It keeps the best few partial replies at each step, here two, instead of committing to one, and picks the best complete sequence at the end. In Season Three, we'll meet sampling methods that language models use instead, for more varied writing.
4:32Attention: scores, softmax, context vector
Here's the fix, attention. Instead of giving the decoder one summary vector at the start, let it look back at all of the encoder's hidden states, one for each input word, at every step, and decide which ones matter right now. It works in three small steps, and you know every one of them. Let's use real numbers. The decoder's current state acts as a query: here, one, one. Each input word has an encoder state: "where", "is", "my", "parcel". Step one, score each input word by how well it matches the query, with a dot product: zero point three, zero point one, zero point seven, and one point eight for "parcel".
Step two, turn the scores into weights with softmax: about thirteen percent, eleven percent, nineteen percent, and fifty-eight percent for "parcel". They add up to one. Step three, build a context vector as the weighted average of the encoder states. "Parcel" contributes most. The result is zero point six seven, zero point five five. And the decoder does this again at every single word it writes, with a fresh context vector each time. It's like a translator who glances back at the relevant part of the original sentence before writing each word.
This idea was introduced for translation in two thousand fourteen, and dot-product versions followed soon after.
6:05Alignment maps: seeing attention
A lovely bonus: we can see what the model is looking at. Plot the attention weights as a grid, with the words of the reply down the side and the words of the message across the top, and bright cells show where the model focused for each output word. In translation, these maps often trace a near-diagonal line, bending where the two languages order their words differently. One caution: attention maps are a useful window into a model, but they're not a full explanation of why it produced what it did.
6:39Why attention changed everything
Attention changed everything for three reasons. First, there's no bottleneck: every input word remains available to the decoder. Second, the gradient no longer has to crawl back through every time step. It can flow directly from an output word to whichever input word it attended to, which eases the vanishing gradient problem. And third, it's a soft, differentiable lookup, so the network learns where to look through ordinary backpropagation. Now here's the question that launched the modern era. If attention is doing so much of the work, do we even need the recurrent networks underneath?
What if we built a model out of attention alone, where every word can attend to every other word, all at once, in parallel? In two thousand seventeen, a paper answered that question with its title: "Attention Is All You Need". That's the transformer, and it's the subject of our next module.
7:39Try it yourself: a different query
Let's change the decoder's query to zero, two, so it cares only about the second number in each encoder state. Which word gets the most attention now? Pause and compute the dot products. The scores become zero point two, zero, zero point eight and one point six. After softmax: thirteen percent, eleven percent, twenty-four percent for "my", and fifty-three percent for "parcel". "Parcel" still leads, but "my" has gained attention compared with before. Each time the decoder's state changes, it asks a different question, and attention gives it a different blend of the input.
That's what makes attention so flexible.
8:27Additive vs dot-product attention
There are a few ways to score how well a query matches each input. The original attention used a tiny neural network to compare them, called additive attention. Soon after, researchers showed that a simple dot product works well and is much faster, because it's just matrix multiplication. Transformers use a scaled version of the dot product, which we'll meet in two episodes. They differ only in how similarity is measured; the softmax and the weighted sum are the same.
9:00Attention for summarising and copying
Attention unlocked more than translation. For summarising a long complaint into one line for Sana's dashboard, the decoder can focus on the key sentences. It also made it possible to copy rare words exactly, like an order number or a product name, by attending to them and reproducing them, something earlier models struggled with. And there's a deep connection to search: attention is a soft lookup over a set of items, and in Season Five, we'll see how retrieval systems perform a similar lookup over whole documents.
9:36Evaluating generated replies (BLEU, ROUGE)
How do you judge a generated reply? Translation and summarisation research used overlap metrics, like BLEU and ROUGE, that count words shared with a reference answer. They're quick, but they miss good paraphrases and can reward wrong answers that use the right words, a problem we'll examine closely in Season Four. For Brightcart, what matters is whether the reply has the right status, the right date and the right tone, which calls for checks on facts, human review, and, later in the course, AI judges.
10:14From seq2seq to today's assistants
Here's the lineage that leads to today's AI assistants. Recurrent sequence-to-sequence models in two thousand fourteen. Attention, added the same year. The transformer, built from attention alone, in two thousand seventeen. Then decoder-only language models, trained on vast text, in Season Three. And finally assistants that retrieve documents and use tools, in Season Five. Through all of it, the core ideas from this episode survive: turn the input into vectors, attend to what's relevant, and generate the reply one token at a time.
10:51Common seq2seq mistakes
A few common mistakes. Forgetting to mask padding, so the decoder attends to empty positions. Using plain greedy decoding, which can fall into repetitive loops. Beam search's bias towards short outputs, because every extra word multiplies in another probability below one, which is why beam scores are usually normalised by length. And the gap between training with teacher forcing and generating on your own. Knowing these makes generated text far more reliable.
11:22Why decoders add log-probabilities
In practice, decoders don't multiply probabilities; they add their logarithms. "Your parcel" scores minus one point six one, and "The parcel" scores minus one point zero two, so "The parcel" still wins: the ranking is the same. Why bother? Multiply fifty small probabilities together and the result can become too tiny for a computer to store. Adding logs avoids that. It also makes length normalisation easy: divide the total by the number of words, so longer replies aren't unfairly penalised.
12:00Predict: an alignment map that crosses
Here's an alignment map to predict. We translate "a red mug" into French, "une tasse rouge". Will the bright cells run neatly down the diagonal? Not quite. "Une" lines up with "a", but "tasse" lines up with "mug", and "rouge" looks back to "red". The bright cells cross, because French usually puts this kind of adjective after the noun. Nobody programmed that grammar rule: attention learned to look in the right place from examples alone.
12:36Myth vs Fact
Myth: attention was invented for transformers. Fact: it was invented a few years earlier, to help recurrent sequence-to-sequence models with translation. The transformer's leap was to make attention the main building block, and to drop recurrence entirely.
12:55Recap
Let's recap. A sequence-to-sequence model uses an encoder to read and a decoder to write. A single context vector becomes a bottleneck for long inputs. Teacher forcing speeds up training, and beam search improves on greedy decoding. Attention lets the decoder look back at every encoder state: score them against a query, turn the scores into weights with softmax, and take the weighted average, afresh for every word it writes. One sentence to remember: don't summarise everything up front. Look back at what you need.
13:32Next: tokens and embeddings
Mission complete. Brightcart has a reply generator that reads a customer's message and, at every word it writes, looks back at exactly the words it needs. That completes Module Nine, sequences. And now the final module of this season, the summit of the mountains: transformers. We'll start with a question we've been postponing: how do we turn words into numbers that actually capture their meaning? Tokens and embeddings, next. See you on the map.