مقالاتي
Autocomplete is prediction
Everyone this year says LLMs are just expensive autocomplete. They're right about the autocomplete part. What that actually means, from a single perceptron up, with the data, the math and backpropagation worked out by hand.
نُشر
مدة القراءة
14 دقيقة
المقال
ChatGPT turned one at the end of November, and there’s one line I keep hearing about it, at work, in comment sections, from friends, “LLMs are just expensive autocomplete” (sometimes it’s “slow autocomplete”, sometimes “glorified”, depends on how annoyed the person is). And honestly, they’re not wrong about the autocomplete part. These models really do one thing, guess the next word, over and over.
What I don’t agree with is the “just”. Autocomplete is prediction, and prediction is a lot more than it sounds like. So in this post, I want to open the box, start from the smallest piece there is, a single perceptron, and build up to what happens when ChatGPT picks its next word. There’s some math, but nothing past multiplying and adding, I promise (well, almost).
Autocomplete, thanks to statistics
Type “good” on your phone, and the keyboard suggests “morning”, “night” or “luck”. There’s no magic in there, it counted. In all the text it learned from, and everything you’ve typed, those are the words that come after “good” the most. That’s statistics, plain counting.
And counting gets you surprisingly far. Back in 1948, Claude Shannon generated “English” using nothing but which word tends to follow which, and got things like this:
THE HEAD AND IN FRONTAL ATTACK ON AN ENGLISH WRITER THAT THE CHARACTER OF THIS POINT IS THEREFORE ANOTHER METHOD FOR THE LETTERS THAT THE TIME OF...
Every pair of words in there makes sense, and the whole thing makes none. That’s autocomplete with only the previous word to go on.
The problem is that counting doesn’t scale. To predict the next word of a whole sentence, you’d have to count every sentence that could ever be written, and most of them were never written down anywhere. What you need is something that learns the patterns from what it has seen, and applies them to what it hasn’t. That’s what a neural network does, and its smallest piece is the perceptron.
The perceptron
Frank Rosenblatt came up with it in 1958, and it’s honestly simple. It takes a few numbers in, multiplies each one by a weight, adds them up with a bias, and decides:
- The inputs (, ) are whatever you know about the situation, as numbers.
- The weights (, ) say how much each input matters, and in which direction.
- The bias () says how easily it says yes, whatever the inputs are. Think of it as a threshold.
- The activation, that “1 if ” step at the end, turns the sum into a decision.
That’s it, a weighted vote with a threshold. The interesting part is that nobody sets the weights by hand, the perceptron learns them from data.
A tiny autocomplete
Let’s build the smallest autocomplete possible, one that decides whether to suggest “morning”. It gets two inputs:
- , is the previous word “good”? (1 or 0)
- , is it before noon? (1 or 0)
And here’s the data it learns from, it should only suggest “morning” when both are true:
| Previous word is “good” () | Before noon () | Suggest “morning”? () |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Rosenblatt’s learning rule is just as simple as the perceptron itself. For each row, make a guess, compare it with the right answer, and nudge the weights by the difference:
Read the arrows as “becomes”. If the guess was right, the error is 0 and nothing changes. If it said no when it should’ve said yes, the error is 1, and every weight whose input was on goes up, so next time, the sum comes out higher. And the other way around.
Let’s run it, starting with every weight and the bias at 0:
- Rows 1 to 3, is 0, so it says no, which is right, nothing changes.
- Row 4 (“good”, before noon), is still 0, so it says no, but the answer is yes. The error is 1, so , and all go up to 1.
- Second pass, row 1 (neither), now , so it says yes, wrong. The error is −1, and since both inputs are 0, only the bias moves, down to 0.
And so on. By the sixth pass over the data, it doesn’t make a single mistake anymore, with , and . Let’s check:
| Suggests “morning”? | |||
|---|---|---|---|
| 0 | 0 | −2 | No |
| 0 | 1 | −1 | No |
| 1 | 0 | 0 | No |
| 1 | 1 | 1 | Yes |
Notice the bias doing its job. Without it, saying no to the two middle rows would need and both at 0 or below, and then the last row couldn’t add up to more than 0 either, so it could never say yes. The bias is what lets the perceptron say “not unless I have a good reason”.
If you plot the four rows as points, what the perceptron learned is a straight line, , with the “yes” point on one side and the three “no” points on the other. And that’s all a single perceptron can ever do, draw one straight line.
Where one perceptron gives up
Now change the rule, say the answer should be yes when exactly one of the two inputs is on, but not both. That’s XOR:
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
- yes (1)
- no (0)
The numbers
| Series | x₁ | x₂ |
|---|---|---|
| yes (1) | 0 | 1 |
| yes (1) | 1 | 0 |
| no (0) | 0 | 0 |
| no (0) | 1 | 1 |
Try drawing one straight line that puts the two yes points on one side and the two no points on the other. You can’t, and neither can the perceptron, no matter how long it trains. Marvin Minsky and Seymour Papert made a big deal of exactly this in their 1969 book Perceptrons, and the excitement around perceptrons cooled off for years after.
The fix is to stack them. Put two perceptrons side by side, then a third one on top that reads their answers:
| Neuron | Says 1 when | In words |
|---|---|---|
| at least one is on | ||
| not both are on | ||
| both of the above |
At least one, and not both, that’s exactly XOR. The two in the middle are called a hidden layer, and with layers, a network can draw as many lines as it needs, and combine them into any shape.
But I picked those weights by hand. With three neurons that’s fine, with millions, nobody can, so the network has to learn them, and here Rosenblatt’s rule hits a wall. It only works when you know the right answer for a neuron, and for the hidden ones, nobody does. The data says what the output should be, it says nothing about what should be.
How wrong, and which way
The first problem is the step at the end. It’s either 0 or 1, so a small change in a weight usually changes nothing at all, and that tells you nothing about which way to move it. So we swap it for something smooth, the sigmoid:
It squeezes any number into something between 0 and 1. is 0.5, is about 0.88, about 0.12. Now the output isn’t a yes or a no anymore, it’s how sure the network is, and that’s exactly what a keyboard needs, “I’m 88% sure the next word is morning”.
- sigmoid
- step
The numbers
| Series | z | output |
|---|---|---|
| sigmoid | −6 | 0.0025 |
| sigmoid | −5.5 | 0.0041 |
| sigmoid | −5 | 0.0067 |
| sigmoid | −4.5 | 0.011 |
| sigmoid | −4 | 0.018 |
| sigmoid | −3.5 | 0.0293 |
| sigmoid | −3 | 0.0474 |
| sigmoid | −2.5 | 0.0759 |
| sigmoid | −2 | 0.1192 |
| sigmoid | −1.5 | 0.1824 |
| sigmoid | −1 | 0.2689 |
| sigmoid | −0.5 | 0.3775 |
| sigmoid | 0 | 0.5 |
| sigmoid | 0.5 | 0.6225 |
| sigmoid | 1 | 0.7311 |
| sigmoid | 1.5 | 0.8176 |
| sigmoid | 2 | 0.8808 |
| sigmoid | 2.5 | 0.9241 |
| sigmoid | 3 | 0.9526 |
| sigmoid | 3.5 | 0.9707 |
| sigmoid | 4 | 0.982 |
| sigmoid | 4.5 | 0.989 |
| sigmoid | 5 | 0.9933 |
| sigmoid | 5.5 | 0.9959 |
| sigmoid | 6 | 0.9975 |
| step | −6 | 0 |
| step | 0 | 0 |
| step | 0 | 1 |
| step | 6 | 1 |
Then we need a number for how wrong a guess was, the loss. The usual one is cross-entropy:
Being 50% sure costs 0.69, being 99% sure and right costs 0.01, and being 99% sure and wrong costs 4.6. Confidently wrong is punished hard, which is the point.
Training now means bringing that loss down, and for that we need to know, for each weight, which way to move it to make the loss smaller. That’s the gradient, and we get it with the chain rule, the loss depends on , which depends on , which depends on the weight:
With a sigmoid and cross-entropy, all of that simplifies beautifully:
Look familiar? It’s Rosenblatt’s rule again, except the error isn’t just 0 or 1 anymore, it’s how far off the guess was. Let’s try it on our “good morning” row, with everything starting at 0 and a learning rate of 1:
- , so , and the right answer is 1, so , and the loss is 0.69.
- Each weight moves against its gradient, , same for , and also ends up at 0.5.
- Now , so , and the loss drops to 0.20.
One step, and the network went from a coin flip to 82% sure. Do that over and over, across all the rows, and that’s gradient descent.
Backpropagation
Now the hidden layer. The trick is to keep applying the chain rule, backwards through the network. The output’s error is , like before. Each hidden neuron then gets its share of the blame, depending on how much it weighs in the output (its weight to the output) and how much its own output could still move (the sigmoid’s slope, ):
The first is the output’s error, the second is a hidden neuron’s share of it.
And every weight’s gradient is then the error of the neuron it feeds into, times the input that came through it. Errors flow backwards, from the output to the first layer, which is why it’s called backpropagation. The idea had been around in different forms, but it was the 1986 paper by David Rumelhart, Geoffrey Hinton and Ronald Williams that made it stick, and it’s still how pretty much every neural network is trained today.
Here’s the whole thing, a network with two inputs, two hidden neurons and one output, learning XOR from scratch:
import math
data = [
([0, 0], 0),
([0, 1], 1),
([1, 0], 1),
([1, 1], 0),
]
# Two hidden neurons, each with a weight per input and a bias, then one output neuron.
hidden = [
{"w": [0.5, -0.4], "b": 0.1},
{"w": [-0.3, 0.6], "b": -0.1},
]
output = {"w": [0.7, -0.5], "b": 0.0}
rate = 1
def sigmoid(z):
return 1 / (1 + math.exp(-z))
def forward(x):
h = [sigmoid(n["w"][0] * x[0] + n["w"][1] * x[1] + n["b"]) for n in hidden]
out = sigmoid(output["w"][0] * h[0] + output["w"][1] * h[1] + output["b"])
return h, out
for _ in range(10_000):
for x, y in data:
h, out = forward(x)
# Backward: how wrong the output was, then each hidden neuron's share of it.
delta = out - y
deltas = [delta * output["w"][i] * h[i] * (1 - h[i]) for i in range(2)]
# Every weight takes a small step against its gradient.
output["w"] = [w - rate * delta * h[i] for i, w in enumerate(output["w"])]
output["b"] -= rate * delta
for i, n in enumerate(hidden):
n["w"] = [w - rate * deltas[i] * x[j] for j, w in enumerate(n["w"])]
n["b"] -= rate * deltas[i]
for x, y in data:
print(x, y, f"{forward(x)[1]:.4f}")
Run it, and you get this:
[0, 0] 0 0.0002
[0, 1] 1 0.9998
[1, 0] 1 0.9998
[1, 1] 0 0.0003
The thing a single perceptron could never learn, learned, and nobody told the hidden neurons what to do. It didn’t happen smoothly either, here’s the loss, averaged over the four rows, as it trains:
The numbers
| passes over the data | loss |
|---|---|
| 0 | 0.696 |
| 25 | 0.694 |
| 50 | 0.693 |
| 75 | 0.689 |
| 100 | 0.676 |
| 125 | 0.622 |
| 150 | 0.551 |
| 175 | 0.524 |
| 200 | 0.512 |
| 225 | 0.506 |
| 250 | 0.502 |
| 275 | 0.5 |
| 300 | 0.498 |
| 325 | 0.497 |
| 350 | 0.496 |
| 375 | 0.495 |
| 400 | 0.494 |
| 425 | 0.493 |
| 450 | 0.492 |
| 475 | 0.49 |
| 500 | 0.481 |
| 510 | 0.462 |
| 520 | 0.407 |
| 525 | 0.369 |
| 530 | 0.312 |
| 540 | 0.177 |
| 550 | 0.102 |
| 560 | 0.071 |
| 575 | 0.049 |
| 600 | 0.032 |
| 625 | 0.024 |
| 650 | 0.019 |
| 675 | 0.016 |
| 700 | 0.014 |
| 725 | 0.012 |
| 750 | 0.011 |
| 775 | 0.01 |
| 800 | 0.009 |
| 825 | 0.008 |
| 850 | 0.007 |
| 875 | 0.007 |
| 900 | 0.006 |
| 925 | 0.006 |
| 950 | 0.006 |
| 975 | 0.005 |
| 1,000 | 0.005 |
For a few hundred passes, it’s stuck answering about 0.58 for all three rows that have an input on, basically OR, then around pass 520, the hidden layer finds the split and the loss falls off a cliff.
One heads-up if you play with it though, change the starting weights and it can stay stuck for good, with two of the rows around 0.5 (XOR is famous for that). With these ones, it gets out.
And since the trained network is just a function, nothing stops you from asking it about inputs that aren’t 0 or 1. Here’s what it answers across the whole square, the stronger the color, the surer the yes:
The numbers
| x₂ ↓ x₁ → | 0 | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 1 | 1 | 1 | 1 | 0.96 | 0.53 | 0.06 | 0.01 | 0 | 0 | 0 |
| 0.9 | 1 | 1 | 1 | 1 | 1 | 0.96 | 0.53 | 0.06 | 0.01 | 0 | 0 |
| 0.8 | 1 | 1 | 1 | 1 | 1 | 1 | 0.96 | 0.53 | 0.06 | 0.01 | 0 |
| 0.7 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0.96 | 0.53 | 0.06 | 0.01 |
| 0.6 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0.96 | 0.53 | 0.06 |
| 0.5 | 0.92 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0.96 | 0.53 |
| 0.4 | 0.18 | 0.91 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0.96 |
| 0.3 | 0.01 | 0.17 | 0.91 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 0.2 | 0 | 0.01 | 0.16 | 0.91 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| 0.1 | 0 | 0 | 0.01 | 0.16 | 0.9 | 1 | 1 | 1 | 1 | 1 | 1 |
| 0 | 0 | 0 | 0 | 0.01 | 0.15 | 0.9 | 1 | 1 | 1 | 1 | 1 |
A band of yes across the middle, and no in the two corners. The edges of that band are the two lines the hidden neurons learned, and they’re not the ones I picked by hand. Here, stays on until passes about 1.5 (not both), until it passes about 0.44 (neither), and the output says yes when is on and is off. Same XOR, different wiring, and nobody drew those lines, the training found them.
Back to autocomplete
So what does any of this have to do with ChatGPT? Pretty much everything, it’s the same thing, only bigger:
- The inputs aren’t two yes-or-no questions anymore, they’re all the words so far, turned into numbers.
- The output isn’t one neuron, it’s one score for every word the model knows (tens of thousands of them), and a softmax, the many-answers version of the sigmoid, turns those scores into probabilities.
- The weights aren’t nine, GPT-3 has 175 billion of them.
- The training is the same loop as above, guess the next word, measure how wrong the guess was with cross-entropy, backpropagate, nudge every weight a little. Repeat over a huge chunk of the internet, Meta’s Llama 2 for example was trained on 2 trillion tokens.
And what it learns, thanks to statistics, are the patterns of text. Your phone counted which word comes after “good”. An LLM learns which word comes after half a function, a question, an argument, a joke, from more text than anyone could ever read, with patterns no counting could ever cover.
So, is it just autocomplete?
Mechanically, yes, and I don’t think that’s worth denying. Every word ChatGPT writes is the next word it predicted.
But the “just” hides how much the prediction asks for. To autocomplete “good” you need one pair of words. To autocomplete the next line of a function, you need to follow what the function does, and to autocomplete the end of an argument, you need to follow the argument. The better the prediction, the more of the text’s structure the model has to pick up along the way.
It also explains where it goes wrong. It predicts the likely next word, not the true one, and when the truth is rare, the likely continuation is a confident answer that sounds just right, which is exactly what I ran into in its first three weeks. The model isn’t lying, it’s doing what it was trained to do.
So next time someone tells you it’s just autocomplete, you can agree with them, and then ask what it takes to autocomplete well.
Links
- The Perceptron: A Probabilistic Model for Information Storage and Organization in the Brain (opens in a new tab), Frank Rosenblatt, 1958
- A Mathematical Theory of Communication (opens in a new tab), Claude Shannon, 1948
- Learning representations by back-propagating errors (opens in a new tab), Rumelhart, Hinton and Williams, 1986
- Language Models are Few-Shot Learners (opens in a new tab), the GPT-3 paper
- Llama 2: Open Foundation and Fine-Tuned Chat Models (opens in a new tab)
التالي
SA:MP after sampctl and open.mpأوكل مقالاتي (21)أوصفحة عشوائيةأوالرئيسية