Week 1 — Inferring a distribution from observations

Suppose the same text fragment is followed by three times and by once. What probabilities should a model assign on the next occurrence? The first step toward an LLM is to make this small inference explicit. The architecture will later supply a much richer dependence on the preceding text.

Course: nn-llm-syllabus. Overview: nn-llm-start-here. Authorship and supervision: ai-authorship.

Learning goals and reading

By the end of the page you should be able to fit a two-outcome distribution, perform one parameter update, distinguish sample loss from expected loss, and turn arbitrary real scores into probabilities. The derivations below require only elementary probability and differentiation.

For more exercises, see the companion notes, Chapter 1, §§1.1–1.4. Shannon’s papers are listed in nn-llm-bibliography.

1. What is observed, and what is adjusted?

A token is a chosen unit of text, such as a character, word or word part. The vocabulary is the finite set of possible tokens. A corpus is a collection of observed texts. The context is the part already supplied, and the target is the next token that was actually observed.

For now hold the context fixed and use only two possible targets. Write and . Introduce a real adjustable parameter and define

This is the familiar two-state Gibbs formula. Here labels a token and is adjusted from observations; no magnetic moment or heat bath is being assumed. Positive favours , and gives equal probabilities. We know how to compute the mean given a field: . Fitting asks for the field that reproduces an observed mean.

2. Why minimize a logarithmic loss?

Treat the targets in this one-context example as independent draws. If of them are positive and negative, their likelihood is

The likelihood is a function of the parameter for the fixed observed data. We choose a parameter that makes those observations probable. Taking a logarithm preserves the maximizing value and turns the product into a sum. Changing the sign turns maximization into minimization. Dividing by makes the scale comparable across sample sizes. The resulting loss is

The hat marks a quantity computed from a finite sample. Natural logarithms give units of nats. Differentiate only ; the observations stay fixed:

The stationary condition is mean matching. For the unique minimum is

For three ‘s and one , , , and . The loss falls from at to nats. If one count is zero, the optimal probability is on the boundary and requires an infinite field; there is no finite minimizing .

One step toward the fit

Gradient descent adjusts a parameter against the derivative of its loss:

The learning rate sets the step size; counts updates. Starting at zero with , the three-to-one sample gives and . The prediction moves toward the observed frequency because the predicted mean was too small.

Since , Taylor’s inequality gives

Thus guarantees a decrease away from a stationary point for this loss. This bound uses the maximum susceptibility of the two-state model. It is not a universal learning-rate prescription for a neural network.

3. The source, and what a finite sample can tell us

A source is an idealized distribution over contexts and next tokens . A model supplies . Its expected loss averages over fresh draws from that source:

Add and subtract inside the sum:

Here is the source’s conditional entropy and is relative entropy. The convention is understood; if the model assigns zero probability to an event of positive source probability, the loss is infinite. Concavity of the logarithm gives , with equality only for identical distributions.

The source entropy does not depend on the model parameters. The remaining term measures mismatch. Reducing expected loss is therefore exactly reducing this mismatch. Training instead reduces a sample loss; whether that improves fresh predictions is a separate question called generalization. This is why observations kept out of fitting, called test data, are useful.

A finite test loss can lie below the entropy

Take a fair source and a fixed model . Its expected loss is , larger than as required. But an independent test sample of four draws can contain three ‘s and one , with probability . Its sample loss is , which is below . There is no contradiction: the entropy bound applies to the source average, not to each finite sample.

The model’s own entropy, , is yet another average. It happens to equal here because the sample frequencies equal . The general cross-entropy decomposition contains , not .

4. From two outcomes to a vocabulary: softmax

For each possible next token , let the model produce a real score or logit . A score is not a probability. The softmax map makes these scores positive and normalized:

With scores for , softmax gives , the same fit as before. Adding any common constant to both scores leaves the result unchanged. Only score differences affect the probabilities.

The logarithm of the denominator is the log-sum-exp function. For later use allow a positive scale :

At , the loss for one observed target is . Its derivative is

The indicator is one for the observed token and zero otherwise. This is the multiclass version of the predicted-minus-observed mean in §2, and it is the starting point for training a decoder in Week 10.

Setting makes softmax a Gibbs measure at unit inverse temperature: and . The identity lets us use partition function derivatives. It does not supply a heat bath, an equilibrium dynamics or a thermodynamic limit. Convexity in the scores also does not imply convexity in the weights of the network that produces them.

5. A sequence, context and perplexity

For a sequence , probability’s chain rule gives

No independence between successive tokens is assumed. Taking and dividing by yields the mean next-token loss. During training these contexts come from observed text. During generation the chosen tokens are appended to form subsequent contexts, while the parameters remain fixed.

Perplexity is , the inverse geometric mean probability assigned to observed targets. For an ideal predictor of equally likely alternatives it equals . With mismatch it is not a literal count: a model on a fair source has population cross-entropy and its exponential is .

For ideal predictors, the benefit of context is exactly

An imperfect model may fail to realize that benefit. Week 2 computes it for a source whose correlations are known.

Checkpoints with answers

1. Which quantity is fixed in the derivative of the sample loss? The observed mean . Only is varied. Differentiating the data as though they changed with the model would solve a different problem.

2. Why is a highly confident model not necessarily accurate? Concentration makes small, but a wrong confident prediction has a large . Accuracy concerns an average over observations, not over the model’s preferred outcomes.

3. Does a test loss below prove an error? No. The four-draw example above gives a counterexample with a known nonzero probability. Repeated independent test samples estimate the expected loss; one fluctuation does not change its bound.

4. What changes if all vocabulary scores increase by ten? Nothing in the probabilities or loss: the common factor cancels. This is the freedom to choose an effective energy zero.

What the week establishes

Maximum likelihood gives a concrete loss and a mean-matching condition. Softmax generalizes the two-state construction to any finite vocabulary. The source-average loss separates irreducible uncertainty from model mismatch. These exact identities will remain true when a neural network produces the scores; the difficulty will lie in estimating and optimizing them.

The practical session

A guided calculation with no programming prerequisite, followed by the first browser experiment, which runs without installation.

Varied: the observed proportion , the initial parameter , and the step size . Held fixed: the family of distributions and the fitting criterion. Measured: the loss curve against the analytic optimum , and whether a step with at or above behaves as the bound predicts.

Product: the fitted for at least three samples, checked against , plus one worked instance of a step size that fails and an explanation of why the bound permitted it to.


Connections to other parts of the wiki

  • Week 2 takes this two-state system and makes its context a preceding token, which turns the fit into a transfer matrix.
  • The relative entropy of §3 is the classical case of the quantity that appears in the group’s research as the Araki–Uhlmann relative entropy of states on a von Neumann algebra, catalogued in the relative-entropy area page — the same quantity in different settings, classical and algebraic, and no more than that.
  • The decomposition of §3 returns in Week 12 for the single-site conditionals of a Potts model, and in Week 14, where the same inequality is what makes a variational bound a bound.
  • nn-llm-unit-0 — the block skeleton this note implements.
  • nn-llm-conventions — notation, including the symbols that carry more than one meaning.

Start here · All weeks · Book and experiments · Next week