Part 1 · Understand LLMs Chapter 2
Foundations
Language modeling, neural networks, information theory, tokens and embeddings, and optimizers: the lowest-level primitives of an LLM, and the reason behind almost every design decision that follows.
After this chapter you will be able to
- Say what language modeling estimates, split a sentence's probability into per-token predictions with the chain rule, and write the cross-entropy loss and perplexity.
- Read the forward and backward pass of a neural network, and see how linear layers, nonlinearities and gradients make up the one machine used to train LLMs.
- Put loss, perplexity and the KL penalty into the language of information theory and convert between them.
- Explain how text is cut into tokens, how tokens become vectors, and how vocabulary size affects cost and quality.
- Choose an optimizer and a learning-rate schedule for training, and work through a checklist when the loss spikes or turns into NaN.
If Python or matrix dimensions are new to you, start with Python and tensor shapes. Calculate one entry by hand before running the layer.
LLMs look all-powerful, but their underlying task is plain: given what came before, predict the next token. This chapter takes that plain task apart and explains the primitives it uses: what the training objective is and why “predict the next word” can produce such a rich set of abilities; how the neural network that fits the objective is trained; the information theory behind loss, perplexity and KL divergence; how text is cut into tokens and tokens become vectors; and which optimizer and learning rate to train with.
The pages in this chapter are the shared language of every later chapter. The Model Architecture chapter shows how these vectors are processed inside a Transformer, and the Pre-training chapter puts the loss and optimizers here into a full training pipeline. Readers who already know this material can check themselves against the “After this page you will be able to” box of each page and skip what they do not need.
Chapter roadmap
- 2.1
Python and tensor shapes: from a table to a linear layer
Start with a table of numbers, name the position and feature axes, and calculate a dot product and bias before running a linear layer in standard Python.
- 2.2
From Language Modeling to Next-Token Prediction
Language modeling estimates the probability of a text and uses the chain rule to split it into a product of "predict the next token given the prefix"; training uses cross-entropy, evaluation uses perplexity, and the causal mask hides future tokens.
- 2.3
Neural Network Basics: From Linear Layers to Backpropagation
A neural network chains linear maps and nonlinearities. Backpropagation (the chain rule) gives the loss gradient for every parameter, and training steps against it. This page builds a two-layer net in NumPy, checks its gradients, and runs learning-rate and initialization experiments.
Prerequisites2.1Python and tensor shapes
- 2.4
Information Theory Refresher: Entropy, Cross-Entropy and KL Divergence
Entropy measures how surprising a distribution is on average, cross-entropy is the cost of coding data from p with a code built for q, and KL divergence is the excess over the optimum. A language model's training loss is cross-entropy; perplexity is its exponential.
Prerequisites2.2From Language Modeling to Next-Token Prediction
- 2.5
Tokenizers and Embeddings: BPE, Vocabulary Size and the Embedding Table
An LLM only processes integers: a tokenizer splits text into tokens with a subword algorithm such as BPE and maps them to ids, then each id is looked up in a vocabulary-size by hidden-size embedding table. Vocabulary size trades off sequence length, parameters and long-tail coverage.
Prerequisites2.2From Language Modeling to Next-Token Prediction
- 2.6
Optimizers and Training Stability: AdamW, Warmup, Mixed Precision, Loss Spikes
Large-model pretraining almost always uses AdamW with linear warmup plus cosine (or WSD) decay, gradient clipping and mixed precision. This page explains what each hyperparameter is for and gives a checklist for loss spikes and NaNs.
Prerequisites2.4Information Theory Refresher