From neurons to tokens
The full foundation in one day: how a neural network learns, how tokenizers and encoders are trained, how tokens become vectors, and why Korean costs more tokens.
Foundations — how a network learns
A neuron, training a classifier, and one full backprop step.
What is a neural network?
A neural network is just a big stack of adjustable knobs (called weights). Numbers go in, the network multiplies them by its knobs and adds them up, and an answer comes out. “Learning” means slowly turning the knobs until the answers are right.
Try one neuron
Drag the weights. The neuron multiplies each input by its weight, adds a bias, and squashes the result into an output between 0 and 1.
The squash: the sigmoid σ
output = σ(0.92) = 0.715
Training a network to classify
Below is a real neural network running in your browser, learning to separate two groups of dots. Press Play and watch the colored boundary bend until it sorts them correctly — and the loss (how wrong it is) fall toward zero.
Forward pass → backprop → weight update
Above you watched the boundary move. But HOW does a weight know which way to turn? Here is a tiny real network (2 inputs → 2 hidden → 1 output). Step through one full training iteration and watch the exact value of every weight change.
| weight | value | gradient ∇ | new value |
|---|---|---|---|
| W₁ x₀→h₀ | 0.60 | — | — |
| W₁ x₁→h₀ | -0.50 | — | — |
| W₁ x₀→h₁ | -0.40 | — | — |
| W₁ x₁→h₁ | 0.70 | — | — |
| b₁ h₀ | 0.10 | — | — |
| b₁ h₁ | -0.20 | — | — |
| W₂ h₀→ŷ | 0.50 | — | — |
| W₂ h₁→ŷ | -0.60 | — | — |
| b₂ ŷ | 0.15 | — | — |
Tokenizer — text into pieces
First, text is split into subword tokens. The tokenizer is trained too.
Training a tokenizer (BPE)
A subword tokenizer is also trained — but differently from the network above. It starts from single characters, then repeatedly glues together the most frequent neighboring pair. Press Step (or Play) to watch it build a vocabulary of reusable pieces from this little corpus.
Encoder — learning meaning
A real BERT learns by filling in masked blanks (self-supervised).
Masked Language Modeling — fill in the blank
Once the tokenizer above has split text into pieces, the encoder learns what those pieces MEAN. This is how modern text encoders (BERT, RoBERTa, XLM-R) actually learn — with no human labels. Hide a word with [MASK] and the model must predict it from context. Repeat over billions of sentences and it absorbs grammar, facts, and meaning. Below is a REAL multilingual BERT predicting live in your browser.
Downloading the real BERT weights… (loads automatically — a moment)
Embeddings — tokens as vectors
How a token gets its vector, then real word maps in 2-D and 3-D.
Token labeling & the embedding table
Before any learning, two setup steps turn tokens into something a network can use. (1) Every distinct token from the corpus is given a number — its label/ID. (2) Each ID points to a row of N numbers — its vector. Those numbers begin random; training (next) makes them meaningful.
| id | token | d1 | d2 | d3 | d4 | d5 | d6 | … |
|---|---|---|---|---|---|---|---|---|
| 0 | king | -0.98 | -0.88 | 0.95 | 0.40 | 0.04 | -0.19 | … |
| 1 | queen | -0.07 | -0.52 | 0.11 | 0.46 | -0.48 | -0.69 | … |
| 2 | throne | 0.53 | 0.04 | -0.61 | -0.26 | -0.41 | 0.07 | … |
| 3 | crown | -0.41 | 0.96 | -0.50 | 0.76 | -0.62 | -0.71 | … |
| 4 | prince | -0.69 | -0.42 | 0.10 | 0.52 | -0.85 | 0.83 | … |
| 5 | royal | 0.11 | 0.24 | 0.36 | 0.93 | 0.25 | 0.84 | … |
| 6 | dog | 0.66 | -0.92 | -0.65 | -0.29 | -0.15 | -0.96 | … |
| 7 | cat | -0.82 | 0.54 | -0.26 | -0.15 | -0.06 | 0.89 | … |
| ⋮ (24 tokens total) | ||||||||
Learning an embedding from scratch
A network can't read letters — only numbers. So every token is handed a short list of numbers called its embedding (here just 2 numbers, so we can plot it). At the start these numbers are RANDOM. Watch the network learn better ones, live.
| king | 0.25 | -0.99 | |
| queen | 0.05 | 0.96 | |
| throne | 0.94 | -0.44 | |
| crown | 0.23 | 0.44 | |
| prince | -0.15 | 0.99 | |
| royal | -0.09 | -0.02 | |
| dog | -0.72 | -0.19 | |
| cat | -0.50 | -0.69 | |
| pet | -0.02 | -0.87 | |
| puppy | -0.21 | 0.53 | |
| kitten | -0.43 | -0.62 | |
| animal | -0.91 | -0.15 | |
| apple | 0.18 | 0.60 | |
| banana | -0.41 | -0.59 | |
| fruit | 0.32 | 0.09 | |
| grape | 0.52 | -0.61 | |
| sweet | 0.44 | -0.09 | |
| juice | 0.54 | 0.99 | |
| one | -0.52 | 0.51 | |
| two | -0.69 | -0.35 | |
| three | -0.17 | -0.50 | |
| four | -0.70 | 0.45 | |
| number | 0.15 | -0.75 | |
| count | -0.47 | -0.90 |
Turning words into numbers (embeddings)
Networks only understand numbers, so each word becomes a list of numbers — a vector. Through training, the network learns to place words with similar meaning near each other. Click a word to see its nearest neighbors.
Now in 3D — add your own words
Same idea, but live and in 3D with a REAL model. It turns each word into a 384-number vector; we project that to 3D so you can see it. Drag to rotate, use +/− to zoom, click a word for its closest relatives. Add any word — even Korean — and watch where it lands.
Downloading the embedding model (~23 MB, once) & embedding words…
Tokenization & the token tax
Why Korean and low-resource languages cost more tokens.
Why does the same sentence cost more in Korean?
AI language models don't read words — they read tokens (chunks of text). Type the same meaning in English and Korean below, and watch how many tokens each one becomes. The counts come from GPT's real tokenizer, running right in your browser.
Tokenization: how machines slice up language
Type anything (try English and Korean!). The same text is split three ways. Notice how the counts and pieces differ.
Character (by letter)
Tiny vocabulary, never “unknown”, handles typos & emoji. But sequences get very long and the model must learn what a word is from scratch.
Word (by spaces)
Intuitive and short. But the vocabulary explodes and any new/rare word becomes “unknown” (UNK). Korean is brutal here: 학교, 학교에서, 학교를 all look like different words.
Subword (GPT's real BPE)
The winner used by all modern LLMs: keep common words whole, split rare ones into reusable pieces. Below is GPT-4's actual tokenizer. ‘␣’ marks a space; the amber chips are partial-byte pieces — exactly how Korean often gets shattered.
Trade-offs at a glance
| Character | Word | Subword | |
|---|---|---|---|
| Vocabulary size | tiny | huge | medium |
| Unknown words? | never | frequent | rare |
| Sequence length | long | short | medium |
| Used by | rare today | old NLP | BERT, GPT, all LLMs |
WordPiece vs SentencePiece vs BPE — the real models
The panel above uses GPT’s BPE. Here are three real tokenizers from actual models, running live in your browser. Watch the markers: BERT uses ## (glue to the previous piece), XLM-R uses ▁ (a space before a word).
Downloading tokenizers…
Normalization: when identical-looking text isn't equal
Before tokenizing, text is cleaned. The sneakiest case: two strings that look identical but are stored as different bytes — so the computer says they're NOT equal. This bites Korean especially (and is the cause of broken filenames between Mac and Windows).
Unicode forms — “한글”
| NFC | U+D55C U+AE00 | 2 code points |
| NFD | U+1112 U+1161 U+11AB U+1100 U+1173 U+11AF | 6 code points |
| NFKC | U+D55C U+AE00 | 2 code points |
| NFKD | U+1112 U+1161 U+11AB U+1100 U+1173 U+11AF | 6 code points |
The token tax: low-resource languages pay more
Every bar below is the SAME sentence — “Artificial intelligence is changing the world.” — in a different language, measured with GPT's real tokenizer. English is cheapest because the tokenizer saw the most English. Languages with less training data get shattered into more, smaller pieces.
Why this happens
A subword tokenizer is trained on data. It learns big reusable chunks for languages it saw a lot of (English), and only tiny byte-level pieces for languages it barely saw. Korean is agglutinative — particles & endings (조사/어미) attach to stems (학교+에서+는) — so even subwords need many pieces.
What the tax costs
- Money — APIs charge per token
- Speed — more tokens, slower replies
- Memory — fills the context window sooner
- Fairness — speakers of low-resource languages pay more for worse AI
Try it: the price of the tax
A rough estimate. Same app, same number of messages — Korean costs more simply because it becomes more tokens.
Reference
Vocabulary card for the key terms.
Korean ↔ English glossary
| Term | 한국어 | Plain meaning |
|---|---|---|
| NLP | 자연어처리 | Computers working with human language |
| LLM | 거대언어모델 | A huge model that predicts text |
| Token | 토큰 | A chunk the model reads (not always a word) |
| Tokenization | 토큰화 | Cutting text into tokens |
| Vocabulary | 어휘집 | The fixed list of tokens a model knows |
| OOV / UNK | 미등록 단어 | A word not in the vocabulary |
| Normalization | 정규화 | Standardizing text (case, spacing, Unicode) |
| Subword | 서브워드 | Pieces between a letter and a word |
| BPE / WordPiece / SentencePiece | — | Subword algorithms (GPT / BERT / multilingual) |
| Embedding | 임베딩 | Turning a token into numbers |
| Morpheme | 형태소 | Smallest meaning unit (key for Korean) |
| Agglutinative | 교착어 | Korean attaches particles/endings to stems |
| Low-resource language | 저자원 언어 | A language with little training data |
| Context window | 문맥 길이 | Max tokens a model reads at once |
| Pretraining / Fine-tuning | 사전학습 / 미세조정 | Train broadly / adapt to a task |
Attention & Transformers
How tokens look at each other: self-attention, the Transformer block, and the split between encoders (reading) and decoders (generating).
Attention — tokens look at each other
Each token decides which other tokens to focus on, and by how much.
Self-attention — which tokens matter?
Each token builds a query and compares it against every other token's key. The softmax of those scores decides how much of each token's value to mix in — so a word can “look at” the words it depends on.
| The | cat | sat | on | the | mat | |
| The | 16 | 17 | 18 | 16 | 16 | 17 |
| cat | 12 | 21 | 21 | 13 | 12 | 21 |
| sat | 12 | 20 | 22 | 15 | 12 | 20 |
| on | 14 | 16 | 20 | 20 | 14 | 16 |
| the | 16 | 17 | 18 | 16 | 16 | 17 |
| mat | 12 | 21 | 21 | 14 | 12 | 20 |
Guess where the word looks
The highlighted word is ambiguous. A person uses the rest of the sentence to figure out what it means — and so does attention. Which word do you think it points to?
The trophy didn't fit in the suitcase because it was too big.
The Transformer block
Attention + a feed-forward network, stacked with residual connections and normalization.
Putting a Transformer layer together
One block = multi-head attention → add & normalize → feed-forward network → add & normalize. Stack a few dozen of these and you have GPT or BERT.
- Multi-head attention — mixes tokens together
- Feed-forward network — processes tokens separately
- Add & normalize
- residual / skip connection
Build a Transformer block
You've seen the finished block — now assemble one yourself. Click the four pieces in the right order, bottom to top. Data flows up from the input embeddings to the output.
Click a stacked piece to take it back.
Encoder & decoder
Reading (encoder) vs generating (decoder) — and how the two combine for translation.
Reading vs generating
Same attention machinery, one difference: what each token is allowed to look at. Green means “can look”.
Encoder — reads
Every token sees every other token — full context, both directions. This is BERT.
Decoder — generates
Each token sees only itself and the words before it — it can't peek at the future. This is GPT.
Row = the token doing the looking · column = the token being looked at