Intro to AI : Part 1

Tokens and Markov Chains

Mehmet Kerem Turkcan
Associate Research Scientist
Center for Smart Streetscapes, Columbia University
New York, USA
keremturkcan.com;  mkt2126@columbia.edu

Move with the arrow keys, a presentation clicker, or the buttons at the bottom left; the gear at the bottom right opens the slide settings.

A game your brain plays continuously

The cat sat on the  ____  .

Question for the class: which word goes in the blank, and which words are close runners up?

You just ranked candidate words by how well they fit. A ranking of this kind can be written as a set of probabilities.

Every system in this lesson plays the same game: given the text so far, assign a probability to every possible continuation.

For the classroom: collect three candidates and an ordering from students before advancing; the next slides give the machine's version of what they just did.

Plan for today

  1. Tokens: the pieces a computer cuts text into, and the numbers behind them.
  2. The game: next token prediction, scored with a probability for every token.
  3. Markov chains: an idea from 1913 that plays the game with counted probabilities; we build one by hand and sample from it.
  4. The wall: why counting stops scaling once the context grows.

Part 2 continues with RNNs and transformers.

Tokens: how computers read text

A computer stores numbers. Before a model can read text, the text is cut into small pieces called tokens; each token is mapped to a whole number, called its token ID.

A helpful analogy: tokens behave like syllables. They are larger than single letters; they are smaller than many words; together they can spell out any text.

unbelievable text un believ able tokens 403 6462 712 token IDs
The token IDs shown here are illustrative; each tokenizer fixes its own numbering.

The vocabulary: a fixed menu of pieces

A tokenizer carries a fixed list of tokens, called the vocabulary. The list is built from data: character sequences that appear often in real text earn their own token.

the  →  the
streetscape  →  street scape
unbelievable  →  un believ able

Frequent words hold a single token; rare words are spelled from several tokens. One widely used tokenizer, released with a 2019 language model, holds exactly 50,257 tokens.

Try it: how would your class cut the word unhappiness into pieces a tokenizer might know?

The splits above are illustrative; each tokenizer learns its own cuts from its own training text.

Scoring the next token game

Given the tokens so far, the player outputs a probability for every token in the vocabulary. A high probability means the token fits the context well; a low probability means it fits poorly.

The cat sat on the ?
mat 0.42 floor 0.18 chair 0.11 sofa 0.06 banana 0.01 all others 0.22
Illustrative numbers. A real model scores the full vocabulary at once, and the probabilities add up to 1.
For the classroom: have students assign probabilities to five candidates of their own, then check that the five numbers plus a leftover share add up to 1.

Before Markov: independent events

Probability theory began with independent events. In 1713, Jacob Bernoulli's law of large numbers explained why averages settle down, provided that every trial, like every flip of a coin, ignores the ones before it.

H T T H T H H T T H H T H T 1/2 the running share of heads settles toward one half

For the following two centuries, mathematicians kept the assumption: averages settle when the trials stay independent.

1874: the fate of family names

In 1874, Francis Galton and Henry Watson asked why some family names die out, and they modeled the question as a chain: the sons of one generation carry the name into the next, so each generation depends on the one before it.

× × generation 1 generation 2 generation 3 generation 4 a branch with no sons ends
One surname across four generations; once a branch reaches zero, it never returns.

Their chain answered a practical question: if each man has some random number of sons, how likely is the name to survive forever? Dependence between the steps sat at the center of the mathematics, four decades before the poem experiment.

A quarrel in St. Petersburg

Around 1902, Pavel Nekrasov claimed that the law of large numbers requires independence, and he read support for free will into the claim: social statistics settle down, so the individual acts behind them, he argued, must be independent, hence freely chosen.

"an abuse of mathematics"
A. A. Markov, on Nekrasov's argument; the two had been rivals in the Academy for years.

Markov set out to break the claim with a counterexample: a dependent sequence that still obeys the law of large numbers. He chose text as his material, because each letter of a text depends on the letters around it so plainly that no one could deny the dependence.

A mathematical quarrel produced the Markov chain; the applications arrived later.

1913: counting letters in a poem

Andrey Markov took 20,000 letters of Pushkin's poem Eugene Onegin and marked each letter as a vowel or a consonant.

He then counted transitions. In his tally: about 43 of every 100 letters were vowels; after a vowel, a vowel followed about 13 times in 100; after a consonant, a vowel followed about 66 times in 100.

Although the next letter clearly depends on the current letter, the counted sequence still obeyed the law of large numbers, so the counterexample worked. Models of this kind now carry his name.

V vowel C consonant 0.87 0.66 0.13 0.34
Rounded transition probabilities from the 1913 letter study.

1948: Shannon's game with whole words

In 1948, while founding information theory, Claude Shannon played the same game with whole words. Because no computer held English statistics yet, he sampled from books by hand: after writing a word, he opened a book at a random page, read until that word appeared again, and wrote down the word printed right after it.

"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 WHO EVER TOLD THE PROBLEM FOR AN UNEXPECTED"
A word pair approximation to English; Shannon, 1948.

