Oussama Essamadi

Publications

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.

Publié

Temps de lecture

14 minutes

Article

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:

z=w1x1+w2x2+bz = w_1 x_1 + w_2 x_2 + b
y^={1if z>00otherwise\hat{y} = \begin{cases} 1 & \text{if } z > 0 \\ 0 & \text{otherwise} \end{cases}
  • The inputs (x1x_1, x2x_2) are whatever you know about the situation, as numbers.
  • The weights (w1w_1, w2w_2) say how much each input matters, and in which direction.
  • The bias (bb) says how easily it says yes, whatever the inputs are. Think of it as a threshold.
  • The activation, that “1 if z>0z > 0” 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:

  • x1x_1, is the previous word “good”? (1 or 0)
  • x2x_2, 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” (x1x_1) Before noon (x2x_2) Suggest “morning”? (yy)
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:

error=y−y^w1←w1+error⋅x1w2←w2+error⋅x2b←b+error\begin{gathered} \text{error} = y - \hat{y} \\ w_1 \leftarrow w_1 + \text{error} \cdot x_1 \\ w_2 \leftarrow w_2 + \text{error} \cdot x_2 \\ b \leftarrow b + \text{error} \end{gathered}

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, zz is 0, so it says no, which is right, nothing changes.
  • Row 4 (“good”, before noon), zz is still 0, so it says no, but the answer is yes. The error is 1, so w1w_1, w2w_2 and bb all go up to 1.
  • Second pass, row 1 (neither), now z=1z = 1, 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 w1=2w_1 = 2, w2=1w_2 = 1 and b=−2b = -2. Let’s check:

x1x_1 x2x_2 z=2x1+x2−2z = 2x_1 + x_2 - 2 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 w1w_1 and w2w_2 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, 2x1+x2−2=02x_1 + x_2 - 2 = 0, 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:

x1x_1 x2x_2 yy
0 0 0
0 1 1
1 0 1
1 1 0
XOR's four rows, as points
x₂x₁
  • yes (1)
  • no (0)
The numbers
Seriesx₁x₂
yes (1)01
yes (1)10
no (0)00
no (0)11

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
h1h_1 x1+x2−0.5>0x_1 + x_2 - 0.5 > 0 at least one is on
h2h_2 −x1−x2+1.5>0-x_1 - x_2 + 1.5 > 0 not both are on
y^\hat{y} h1+h2−1.5>0h_1 + h_2 - 1.5 > 0 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 h1h_1 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:

σ(z)=11+e−z\sigma(z) = \frac{1}{1 + e^{-z}}

It squeezes any number into something between 0 and 1. σ(0)\sigma(0) is 0.5, σ(2)\sigma(2) is about 0.88, σ(−2)\sigma(-2) 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”.

The step jumps, the sigmoid slides
outputz
  • sigmoid
  • step
The numbers
Serieszoutput
sigmoid−60.0025
sigmoid−5.50.0041
sigmoid−50.0067
sigmoid−4.50.011
sigmoid−40.018
sigmoid−3.50.0293
sigmoid−30.0474
sigmoid−2.50.0759
sigmoid−20.1192
sigmoid−1.50.1824
sigmoid−10.2689
sigmoid−0.50.3775
sigmoid00.5
sigmoid0.50.6225
sigmoid10.7311
sigmoid1.50.8176
sigmoid20.8808
sigmoid2.50.9241
sigmoid30.9526
sigmoid3.50.9707
sigmoid40.982
sigmoid4.50.989
sigmoid50.9933
sigmoid5.50.9959
sigmoid60.9975
step−60
step00
step01
step61

Then we need a number for how wrong a guess was, the loss. The usual one is cross-entropy:

L={−ln⁡y^if the answer is yes−ln⁡(1−y^)if it’s noL = \begin{cases} -\ln \hat{y} & \text{if the answer is yes} \\ -\ln(1 - \hat{y}) & \text{if it's no} \end{cases}

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 y^\hat{y}, which depends on zz, which depends on the weight:

∂L∂w1=∂L∂y^⋅∂y^∂z⋅∂z∂w1\frac{\partial L}{\partial w_1} = \frac{\partial L}{\partial \hat{y}} \cdot \frac{\partial \hat{y}}{\partial z} \cdot \frac{\partial z}{\partial w_1}

With a sigmoid and cross-entropy, all of that simplifies beautifully:

∂L∂w1=(y^−y) x1∂L∂w2=(y^−y) x2∂L∂b=y^−y\begin{gathered} \frac{\partial L}{\partial w_1} = (\hat{y} - y)\, x_1 \\ \frac{\partial L}{\partial w_2} = (\hat{y} - y)\, x_2 \\ \frac{\partial L}{\partial b} = \hat{y} - y \end{gathered}

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:

  • z=0z = 0, so y^=σ(0)=0.5\hat{y} = \sigma(0) = 0.5, and the right answer is 1, so y^−y=−0.5\hat{y} - y = -0.5, and the loss is 0.69.
  • Each weight moves against its gradient, w1=0−1⋅(−0.5)⋅1=0.5w_1 = 0 - 1 \cdot (-0.5) \cdot 1 = 0.5, same for w2w_2, and bb also ends up at 0.5.
  • Now z=0.5+0.5+0.5=1.5z = 0.5 + 0.5 + 0.5 = 1.5, so y^=σ(1.5)≈0.82\hat{y} = \sigma(1.5) \approx 0.82, 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 y^−y\hat{y} - y, 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, h(1−h)h(1 - h)):

δ=y^−yδh=δ⋅wout⋅h (1−h)\begin{gathered} \delta = \hat{y} - y \\ \delta_h = \delta \cdot w_{\text{out}} \cdot h\,(1 - h) \end{gathered}

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 XOR network's loss, pass after pass
losspasses over the data
The numbers
passes over the dataloss
00.696
250.694
500.693
750.689
1000.676
1250.622
1500.551
1750.524
2000.512
2250.506
2500.502
2750.5
3000.498
3250.497
3500.496
3750.495
4000.494
4250.493
4500.492
4750.49
5000.481
5100.462
5200.407
5250.369
5300.312
5400.177
5500.102
5600.071
5750.049
6000.032
6250.024
6500.019
6750.016
7000.014
7250.012
7500.011
7750.01
8000.009
8250.008
8500.007
8750.007
9000.006
9250.006
9500.006
9750.005
1,0000.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:

What the trained network answers, across the whole square
x₂x₁
The numbers
x₂ ↓ x₁ →00.10.20.30.40.50.60.70.80.91
111110.960.530.060.01000
0.9111110.960.530.060.0100
0.81111110.960.530.060.010
0.711111110.960.530.060.01
0.6111111110.960.530.06
0.50.92111111110.960.53
0.40.180.91111111110.96
0.30.010.170.9111111111
0.200.010.160.911111111
0.1000.010.160.9111111
00000.010.150.911111

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, h1h_1 stays on until x1+x2x_1 + x_2 passes about 1.5 (not both), h2h_2 until it passes about 0.44 (neither), and the output says yes when h1h_1 is on and h2h_2 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.

Suivant

ouLes 21 publicationsouAu hasardouAccueil

ProjetsPublications

Chargement de la liste…

Chargement de la liste…

Raccourcis clavier

Recherche et commandes
Ctrl K⌘K
Passer du clair au sombre
T
Tous les projets
P
Toutes les publications
W
Chercher dans la liste ouverte
S
Accueil
H
M’écrire
C
Afficher ces raccourcis
?
Fermer ce qui est ouvert
esc

Les raccourcis sont activés.