Barbara - Training the Router
One Sentence In, One Action Out
Summary: Barbara’s router reads what the speech recognition heard and returns one of 47 action tokens in a single pass. It is Gemma 3 270M with a small classification head, trained on about 20,000 generated sentences (28,000 with the held-out ones). This part is my journal of how I built it: the dataset, the method, how I read the training graphs, and what moved the score. One method change, training the model’s last layers, gave the big jump; after it, training a few more layers added a little, and the data did the rest.
Why a Router
Routing is a classification problem: a sentence comes in, and one label out of a fixed list comes out. Each action gets its own token, and the token is the full action, not a module name to resolve later:
- “what’s the weather like” →
<weather> - “turn off the sofa light” →
<lights_sofa_off> - “add milk to the shopping list” →
<shopping_add> - anything Barbara can’t do, or can’t understand →
<unknown>, which goes to chat
The action tokens are only class labels: no word is added to the vocabulary. The input is only what I said, so it is short and one pass is enough. And the model can only pick one of the 47 tokens (46 actions and <unknown>), so its answer can’t be malformed.
The Model: Gemma 3 270M With a New Head
A language model ends with a “head” that scores each of its 262,144 vocabulary words to guess the next one. I replaced it with a head of 47 scores, one per action token. This follows Sebastian Raschka’s LLM classifier (chapter 6 of Build a Large Language Model (From Scratch)), and transformers ships it as Gemma3TextForSequenceClassification.
- The last token reads the whole sentence. The model is causal: each position only sees the words before it, so the head reads the last real token, the only one that has seen everything. With left padding, it is always the last position.
- The embeddings stay frozen. The embeddings, the table that turns each token into 640 numbers, hold 168M of the 268M parameters, mostly for tokens my data never uses.
- Only the end learns: the head, the final norm and the last 4 of the 18 blocks, 22.3M parameters. How many blocks to train was the one method decision that mattered.
Why Gemma 3 270M? With only a head trained on top of the frozen model, Qwen3-0.6B-Base reached 74.9% against Gemma’s 71.1%. Not enough to justify 2.2 times the parameters and 3.6 times the CPU time.
For experts: Hugging Face Trainer, AdamW, learning rate 5e-5, batch 16, 10% warmup then linear decay, weight decay 0.01, up to 5 epochs, inputs cut at 32 tokens (median 8, at most 21).
The Dataset
I had no recordings of French-accented English spoken to a home robot, so a deterministic script generates the data.
Phrasings and templates
Each token has a file of base phrasings, one per line, tagged llm: an LLM wrote them, following my labeling and realism rules. They are what a real person says to a home robot: short commands (“sofa off”), questions, indirect requests (“it’s dark in here”), never contrived lines like “let there be light”. About ninety rules decide the labels: “switch the bedroom light”, without on or off, is a toggle; “open the light”, a French calque, means on; “leave the sofa light on” is <unknown>.
Light tokens also get lines tagged template: frames like “turn {on/off} {target}” crossed with every name of a light, filling up to half of each light token’s groups. They turned out far too easy: on the current test, 99.3% against 94.0% for the llm lines of the action tokens, intervals apart. So I add llm lines, not templates.
Every action token gets 107 to 116 lines, so no action dominates. <unknown> gets more: the hardest class, it is full of near-misses that look like an action but aren’t one: lights she doesn’t control (“kitchen light”), other devices (“turn the tv volume up”), state questions (“is the bedroom light on”), the radio (“play france info”), truncated requests (“turn the”). Each one teaches the model where an action stops.
Speech-to-text noise for a French accent
The router never reads clean text: it reads what the speech recognition wrote while I spoke English with a French accent. Speech recognition writes real words, close to the sound: “of” for “off”, “life” for “light”, “so far” for “sofa”, “bad room” for “bedroom”, “sis” for “this”, “well” for “will”. It almost never writes non-words like “ze”.
So each base phrasing becomes a group of five examples: the clean text and four noisy variants. Noise comes in three kinds, often combined: wrong words, missing words and extra words (fillers like “euh”, restarts, rarely a garbled background capture like “la la la”). A real group:
| Variant | Text |
|---|---|
| clean | turn off the sofa light |
| wrong word | torn off the sofa life |
| wrong word + extra word | turn erm off the sofa like |
| wrong word | turn off the sofa life |
| wrong word | turn of the sofa light |
The hard part is that noise must never change the label:
- the words that carry the label are never dropped, and only replaced by confusions that keep the meaning (“on” → “one”, never “off”): dropping “on” from “switch light on” would give “switch light”, a toggle;
- inserted fragments never contain a word that could change the label, and a script checks that no “off” or “of” ever appears in an “on” example.
Splitting by phrasing family
The rule: never let a phrasing, or a near-copy of it, be in two splits, or the model is tested on sentences it has already seen.
- All five variants of a group go to the same split.
- Groups are gathered into families: the same set of words in any order, ignoring articles, “please”, her name, a few small words, and “light” or “lamp”. “turn on the desk lamp” and “turn on desk” are one family, and a family never spans two splits. On an early dataset, an independent check found that about 10% of the test groups had a near-copy in train; they scored 98% and inflated the headline by about half a point.
- Each action token gets 15 groups in test and 15 in eval (16 for one token);
<unknown>gets its proportional share.
Automatic checks, such as no shared text, group or family between splits and a ±10% balance between tokens, must pass before any training. Today the dataset holds 27,810 examples in 5,562 groups: 20,280 for training, 3,765 for test and 3,765 for eval. 20% are clean.
Measuring Honestly
| Split | Used for | Never used for |
|---|---|---|
| train | fitting the weights | any decision |
| test | choosing: early stopping, best checkpoint, threshold, which run to keep | fitting |
| eval | watching, to detect that I overfit test | any decision |
Early stopping: every 200 steps, the Trainer measures test, keeps the best checkpoint (saved weights) so far, and stops after 3 measures without progress. To overfit is to fit the examples you look at rather than the general pattern, and test numbers are a bit optimistic, because test chooses the checkpoint, the threshold and the run. Eval is the honest witness, measured at the same moments and only logged. The Hugging Face Trainer prefixes every evaluation metric with eval_, so only test is passed to it, and the eval split goes through a separate callback.
I follow two scores: accuracy, the share of right answers, and macro-F1, which scores each token on its own (balancing missed requests against false alarms) and then averages them, so a small token weighs as much as a big one. Early stopping and run choices use test macro-F1. On the current router, eval minus test macro-F1 is 0.0 points (−2.0 to +1.8): no sign that I overfit test.
Those brackets are 95% confidence intervals. The five variants of a group are not independent: if the clean sentence is wrong, its variants usually are too. So the intervals come from a group bootstrap: draw the held-out split’s 750 or so groups at random with replacement, compute the score, repeat 1,000 times, and keep the middle 95%. In practice:
- the headline accuracy is known to about ±1.6 points: 92.0% (90.4–93.5%) on the current test;
- one wrong group moves a class’s recall by about 7 points, since each class has 15 groups per split;
- two models on the same split are compared with a paired bootstrap.
Most differences between my runs are inside these bars.
Method: How Much of the Model to Train
The first measurements, on the first dataset versions:
A random head; the head alone; head, final norm and last 2 blocks; the same after two dataset reworks; then 4 blocks on the same data as the bar before it (the last two allow 5 epochs instead of 3). The head-only bar is a scratchpad run before the training code existed (learning rate 3e-3, no interval, no macro-F1), hence accuracy here. Test sets differ between dataset versions: read the trend, not the exact gaps.
- Head only, 71.1%: the frozen model already “understands” the sentences, but a single linear layer on top of it struggles to separate close actions. 291 of its 701 errors were on/off/toggle mix-ups on the right light.
- Last 2 blocks, 90.9%: +19.8 points, the big jump. Same-light mix-ups fell from 291 to 56. Training the last blocks lets the model weigh the small words that decide the action, like “on” and “off”.
- Last 4 blocks, 93.4%: on the same data as 2 blocks, the test gain (+0.8 points) is inside its paired interval (−0.7 to +2.2), so not significant on test. Eval gained +1.5 (+0.3 to +2.9), and wrong actions fell clearly, on a single seed. I took 4 blocks, for 0.19 instead of 0.15 s per step on the CPU.
A wrong action is worse than “sorry, I didn’t get that”, so I also count wrong real actions: the model fired an action, and it was the wrong one.
Same test set for the three bars. The threshold is explained below.
Early stopping had its own lesson: a noisy dip once stopped a run at 2,200 steps, undertrained, while the score was still rising. With a patience of 6, the same data gave 94.1% macro-F1 instead of 91.2%. I kept 3 as the default and rerun with 6 when a run stops in its first two epochs.
I never ran a long hyperparameter search: the plateau came from the data, and 90% is good while 95% is largely enough for a home robot.
Data: Where the Remaining Points Come From
Reading the errors

