5/30
Part 2 of 6 · Loss checksLearn and do
A fresh model gives every token the same small chance
With 65 tokens in the vocabulary, a fresh model gives each one a chance of about 1/65:
The cheapest check runs before any training. A fresh model knows nothing, so it gives every token about the same chance, , where is the number of tokens in the vocabulary. Its first loss is then about .
Karpathy's recipe makes this a rule: check that the loss starts at the value it should. It takes one forward pass, and it tests the model, the loss and the init together.
The gap above ln V tells you how big the logits are
import math
import torch
import torch.nn.functional as F
torch.manual_seed(0)
V = 65
targets = torch.randint(0, V, (20_000,))
for s in (0.17, 0.58):
# random logits, spread s
logits = s * torch.randn(20_000, V)
loss = F.cross_entropy(logits, targets).item()
print(f"s {s}: loss {loss:.3f} ln V + s*s/2 = {math.log(V) + s * s / 2:.3f}")
Random logits with spread s = 0.17, then 0.58, over 65 tokens. How do the losses compare with ln 65 = 4.17?
Pick one to see what it prints.
Pick an answer above to see the rest