Every neighboring pair above fits together, because each pair came from real text; the sentence as a whole wanders, because nothing beyond the current word steers it.

For the classroom: students can replicate Shannon's procedure with any novel in about ten minutes, and the results read like the book while meaning nothing.

A Markov chain: states and arrows

A Markov chain is a set of states joined by arrows, where each arrow carries the probability of moving to the next state. Its defining rule, called the Markov property, says the following: the probability of the next state depends only on the current state. Below, in a chain counted from a corpus of three sentences, the states are tokens.

the cat dog mat rug ate sat on . 0.4 0.2 0.2 0.2 0.5 0.5 1.0 1.0 1.0 1.0 1.0 1.0
Corpus: (i) the cat sat on the mat . (ii) the dog sat on the rug . (iii) the cat ate . The arrows leaving any state add up to 1; the amber arrow returns from on to the.

Where do the arrow numbers come from?

They are counted. Take a pile of training text, called the corpus, and tally every neighboring pair of tokens:

P(\text{next} = B \mid \text{current} = A) = \frac{\text{count}(A\ B)}{\text{count}(A)}

The counting carries a guarantee: these values make the corpus itself as probable as it can be. Statisticians call it maximum likelihood estimation; it is the optimization step of the Markov chain.

Pairs never seen in the corpus get probability zero; practical systems soften the zeros with smoothing.

Counting the pairs that start with the (5 in total):

paircountprobability
the  cat22 / 5 = 0.4
the  dog11 / 5 = 0.2
the  mat11 / 5 = 0.2
the  rug11 / 5 = 0.2
total51.0

Sampling step by step

To generate, the chain repeats a single step again and again: from the current token, choose the next at random, so that each arrow wins in proportion to its probability. Random choice in proportion to probability is called sampling. Starting from the, our counted arrows cut the interval from 0 to 1 like this:

cat 0.40 dog 0.20 mat 0.20 rug 0.20 0 0.40 0.60 0.80 1 roll 1: 0.57 roll 2: 0.71
positionwhat happensmove
theroll 1 gives 0.57, which lands in the dog segment (0.40 to 0.60)dog
dog, sat, onno roll is needed, because each has a single arrow with probability 1.0sat, on, the
theroll 2 gives 0.71, which lands in the mat segment (0.60 to 0.80)mat
mata single arrow leads to the period, which ends the walk.

The walk spells the dog sat on the mat .   Although this exact sentence appears nowhere in the corpus, every step is backed by a counted pair.

Two tokens of context

Our chain reads one token of context. To read two, the state becomes a pair of tokens, and the count table needs a row for every possible pair. With a vocabulary of 50,000 tokens:

context the chain readsrows the count table needs
1 token50,000
2 tokens2,500,000,000  (2.5 billion)
3 tokens125,000,000,000,000  (125 trillion)
Why not count longer contexts?

"Keep counting with longer and longer contexts." Because each added token multiplies the table by 50,000, while real text supplies examples for almost none of the longer contexts, the table grows even as the counts inside it stay at zero.

Agreement across seven tokens

Start from a correct sentence, exactly as a book would print it:

Question for the class: the verb is now covered. Which earlier word decides between was and were?
The girl who fed the three noisy cats was ____ smiling. the nearest noun pulls toward were the subject decides: was

A model that reads only nearby tokens follows cats to were; because the subject girl sits seven tokens back, the correct choice, was, depends on information far outside a short window.

Part 1 Overview

  1. Tokens: text is cut into pieces the size of syllables, and each piece carries a whole number ID; the vocabulary is the fixed menu of pieces.
  2. The game: assign a probability to every token that could come next.
  3. Markov chains: counted probabilities over states, where the next token depends only on the current one; the counting is maximum likelihood estimation, softened by smoothing.
  4. The wall: because longer context multiplies the table by the vocabulary size, the counts a longer chain needs never appear in any real corpus.

Part 2: a network that reads token by token while carrying a learned memory; then the transformer, which reads the whole window at once.

Sources and further reading

  • J. Bernoulli, 1713: Ars Conjectandi; the law of large numbers for independent trials.
  • F. Galton and H. W. Watson, 1874: On the Probability of the Extinction of Families.
  • A. A. Markov, 1913: the letter study on Eugene Onegin, presented to the St. Petersburg Academy of Sciences; the first Markov chain analysis of text.
  • B. Hayes, 2013: First Links in the Markov Chain, American Scientist 101(2); retells the study and the Nekrasov dispute, and supplies the counts quoted here.
  • C. E. Shannon, 1948: A Mathematical Theory of Communication; source of the word pair approximation quoted here.
  • A. Radford and colleagues, 2019: Language Models are Unsupervised Multitask Learners; the language model whose tokenizer holds 50,257 tokens.

Slides: Mehmet Kerem Turkcan, Center for Smart Streetscapes, Columbia University; keremturkcan.com; mkt2126@columbia.edu

Intro to AI, Part 1: Tokens and Markov ChainsM. K. Turkcan, Columbia University