Every held-out group lands in one of three bins: all five sentences right, only noisy variants wrong (I check that noise’s realism), or the clean sentence wrong. On the current router, only 6% of the groups have a wrong clean sentence, but they carry 66% of the test errors: the model has never seen that way of saying it. More training won’t fix them; new phrasings will. The flip rate (5.4% on test) is how often a noisy variant gets a different answer than its clean sentence.
It was already true after the first real run: 60% of the test errors came from 32 groups whose clean text was wrong. “Turn on the main light” failed 5 times out of 5, because “main light” had one training example. The first dataset rework (realistic phrasings, gaps filled) took test errors from 220 to 184, on a reshuffled test set, and the F1 of <unknown> from 69% to 76%, with the same code.
Coverage: one wording is not enough
A cue that exists as a single family lands entirely in train or entirely in test, where it fails 5 times out of 5. My rule now: every important cue gets at least 3 differently worded families.
The other side: adding or removing one line reshuffles the split of every later token, and a sentence safely in train can move to test. That happened:
- “not much”, the answer to “what’s up?”, was held out once and switched every light on (
<lights_all_on>, 0.99); - “remind me to buy milk” was held out once and went to the shopping list’s check-off action: milk would have been checked off my list.
So a short list of one-liners and dangerous near-misses is pinned to train (TRAIN_ONLY) whatever the shuffle. And since the test set itself changes, every comparison across dataset versions is indicative only, unless it is limited to the groups held out in both versions.
Contrast: the <music> token
When Barbara got Spotify on her speaker, I added <music> with 113 phrasings. The first run looked fine on paper: 94.7% music recall on test (the share of music requests that reached <music>). But its errors showed TV, video and radio requests going to music: “put on netflix”, “play a video on youtube”, “turn the tv volume up”, “play france info”, and “add this song to my playlist”, though playlist edits aren’t supported.
The fix was contrast lines: 20 new <unknown> lines for the TV, videos, the radio, games and playlist edits (“open netflix”, “lower the tv volume”), and reworded music lines for what the music side lacked (“music for cooking”, “what’s playing”).
Part of the gain was the reshuffle: some of the failing sentences moved to train. On the 8 groups held out in both runs, wrong answers went from 22 to 11 out of 40, and none of the 35 held-out sentences from the new music lines was wrong. But “add this song to my playlist” still goes to <music> 5 times out of 5, with a probability around 0.9999, and “turn down the volume a little” still dims the bedroom. The headline barely moved (test macro-F1 93.4% → 93.2%; eval 94.7% → 93.2%, mostly through <unknown>, both inside their intervals): the gain is in the sentences that matter, not in the average.
Adding tokens without losing accuracy
Barbara went from 34 to 47 tokens in nine days (metro, news, shopping list, calendar, reminders, Christmas, wake-ups, music), each through the same loop: rules, phrasings, near-misses moved out of <unknown>, a run, its errors, contrast lines, another run.
Macro-F1 here, because it is the score that picks the runs. Each point has its own test set, and the intervals are about ±1.6 points.
The score drifts down by about 1.7 points while the task grows from 34 to 47 choices, many of them close to each other (three shopping actions, three reminder actions).
Reading the Loss Graph
Every run writes ten diagnostic graphs, each flagged OK, WATCH or PROBLEM. The loss graph tells the most.

