Week 03 lab · same text, two models

Markov chain vs. tiny transformer.

Give both models the exact same evidence. One memorizes transition counts. The other learns thousands of numerical weights. Then watch them write side by side.

01

One shared training text

54 tokens · 36 unique · 264/500,000 characters
Load an example
Markov chain

Waiting to count transitions.

Tiny transformer

Waiting to train.

02 · Under the hood

Two ways to predict the next token.

Classical language model

Markov chain

Counts

It asks: “What exact tokens came next after this exact context in the training text?”

thesmallrobotrolledwatched67%33%LOOK UP THE LAST 1–2 EXACT TOKENS, THEN FOLLOW A COUNTED EDGE
Memory
Transition table
Context
Last 1–2 tokens
Training
Count once
Neural language model

Tiny transformer

Weights

It asks: “Which parts of the recent context should influence my learned probability for every token?” PyTorch can run those calculations on a remote GPU.

32 tokenIDstoken +positionvectors4-headattention2 blocks+ MLPtokenchancesMIX INFORMATION, THEN UPDATE THOUSANDS OF WEIGHTS FROM PREDICTION ERROR
Memory
Learned numbers
Context
Last 32 positions
Training
6 passes, 120–600 updates

Same goal, different machinery. Both output a probability distribution over the same vocabulary. The difference is how they produce it.

03

Run the head-to-head test

Modal GPU first · browser fallback available
Model A · counted paths

Markov chain

0/36

Its counted-path output will appear here.

Next-token odds

Generate to reveal this model's next-token probabilities.

Model B · learned weights

Tiny transformer

0/36

Its neural output will appear here.

Next-token odds

Generate to reveal this model's next-token probabilities.

Both models are hard-limited to tokens found in your shared training text.

04 · Compare and contrast

What actually changed?

QuestionMarkov chainTiny transformer
What gets learned?Counts of observed transitionsEmbeddings and matrix weights
Can it score an unseen path?No, it backs off to a shorter seen contextYes, every vocabulary token gets a score
Does word similarity exist?No, tokens are just table keysPartly, tokens get learned vectors
Where does it run?Locally in this browserPyTorch on a Modal T4, with a browser CPU fallback
Best teaching insightPrediction can start with simple statisticsAttention learns which context matters
Pseudocode

Same task. Different recipe.

Both repeat “predict, sample, append.” Training is where their paths split.

Markov chain
split the training text into tokens

for every token:
  count what followed the last 1–2 tokens

to generate:
  look up the current context
  sample from its counted next tokens
  back off if that context was never seen
Tiny transformer
split the training text into tokens
give each token and position a learned vector

repeat for 6 passes, bounded to 120–600 updates:
  split Q, K, V across 4 attention heads
  attention ← softmax(QKᵀ + padding mask) × V
  hidden ← 2 × (attention + feed-forward block)

  predict the next training token
  update every weight using the prediction error

save the weights under a short-lived model ID

to generate on the GPU:
  predict and sample one vocabulary token
  stream that token back to the browser
  append it and repeat