
Language Models · v1.0.5
2026-11-03 01:40:26

Built already
GPTModel and a greedy generation loopStill missing
argmaxTraining runs on a 5,145-token public-domain story, so every experiment finishes on a laptop.

\[\mathcal{L} = -\frac{1}{N}\sum_{i=1}^{N} \log p(y_i \mid x_i)\]
\[\text{perplexity} = e^{\mathcal{L}}\]
cross_entropy computes \(\mathcal{L}\) from raw logits and target IDs.
Loss per token falls steeply as the true token gains probability.




\[p_i = \frac{\exp(z_i / T)}{\sum_j \exp(z_j / T)}\]
argmax decoding is deterministic: one prompt, one continuation, often a memorised passage.argmax; \(T>1\) flattens toward nonsense.Creativity and coherence pull in opposite directions, and the temperature is the dial between them.

def generate(model, idx, n_new,
temperature=0.0,
top_k=None, eos_id=None):
for _ in range(max_new_tokens):
logits = last_position_logits(model, idx)
if top_k is not None:
logits = mask_below_top_k(logits, top_k)
next_id = (sample(softmax(logits / T))
if T > 0.0
else argmax(logits))
if next_id == eos_id:
break
idx = append(idx, next_id)
return idxGreedy decoding is the special case \(T = 0\) — the earlier behaviour is subsumed, not discarded.
Construct an architecture, then assign parameters into it by name — the same mechanism serves for weights trained elsewhere.

Coherent English from the loaded model is itself the proof that architecture and assignment agree.
A pretrained GPT-2 is a general next-token predictor: it completes text, but it answers no question and labels nothing. Stage 3 is adapting it.
In 6 Fine-tuning for classification:
The loss function, training loop and checkpointing carry over unchanged; only the head and the targets differ.