Skip to content

Adapt a tiny language model

When a language model moves to a new domain, how much does each kind of adaptation gain, how much does it forget, and how many values does it train?

Notebook capstone 2~30 minadvanced Two simulated sentence domains (garden and space)

The story

A support team has a small model that writes short status lines about their garden-equipment product. They now want lines about a space-themed product as well, and two ways to adapt are on the table: retrain the model's whole output layer, or add a low-rank adapter that trains far fewer values and leaves the original weights untouched.

Before choosing, they want numbers: held-out loss on the new domain, held-out loss on the old one measured afterward as retained capability, and a count of trainable and frozen values. The workflow is small enough to inspect every step: a character model with six-number embeddings, trained by full-batch gradient descent in plain NumPy or base R, on sentences from two simple grammars split into training and test sets before anything is fitted.

Data: Two simulated sentence domains (garden and space) · Simulated. Generated in the notebooks from two small grammars with a written-out Wichmann–Hill stream (ML reference, language workflow).

Stage 1 · notebooks · ~15 min

Train and score the source model

Write the cross-entropy of a next-character model, score it before and after training on held-out garden sentences, then score the trained model on the space sentences it never saw.

How much did training help on held-out source text, and how does the source model do on the new domain?

Notebook exercise: A tiny character model, scored honestly. Write the cross-entropy of a next-character model, score it before and after training on held-out sentences, and see how badly it fits a new domain. 3 checks in Python or R.

Stage 2 · notebooks · ~15 min

Adapt, then audit

Compare a full output head with a rank-2 adapter: count the values each trains, the loss each gains on held-out space sentences, and the loss each adds on held-out garden sentences.

Which branch gains more on the target, which forgets more of the source, and what does each train?

Notebook exercise: Adapt it, and check what it forgets. Count trainable and frozen parameters, then measure target gain and source loss for a full output head and a low-rank adapter. 3 checks in Python or R.

Hints
  • Score on held-out sentences; training text says how well the model fits, not how well it generalizes.
  • Retained capability is measured on the old domain after adapting, never used to pick a checkpoint.
  • Count only the values each branch updates; the frozen embeddings don't count as trainable.

model-card

Adaptation card for the status-line model

Write the adaptation section of a model card: the two branches, what each trains, target gain and source loss on held-out text, and which you would ship with what caveat.

  1. Base model and split
  2. Two adaptations
  3. Gain and forgetting
  4. Choice and caveats
Rubric
CriterionPoints
Reports losses on held-out sentences for both domains.2
Counts trainable values for each branch and names what is frozen.2
States that the full head gains more and forgets more than the adapter here.2
Says the adapter still forgets and that retained capability must be measured.1
Notes that one seed and one tiny model show a trade-off, not a ranking.1

Model answer

Show the model answer

Base model and split. A character model with six-number embeddings was trained on 100 garden sentences and scored on 30 held-out ones; held-out cross-entropy fell to about a third of the untrained model's. On held-out space sentences the same model scored worse than untrained: confident specialization puts tiny probabilities on unfamiliar continuations.

Two adaptations. Both freeze the embeddings and train for the same number of steps on space sentences. The full head retrains the whole output matrix; the rank-2 adapter trains two thin matrices whose product is added to it, well under half as many values, and starts exactly at the base model.

Gain and forgetting. The full head gains more on held-out space sentences and raises the garden loss more; the adapter gains less and forgets less, but it still forgets a substantial amount.

Choice and caveats. If the garden lines must keep working, the adapter is the safer start, with the garden test set rerun after every update. Fewer trainable values don't prevent forgetting, and one seed on a tiny model illustrates the trade-off rather than ranking the methods.