Beneath Generative AI: Talking Machines

What comes next?

Hey, what are |
Hey, what are you doing tonight?

A very small language model

Alice was beginning to get very tired.

Alice was getting very sleepy.

The White Rabbit was late.

The White Rabbit was very late.

Alice and the Rabbit were friends.

After Alice

Next wordCountProbability
Total occurrences3
Pair count÷Previous-word count=Probability

Text becomes tokens

Start with a short piece of text.

Learn merges from training text

alice was beginning to get very tired of sitting by her sister on the bank , and of having nothing to do : once or twice she had peeped into the book her sister was reading , but it had no pictures or conversations in it , " and what is the use of a book , " thought alice " without pictures or conversations ? "

i + n8in the whole paragraph
beginning× 2
sitting× 1
having× 1
nothing× 1
into× 1
reading× 1
in× 1
2 in our excerpt + 6 elsewhere
alice was beginning

Numbers with learned meaning

A token ID selects a row in an embedding table.

One row per vocabulary item

34 tokens × 4 dimensions
Token aID 0E[0]
IDTokenx0x1x2x3
0a0.0030-0.00700.01200.0010
1l-0.00800.0050-0.00200.0090
…
6b0.7381-0.2512-0.67190.7938
…
Selected vector
0.003-0.0070.0120.001

Give each token a position

Same words. Who is following whom?

Two occurrences of the same token
a0l1ic2e3w4a5s6

a at position 0

Same learned embedding
0.003-0.0070.0120.001
+
Position vector P[0]
0.0001.0000.0001.000
=
Input to attention
0.0030.9930.0121.001

a at position 5

Same learned embedding
0.003-0.0070.0120.001
+
Position vector P[5]
-0.9590.2840.0500.999
=
Input to attention
-0.9560.2770.0621.000

The embedding alone does not tell these two occurrences apart.

Context changes a token

Alice put the book down because she …

AliceputthebookdownbecausesheA context-dependent representation

Queries, keys & values

A query asks. Keys describe what is available.

QQueryWhat am I looking for?
KKeyStarch base
↓Match → softmax
↓
VValuePasta from the book’s ingredient analogyPasta
KKeyRich acidity
↓Match → softmax
↓
VValueTomato from the book’s ingredient analogyTomato
KKeySlight spice
↓Match → softmax
↓
VValueBasil from the book’s ingredient analogyBasil
VWeighted values

More than one perspective

The same ingredients can become different dishes.

FlourTomato from the book’s ingredient analogyTomatoCheeseBasil from the book’s ingredient analogyBasil
Chef 1 from the book’s attention-head illustration

Chef 1 · Head 1

“I want an Italian-style dish.”

Tomato & basil pasta
Each head’s learned perspective
WQ1WK1WV1
Flour30%
Tomato25%
Cheese10%
Basil35%
Head 1 output · a different mix of the same values
Chef 2 from the book’s attention-head illustration

Chef 2 · Head 2

“I want a New York-style dish.”

New York-style pizza
Each head’s learned perspective
WQ2WK2WV2
Flour35%
Tomato25%
Cheese35%
Basil5%
Head 2 output · a different mix of the same values
Keep both perspectives · concatenate
Italian-style
New York-style
Mix with WOLearned output projection
One richer representation

Inside a feed-forward network

Four input coordinates.

Four driving measurements enter three illustrative hidden neurons: speed and terrain, speed and visibility, and terrain and distance driven. Their outputs feed one collision prediction.Scaled inputsHidden neuronsPredictionWeight 1Weight 1Weight 0Weight -0.6Weight 0Weight -0.4Weight 0Weight -0.8Weight 0Weight 0Weight 0Weight -0.8Weight 1.2Weight 1Weight 0.8Speed40 mph→0.571Terrain quality5 / 10→0.500Visibility180°→0.500Distance driven50k miles→0.167?0.471Speed + terrain?0.271Speed + visibility?0.067Terrain + distance driven?59.6%Collision probability
Speed40 mph0.571
Terrain quality5 / 100.500
Visibility180°0.500
Distance driven50k miles0.167
↓
?0.471
Speed + terrain
?0.271
Speed + visibility
?0.067
Terrain + distance driven
↓
Collision probability?59.6%

Could these conditions lead to a collision?

Convert four familiar measurements into four numbers.

Learning from the next token

The training text supplies the target.

Four input coordinates x0 to x3 from a plus position 0, three hidden neurons and four output features, followed by a separate vocabulary head.Input vectorHidden neuronsFeature vectorVocabulary head× 0.5× 1× -0.5× 10.003x00.993x10.012x21.001x3?1.4894999999999998?0?1.0194999999999999?1.7642499999999997?0.97?0.6321249999999998?0.27474999999999994l?ic?e?Feed-forward networkScores → softmax → probabilities

The same four coordinates

a’s embedding + position 0 → x₀, x₁, x₂, x₃.
Each connection multiplies one coordinate by a weight.

Neurons transform the result

A hidden neuron adds its inputs and a bias, then applies an activation function.

One decision. Then another.

Compute a distribution over possible next tokens.

Alice was |

Sample one token using these probabilities, then append it.

You now know how LLMs work.

At the very least

Use them better.

At the most

Build on them.

Beneath Generative AI:

Dreaming in Pixels

How does noise become an image?

Noise
Refine
Image

Explore the algorithms

Grokking AI Algorithms, Second Edition book cover

Grokking AI Algorithms

A visual guide to the ideas behind AI.

grokkingaialgorithms.com

Join the community

AIZA

AIZA

Practical AI talks, demos, and conversations.

weareaiza.com

Build with us

Viszen

Viszen

Turn ambitious ideas into working solutions.

viszen.tech

17 scenes. One language-model journey.

Inside the scene

S01 · Slow the language model down00:00–01:30

Audience takeaway

A language model repeatedly predicts possible next tokens from the available context.

Speaker beats

Introduce the question: how can next-token prediction produce useful language? Given Alice was, compare beginning at 60%, getting at 25%, and very at 15%. The percentages and bar lengths show how likely each candidate is in this example. The red word is selected; advance so it joins the context. The updated context produces a fresh distribution. Keep advancing one word at a time until the sentence is complete. The same model is reused. The following scenes unpack tokens, representations, attention, training, and generation.

Keep the explanation precise

The alternatives, probabilities, and selections are scripted illustrations, not a live model. Each simplified three-choice distribution sums to 100%; a real model scores its entire vocabulary. This preview selects the most likely candidate; sampling from the distribution comes later. Words stand in for tokens; actual token boundaries vary. Generation holds the trained weights fixed.

Transition

Start with a prediction everyone can make.

From Grokking AI Algorithms, Second Edition

Five Alice training sentences
Figure 11.3 · printed page 390 · PDF page 422

You control the show

Advance, including demos
→ Space Page Down
Previous step
← Page Up
Next / previous scene
Shift + → Shift + ←
Scene overview
O
Notes & book sources
N
Fullscreen
F
Replay this scene
R
Blank the screen
B
Offline copy ready

Advance to reveal ideas and run every demo. No mouse is needed: each press shows the next prepared step, and going back restores the previous state. Diagram controls are optional for exploring. The offline HTML opens directly in your browser without a server or internet connection.

35 minutes of prepared material, with 5 minutes for discussion and pauses in a 40-minute slot. Sources and notes are visible on this screen when opened.