The loss measures how surprised the model is by the right answer: very high when it is confidently wrong. The horizontal axis counts steps (one batch of 16 sentences); an epoch, one pass over the training set, is about 1,270 steps. The top panel uses a log scale, so each grid line is ten times the one below. The green training loss falls to 0.003: the model knows its training set almost by heart, normal for 22M parameters on short sentences.
The blue (test) and orange (eval) lines are the loss on sentences it never trained on. They reach their minimum early (test 0.588 at step 1,800) and then slowly rise. Yet the score kept improving until step 3,800, the checkpoint I kept. Both are true at once: the model gets more answers right, and its wrong answers get more confident. Loss punishes a confident mistake hard, accuracy doesn’t. That is overconfidence: probabilities higher than the real chance of being right.
Calibration and the 0.8 Threshold

The router always returns a probability with its token. If the top probability is under a threshold τ, Barbara ignores the action and answers through chat instead. In the left panel, moving τ to the right turns more wrong actions (solid lines) into harmless chat answers, but also sends more action requests to chat (dashed lines), some of them answered right before. The right panel shows the error rate among the answered requests as the router answers more of them, most confident first: lower is better.
At τ = 0.8, chosen on test, wrong real actions fall from 271 to 219, for 28 correct actions that now get a chat answer: a good trade.
Why not a higher threshold? Because of overconfidence. A model is well calibrated when its 90% answers are right 90% of the time. Mine are not: 57% of its test errors come with a probability of 0.99 or more, and no sensible threshold can catch those. Its AUROC, how well confidence separates right from wrong (1 is perfect, 0.5 is chance), is 0.90 on test and 0.86 on eval. The calibration error (ECE, the average gap between confidence and actual accuracy) is 0.068 on test and 0.069 on eval, flagged PROBLEM. Confident errors need data. The rest needs calibration.
Training on the Orin’s GPU
The same Docker setup trains on my laptop’s CPU or on the Orin’s GPU, where it uses bf16 (16-bit numbers instead of the CPU’s 32-bit fp32): a step takes 0.22 s there against 0.38 s on the laptop, and the current router trained in 986 s. On the CPU, 8 threads was the fastest setting and 16 threads 20 times slower, because the hybrid processor’s efficiency cores stall the others.
What’s Left for the Router
- Temperature scaling. Divide the scores by one number T, fitted on test, so the probabilities mean what they say; judge the calibration on eval, then re-choose τ on test.
<unknown>is still the weakest class: 69.6% F1 on test. On eval, it is recognized only half the time: lights in rooms I don’t have go to the living room or bedroom lights, holidays go to<christmas>, travel questions go to<metro>. More near-misses and contrasts.- Known misses: the two music ones above, and “why is the bedroom so dark”, which still goes to
<unknown>instead of switching the bedroom on. - Real transcripts. Every test sentence today is generated. Barbara’s exchange log keeps what she really heard; once labelled, those sentences become the eval set that no generator can flatter.
Part 3: build, simulate and deploy.