Zero Labs
Industry News / AI

Build a Jev-Style Decision Model from an LLM, Part 3: Training the Jev Head

Source · Towards AI By Divye Singh

From the publisher’s feed, collected in the ZeroLabs newsroom.

How four vectors become probabilities: the pointer head that scores the options, how it is trained and how far a frozen backbone can take it.Photo by Alex Knight on UnsplashWhere Part 2 left offPart 2 ended with a record written into one token sequence, with one-token markers at known places. One forward pass through the model gives a vector at every token. A vector is a list of numbers and here each list has 1,024 numbers. We keep four of them for a question with three options: one at the end marker of each option and one at the final decide marker.This part turns those four vectors into probabilities. It also shows how far a frozen model can go. Frozen means the original model is never changed. Only a small scoring head on top of it learns. By the end, a head with 524,288 trainable parameters reaches 0.959 accuracy on 800 synthetic tickets. Synthetic means a program made the tickets from templates. Random guessing on the same questions scores 0.352.One example runs through the whole article. Imagine a support ticket where a customer says they were billed twice. The question asks which department should handle it. The options are billing, technical and sales. The right answer is billing.The whole pipeline, with this part marked:What this part covers:What the four vectors look like before any training and why raw scores fail.The pointer head, which scores any number of options with one small set of numbers.The order effect, where the same option scores differently when the list is reordered.Training on 12 tickets and then on 800 tickets, with the best recipe after each experiment.Calibration, which asks whether a stated probability of 0.8 is right about 80% of the time.Where it stops: the model underneath never learns here. That is the limit of this part and Part 4 removes it with LoRA and then adds images.What the vectors look like before trainingBefore designing any scoring head we looked at the raw vectors. This was one of the most useful steps of the project. We ran the billed-twice ticket with the three department options and printed four kinds of numbers.Three words first. The length of a vector is how big it is. For the vector (3, 4) the length is 5, because 3 x 3 + 4 x 4 = 25 and the square root of 25 is 5. The dot product of two vectors multiplies the matching numbers and adds them up. For (2, 1) and (3, 4) it is 2 x 3 + 1 x 4 = 10. The cosine compares direction only. A cosine of 1 means two vectors point the same way. A cosine of 0 means they are unrelated. The three are linked by one formula:Here a and b are the two vectors and the double bars mean length. Theta is the angle between them.The first dot product follows from the formula: 116.4 x 125.1 x 0.596 = 8,679. The lengths are near 115 and 125 and so every dot product lands in the thousands. That happens no matter what the model thinks about the ticket.Why the raw scores failThe standard fix is to divide the dot product by the square root of the vector size. The vectors have 1,024 numbers and the square root of 1,024 is 32. We tried it. It is not enough:Softmax exponentiates each score and shares the total out. A score gap of 46.7 between billing and technical gives a factor of e^(46.7). That is more than 10²⁰. All of the probability lands on one option.The model cannot learn from this. The signal to improve an option shrinks with the probability it gets and 5 x 10^-21 is nothing. Compare a gap of 2. The scores 2 and 0 give e² = 7.39 and e⁰ = 1. The total is 8.39. The shares are 7.39 / 8.39 = 0.881 and 1 / 8.39 = 0.119. Both options still get a clear signal. A gap near 45 gives none.Two facts that decide the designThe vectors are nearly parallel. The option cosines are 0.82 to 0.98. A large shared part of every vector drowns the small part that tells the options apart.The scores need proper scaling. Without it the softmax saturates on the first step and training stalls.The first fact means a plain dot product cannot work. We need learned projections that keep the small directions that matter and ignore the huge shared part. The second fact means the head must also keep the scores small. The pointer head in the next section does both.The pointer head and the order effectThe pointer headAn ordinary classifier has a fixed output layer. It might have 3 outputs for 3 classes. Our options change with every question. One question has two options and another has five. One has “billing” and another has “calm”. A fixed output layer cannot represent that.So the head scores each option using that option’s own vector. This design is called a pointer, because it points at one of the items in its input. It uses two learned projections. A projection is a table of numbers that turns a list of 1,024 numbers into a shorter list of 256. One projection handles the option vectors. The other handles the decide vector. The score of option k is:h_k is the vector of option k and h_d is the decide vector. W_o and W_d are the two learned projections. d_head is the projected size, which is 256 here. The small T on top just says that we take the dot product of the two projected lists.This is how the head answers the two facts above. The projections keep the small directions that tell the options apart and drop the huge shared part, and the division by the square root of 256 keeps the scores small from the first step. Each projection holds 1,024 x 256 = 262,144 numbers. Two of them hold 524,288. That is 0.061% of the 853.5 million numbers in the whole model.The head works for any number of options in any order. It never learns anything about option identity. It only learns how an option’s vector relates to the vector of the question.Here is the arithmetic on a miniature version. Pretend the projected lists have only 4 numbers, so we divide by the square root of 4, which is 2. Pretend the projected decide vector is (1, 1, 1, 1). Then the dot product with any option is just the sum of that option’s numbers.The total of the “e to the score” column is 7.389 + 1.649 + 0.368 = 9.406. Each probability is its value divided by 9.406. For billing that is 7.389 / 9.406 = 0.786. A fourth option would be one more row. Nothing in the head changes.A real head before any training gave the scores 1.546, -1.107 and -0.847 on our billed-twice ticket. Its probabilities were 0.861, 0.061 and 0.079. These come from random starting numbers. They are meaningless except for one thing: they are small and sane, so learning can start.Padding and the masked lossQuestions have different numbers of options. A batch is a group of questions processed together and it needs one shape. So we pad every question up to the longest one in the batch. A question with 3 options gets 2 empty slots when the longest has 5.The empty slots must not take part in the softmax. We set their scores to minus infinity first. Then e to that score is 0 and they get no share. The loss for one question is the negative log of the probability given to the true option, over the real options only:s_y is the score of the true option. Forgetting the mask gives a loss that is wrong but finite. Take the billing question again. Add two empty slots that hold the junk value 9:The loss is the negative natural log of the probability. For the correct case that is -ln(0.786) = 0.241. For the bug, e⁹ = 8,103 for each junk slot, so the total is 9.406 + 2 x 8,103 = 16,215. Billing gets 7.389 / 16,215 = 0.00046.Nothing crashes. The training just learns from junk. This is the kind of bug that survives silently. So we test the batched loss against a one-question-at-a-time computation. On a real batch of 32 questions the masked loss was 1.138307 and the one-at-a-time loss was 1.138307. Without the mask the batch loss was 1.754511.The order effect nobody mentionsThe head is symmetric. It applies one function to every option. But its inputs are not symmetric. The model reads left to right. The vector for the third option has read the first two options. The vector for the first option has read neither.We tested it. We reversed the order of the options and mapped the scores back to the original order. This used the untrained head from above:The same option’s vector had cosines of 0.818, 0.988 and 0.817 with itself across the two orders. These are high but not 1. That was enough to flip the top choice of a random head from billing to sales.This is a property to manage and not a bug to remove. The remedy is to show each question in many shuffled option orders during training. The label must move with its option. Imagine the options listed as billing, technical and sales with the label “option 1”. After shuffling to sales, billing, technical the label must become “option 2”. If the label is not moved, the model trains on wrong answers and nothing crashes.We measure the effect with an order-stability metric. Among choice and noul questions, it is the share that get the same option text in every ordering. Say 10 questions are each asked in 4 orders and 7 of them get the same answer every time. Stability is 7 / 10 = 0.7. It is separate from accuracy. A model can be fully stable and fully wrong.Training experiments and what they taught usEverything in this section trains only the small head on top of a frozen model. The backbone never changes. So we run it once and cache the vectors it produces. Then we train the head on the cache. An epoch is one pass over the training data. On cached vectors an epoch takes a fraction of a second. The full 30-epoch run on 800 tickets took 5.5 seconds. Extracting the vectors took about 112 seconds, once.A word on how we report numbers. A seed is a number that fixes the random choices in a run, such as the starting values of the head and the order of batches. Running the same settings with several seeds shows how much of a difference is luck. A number after the plus-minus sign is the spread over seeds. We label two kinds of results. A result is a 3-seed average on the test set or the one-time test number named as such. Best observed, not confirmed is the winner of single-seed or validation-only runs. It may be better than the result and it may be luck.First attempt: 12 hand-written ticketsWe wrote 12 support tickets with three questions each. The questions were the department (choice, 3 options), whether to escalate (noul, yes or no) and how frustrated the customer is (score, three ordered levels: calm, frustrated and furious). We used 8 tickets for training and kept 4 aside as held-out data. We split by ticket. Imagine ticket 9 is held out. Then none of its three questions appear in training.Chance depends on how many options each question has. Random guessing scores 1/3 on the department, 1/2 on the escalation and 1/3 on the frustration. The average is (0.333 + 0.5 + 0.333) / 3 = 0.389.Training accuracy of 1.000 looks wonderful and means nothing here. Eight tickets with three questions each are only 24 distinct facts. The head has 524,288 trainable numbers, which is about 21,800 per fact. That can memorise anything this small. We confirmed it: with a single training ticket, training accuracy was already 1.000.The first run also started with a loss of 26.6. A model that knows nothing gives each of 3 options 1/3. Its loss is -ln(1/3) = 1.10. So 26.6 means the scores were wildly off from the first step.The learning rate. The learning rate is the step size of each update. The optimizer (AdamW) moves every trainable number by roughly the learning rate on every step. Here is a back-of-the-envelope estimate, not a measurement. A typical entry of a vector is about 3.4. The 1,024 absolute values add up to roughly 2,800. A projected value then changes by about the learning rate times 2,800 per step. At 1e-3 that is 2.8 per step. The scores swing by whole units and a score that jumps like that cannot settle.These are 3 seeds each with 6 shuffled option orders in training and 40 epochs. With only 1 order, stability stayed near 0.53 at the three lower rates. So shuffling raised stability from about 0.53 to between 0.78 and 0.86 with no change in accuracy. At 1e-3 training is unstable and the comparison means nothing. At that rate a single run even showed shuffling hurting stability. Report seeds.Regularization did not help. Regularization means a restriction that stops a model from memorising. Strong weight decay (a pull of every number toward zero), a much smaller head and early stopping all failed to move held-out accuracy beyond noise. The problem was not too much capacity. It was too little information.The learning curve on this tiny set went up but was noisy. It is the mean of 5 seeds at a learning rate of 1e-4.Two more lessons. First, a baseline that always predicts the most common label scored 0.333. That is below random guessing (0.389), because the held-out label mix differed from training. So random guessing is the honest reference. Second, a result from one seed at an unstable learning rate can point the wrong way.Best after this experiment, best observed, not confirmed. Nothing here is a result. Four held-out tickets are 12 independent questions and that is too few to confirm anything.The honest reading is that the head needs more data. That is what the next experiment tests.Getting real volume: a synthetic generator and what it cannot tell youWe needed thousands of examples. The open-source any2jev project has a generator that makes synthetic support tickets with rule-based labels. Every label is correct by construction. We ran it in a throwaway environment and wrote our own converter into our format. A check compared every converted label with the generator’s original answer key. There were 0 mismatches over 3,000 tickets.The converter made a few decisions:A choice option that carries a description becomes “key: description”. The model then sees the description right before the end marker of that option.Structured tickets, such as a dictionary or a chat log, are flattened into text.Ordered scales keep their order.The four questions in this data are department (choice, 3 to 5 options), refund (noul), tone (choice, 3 options) and urgency (score, 3 levels). Chance accuracy on validation and test is 0.352.Here is the warning label. This data is template-based and the labels are encoded in keywords. There are eight phrases per department. Urgency and tone use fixed phrases such as “URGENT”, “no rush” and “furious”. A model can score highly by spotting words. High accuracy here shows that the pipeline works. It does not show that the model would handle real tickets.At scale: 800 training ticketsOur first run on the synthetic data used a one-cycle learning-rate schedule (explained below) with a peak of 1e-3, 30 epochs, batches of 64 and seed 0. Validation loss fell fast and stopped improving at about epoch 21. Training loss kept falling. So the head still memorised some training tickets.Results by question for this first run:Chance on this data is 0.352. The test set was used once, after every decision had been made on validation. It agreed with validation. That agreement is the evidence that our tuning did not overfit the validation set. What produced this, one lever at a time:More data. Every doubling of the training tickets helped. The gain per doubling was 0.093, 0.105, 0.074 and 0.084 up to 400 tickets and 0.030 for the last one. The curve was slowing but had not flattened. We are limited by data and not by capacity. The numbers are the mean of 3 seeds with 15 epochs and the one-cycle schedule, on validation.Best after this experiment (validation, 3 seeds): 800 tickets, 6 option orders, one-cycle schedule with a peak of 1e-3, 15 epochs and batches of 64 gave 0.949 +- 0.001 accuracy. There are 2,400 training tickets in the set, so we did not run out of data.A learning-rate schedule. A constant learning rate keeps bouncing around the best point late in training. A one-cycle schedule fixes that. The rate climbs from near zero to a peak and then falls smoothly to near zero. With 14,520 examples at 64 per step, one epoch is 14,520 / 64 = 227 steps. Thirty epochs are 6,810 steps. The first 681 steps (10%) climb to the peak. The other 6,129 steps follow a half cosine curve down.The spread across seeds fell from 0.038 to 0.001. That is a factor of 38. With a constant rate, validation accuracy dipped to 0.858 at epoch 16 and the validation loss spiked to 1.354 at epoch 25 on one seed. So the result depended on which epoch you stopped at. The schedule ends with tiny steps and settles.Input normalization is the last row of the table. RMS normalization divides each vector by its root mean square. That is the square root of the average of its squared numbers. After that, every vector has a typical size near 1. We apply it before the projections. It does not change accuracy once a schedule is in place. But it nearly halves the validation loss, from 0.208 to 0.126. The accuracy is the same and the probabilities are better. Before the schedule existed, normalization was what kept high rates from diverging. In an early 8-epoch run with a constant rate of 1e-3 and one seed, it ended at validation loss 0.230 and accuracy 0.915. Without it the figures were 1.053 and 0.873.We also compared peak rates and epochs. These are 5 seeds each on validation:Gradient clipping caps the size of one update. It gave 0.952 against 0.949 at 15 epochs. That gap is inside the noise, so we did not use it. A higher peak of 3e-3 got close at 15 epochs, but 1e-3 for 30 epochs was the best overall. The schedule needs enough steps to learn the harder questions.Best after this experiment (validation, 5 seeds): one-cycle schedule, peak 1e-3, 30 epochs, batches of 64 and RMS input normalization gave 0.960 +- 0.001 accuracy and a loss of 0.126.Shuffling the options. In this test the labels moved with their options. The numbers are 3 seeds on validation.Accuracy rose by 0.062 and stability by 0.166 for no extra tickets. Part of the accuracy gain is data augmentation, because the head sees each question six ways.The final recipe for the frozen backboneThis is the result of the frozen-backbone part of the project. The test number comes from one seed. The calibration section below reports 3-seed test averages for a retrained copy of the same head.Looking at the mistakes and being wrong about themAccuracy is one number. The errors say what to fix. We predicted that the hard case would be “none of the above”. The generator sometimes deletes the true department from the options and adds a replacement answer. It uses three wordings for that: other, not_listed and none. The data disagreed with us:Only one of the three “none of the above” wordings was hard. The worst true answer was returns. Reading the actual tickets showed why. Several are debatable. Imagine a ticket that says "The screen was cracked when I opened it." The label is returns. A model that answers technical is not being silly. That one phrase accounted for four of the ten worst tickets.Three of the worst tickets, in plain words:“The tracking number shows no updates for a week. I want my money back.” The label is shipping. The model was wrong in all 4 orders.“The screen was cracked when I opened it. I need this fixed today.” The label is returns. The model was wrong in all 4 orders.“The courier left my parcel at the wrong address.” The label is other, because shipping was removed from the options. The model was wrong in 3 of 4 orders and chose returns.Two more facts about the counts. They are per option order, so one bad ticket appears up to four times. Counted per ticket, 66 of 235 department tickets had at least one wrong order. Only 4 were wrong in every order. So most of the remaining error is order sensitivity and not consistent confusion. The real weakness showed in the missing-answer case. When the true department is removed, the model picks the nearest one left. A lost parcel goes to returns instead of "none of these". Recognising that nothing matches is harder than matching.Our reasoning errors in this section, collected because they are the useful part:Calling a gap of 0.375 too big to be rounding (Part 2) before checking the number format.Treating “most common label” as the baseline instead of random guessing.Reading a single-seed result at an unstable learning rate as a real effect.Predicting the hard case from intuition and then having the data point elsewhere.Trusting a training accuracy of 1.000.Can you trust the probabilities?A decision system is only as useful as its probabilities. Say downstream code pages someone when the chance of “urgent” is above 0.8. Then 0.8 should mean right about 8 times in 10. Accuracy is blind to this because it only checks which option came first, so this section adds three measures that grade the probabilities themselves and then the cheapest repair, which is a single number called the temperature.Three measures of probability qualityWe use the billed-twice ticket again, where the true department is billing. Two models see it. An honest model picks billing and gives billing, technical and sales the probabilities 0.786, 0.175 and 0.039. A confident but wrong model picks technical and gives the probabilities 0.05, 0.90 and 0.05. Accuracy only records that the first model is right and the second is wrong, but the probabilities carry much more information than that, and the three measures below read it in different ways.NLL (negative log-likelihood) looks only at the true option. For each question it takes minus the natural log of the probability given to the true option, and the result is averaged over all questions. It is the loss we train on, and it punishes a confident wrong answer without limit:E is the number of questions. p_{e,k} is the probability given to option k on question e. y_e is the index of the true option. For our ticket the honest model scores -ln(0.786) = 0.241. The confident but wrong model scores -ln(0.05) = 2.996 because it gave the truth only 0.05.Brier looks at every option. Write the truth as a 1 for the correct option and a 0 for every other option. Take the difference from the model’s probability for each option, square each difference and add them up. Averaging over all questions gives the mean squared error of the probabilities:y_{e,k} is 1 for the true option and 0 for the others. Here is the arithmetic for our ticket:Three reference points make the scale easy to read. A perfect model scores 0. A model that spreads probability equally over three options scores 1–1/3 = 0.667, which is the bar to beat. A model that is completely sure and completely wrong scores 2, the worst possible value.ECE (expected calibration error) asks the calibration question directly. It groups the answers by how confident the model was and compares the confidence in each group with the accuracy actually achieved there. Then it averages the gaps, weighting each group by its share of all questions:B_m is the set of questions whose top confidence falls in group m. acc(B_m) and conf(B_m) are the accuracy and the mean confidence in that group. A single ticket cannot show this, so the next example uses ten. We checked all three measures by hand on small cases before trusting the code.No single measure is enough, which is why the results tables report all of them:Brier works best as the headline number for probability quality because it is bounded, grows smoothly with the error and charges a wrong answer given with low confidence far less than one given with high confidence. NLL and Brier are also proper scoring rules, which means that in expectation they are lowest only when the model reports the true probabilities. That makes them sensible training signals as well as metrics. Accuracy is not a proper scoring rule.An example where accuracy hides the problemImagine ten tickets on which a model answers “escalate” with a confidence of 0.9 every time and is right on 7 of them. A second model gives the same answers but says 0.7 every time. Both have an accuracy of 0.7, yet only the second one is honest.The arithmetic for the first row follows. The ECE is the gap between 0.9 and 0.7, which is 0.20. The NLL charges -ln(0.9) = 0.105 for each of the 7 right answers and -ln(0.1) = 2.303 for each of the 3 wrong ones, so the average is (7 x 0.105 + 3 x 2.303) / 10 = 0.765. For Brier a right answer has two squared errors of 0.1 x 0.1, which total 0.02, and a wrong answer has two of 0.9 x 0.9, which total 1.62. The average is (7 x 0.02 + 3 x 1.62) / 10 = 0.50.The answers and the accuracy are the same, yet every probability measure improves once the stated confidence matches the truth. This matters in practice. Suppose a rule auto-closes a ticket when the confidence is at least 0.9. The team budgets for an error rate of 0.1, which means 100 wrong closures per 1,000 tickets. If the model is right only 0.7 of the time at that confidence the real figure is 300, so 200 wrong closures per 1,000 tickets are unplanned. A report that shows only accuracy would not reveal them.Temperature scalingTemperature scaling is the simplest repair. It divides every score by one number T before the softmax, so a T above 1 flattens the probabilities and a T below 1 sharpens them. The winning option never changes, which leaves the accuracy untouched:On our billed-twice scores, T = 2 gives:At T = 2 the exponentials are e^(1.0) = 2.718, e^(0.25) = 1.284 and e^(-0.5) = 0.607. Their total is 4.609, so billing gets 2.718 / 4.609 = 0.590. The winner is the same and its probability drops from 0.786 to 0.590.For the ten-ticket example, going from 0.9 to 0.7 means shrinking a score gap. For a yes or no the gap is ln(p / (1 — p)), which is ln(0.9 / 0.1) = 2.197 for 0.9 and ln(0.7 / 0.3) = 0.847 for 0.7. So T = 2.197 / 0.847 = 2.59.In practice T is fitted by minimising NLL on held-out data, which is the validation set, and then judged on a different set, which is the test set. It has one parameter so it cannot overfit much. The fitted value is also a diagnostic, since a T above 1 means the model was overconfident.We also found a bug here. The first version returned NaN on real data. NaN stands for “not a number” and one NaN poisons the whole batch. Padded option slots hold minus infinity, and dividing minus infinity by a trainable T gives a NaN gradient even though those slots carry no weight in the loss. The unit test missed it because it used finite scores everywhere. The fix replaced padded scores with 0 before dividing, and the test now uses padded tensors that look like the real ones. We also limit T to between 0.05 and 20. A model at chance wants T as large as possible, and the limit stops it from returning a silly value.How trustworthy is the frozen head?These are the results for the frozen-backbone head, retrained with seeds 0, 1 and 2 for the calibration study. The recipe is the final recipe from the previous section with a cross-entropy loss. T is fitted on validation and applied to test.The fitted temperature was 1.41 +- 0.02 on validation. That is a little above 1, so the head is slightly overconfident, and scaling fixes part of it. The probabilities were already close to honest, with an ECE of only 0.013. Per question on test the accuracies are department 0.882, refund 0.992, tone 0.985 and urgency 0.994. The department question holds almost all of the remaining error.Brier as a training signal. The Brier score can be differentiated, so it can also be added to the loss. We used cross-entropy plus 1.0 times Brier. Cross-entropy reacts violently to confident mistakes, while Brier looks at the whole probability vector and has a gentler pull on outliers. Training on the metric we report should in principle help, and it costs one extra term and no extra compute. For the frozen head it made no measurable difference. The test Brier was 0.055 against 0.056 and the accuracy was 0.965 against 0.963. The remaining errors come from features the head cannot change. Part 4 reports the same comparison for an adapted backbone, where it helps a little.Best after this experiment, result (test, 3 seeds): the frozen-head recipe above with temperature scaling. T is fitted by minimising NLL on validation and came out at 1.41. The test numbers are in the last column of the table above.Where this leaves usWe now have a working decision model on a frozen backbone. A head with 524,288 trainable numbers turns four vectors into probabilities. On synthetic tickets it reaches 0.963 accuracy over three seeds on the test set. Random guessing gives 0.352. Its order stability is 0.912 and its Brier score is 0.055 after temperature scaling.The limit is the frozen backbone. The head can only use what the model already puts at the decide token. The department question shows it. It stays at 0.882 on test while refund, tone and urgency are at 0.985 or above. Training longer, regularizing and calibrating did not move it. Order sensitivity is part of the same limit. About 1 question in 11 changes its answer when the options are reshuffled. Part 4 lifts this limit by letting the backbone learn with LoRA. It then gives the model eyes.What this part does not give youA real-text result. Every number here is from templated synthetic tickets. We have not tested on public datasets such as boolq, ag_news, banking77 or sst5.The full training set. We used 800 of the 2,400 training tickets. The learning curve was still rising.A multi-seed test number for the 800-ticket run. The notebook test result of 0.959 is one seed, used once. The 3-seed test numbers come from the retrained copy in the calibration section.Confirmed winners from the 12-ticket experiments. Everything there is best observed, not confirmed.Check out Part 4, where the frozen backbone finally learns through LoRA to fix the weak spots of the head and the same Jev gets eyes to decide from screenshots as well as text.Build a Jev-Style Decision Model from an LLM, Part 3: Training the Jev Head was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.