Fine-tuning basics (TypeScript) · Chapter 11 of 20
Full fine-tuning
By the end of this chapter you can write the loop a fine-tuning job runs: batches, the loss, gradients, clipping, an optimizer step, epochs. You will run it on all 45,152 of chota's numbers, watch the loss curve, score the result on the test set with no API at all, and see what else a full fine-tune changes. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Inside
- 4 tasks
- TypeScript
- In the all labs plan
The first 2 chapters of Fine-tuning basics (TypeScript) are free. This one comes with the all labs plan.
Why you need this
Chapter 8's jobs ran on the lab's API: you chose epochs and saw losses fall, but the training itself was the provider's. Irfan wants Chulha to be able to train on its own machine, where nothing is hidden and every setting is yours. And chapter 10 ended with a question: fine-tuning "trains the predictions at <bot>", but how, exactly?
You have the model, the template and the loss. What you do not have yet is the loop that turns a loss into better weights.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
train.ts: the training loop
Write and export train(model, set, o): full fine-tuning in place, in this order: rows = each example's chatIds; one new Rng(o.seed) and one newAdam(); each epoch, order = rng.shuffle of the row indices, then for each slice of o.batch indices: makeBatch, lossAndGrads(model, batch), clipGrads(grads, o.clip), adamStep(model.params, grads, adam, o.lr), keeping the loss. After each epoch call o.onEpoch?.(epoch, that epoch's losses). Return every step's loss, in order.
- Task 2
curve.ts: the loss curve
Write and export valLoss(model, set): the loss on the whole set with no training: lossOf(forward(model, batch.ids).logits, batch).loss for batch = makeBatch of every example's chatIds.
Under if (import.meta.main), print before: val V for chota-base on examples('val'), then train it on examples('train') (epochs 2, lr 0.001, batch 16, seed 0, clip 1) and after each epoch print:
epoch E: train T, val V (T the mean of that epoch's step losses, V valLoss then; 4 decimals)
- Task 3
evaluate.ts: before and after
Write and export evaluate(model, set): { accuracy, format, leaksCaught, leaks } from the replies of generateMany(model, set.map(messageOf), 4), as chapter 1 scored them.
Under if (import.meta.main), on examples('test'): print before: accuracy A%, format F%, LEAK C/L for chota-base, train it with your train (epochs 2, lr 0.001, batch 16, seed 0, clip 1), and print:
trained in S s
after: accuracy A%, format F%, LEAK C/L
- Task 4 · interview question
Interview: what-changed.ts, what else did it change?
Rashmi: "The inbox's type-ahead runs on chota-base too. Can it use the fine-tuned model, so we keep one model?" Train a copy of chota-base with your train (epochs 2, lr 0.001, batch 16, seed 0, clip 1) and keep another as it is. For 'gas ki', 'regulator' and 'booking kiye' print TEXT: BASE -> FINE_TUNED, each the type-ahead generate(m, [USER, ...encode(text)], 8). Then print:
changed N of M numbers (weight numbers that differ from chota-base's)
model file: B bytes (saveModel(model).length)
What you will learn
- A training step: a batch, the loss, the gradients, a small move of every weight.
- Clipping and Adam: how big each move is, and why it is capped.
- Epochs and shuffling: every example once per epoch, in a new order.
- The loss curve: training and validation loss, epoch by epoch.
- Full fine-tuning: every weight trains, and what that costs.
More in Inside
Build "Full fine-tuning" yourself
Chulha, a Nagpur startup, tags WhatsApp messages for 40 LPG gas agencies, and a missed gas leak is not an option. You fine-tune chota, a tiny transformer, until it beats the best prompt while forgetting little. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Fine-tuning basics (TypeScript) are free. This one comes with the all labs plan.