-
Notifications
You must be signed in to change notification settings - Fork 132
temperature scaling for llm probabilities
Temperature scaling recalibrates a model by dividing its logit by a single number T before turning it back into a probability: T above 1 pulls answers toward 0.5, T below 1 pushes them toward 0 and 1. T is fitted on held-out labelled cases by minimising log loss, and because it never moves an answer across 0.5, it changes how confident the model looks without changing which answer it gives. It fixes a model that is uniformly too sure or too shy. It cannot fix a model that leans in one direction on some kinds of question.
That second sentence is the reason for this page. When we fitted a temperature and a bias on our development split and applied them to 999 new questions, accuracy moved from 0.757 to 0.759. The model's problem on those questions was not its confidence level; it was a lean toward yes on questions it could not compute, and a global correction cannot reach that.
This page is the mechanism for a single yes/no probability, where the method comes from, what it repairs, what it cannot, and whether to apply it to your own answers.
A logit is the log-odds of the probability, z = log(p / (1 - p)). Temperature scaling replaces p with sigmoid(z / T). For a yes/no answer this has a neat form: the odds are raised to the power 1/T.
A worked example with illustrative numbers. Take P(yes) = 0.9, odds 9 to 1:
| T | New odds | New P(yes) |
|---|---|---|
| 0.5 | 81 to 1 | 0.988 |
| 1 | 9 to 1 | 0.9 (unchanged) |
| 2 | 3 to 1 | 0.75 |
| 4 | about 1.73 to 1 | about 0.63 |
A P(yes) of 0.5 stays 0.5 for every T, and a 0.1 moves exactly as far toward or away from 0.5 as the 0.9 does. That symmetry is what makes the method safe, and also what limits it. The logit scale itself is explained on logits, log-odds and P(yes).
Guo and colleagues (2017) found that modern neural networks are poorly calibrated, compared several post-processing fixes, and concluded that on most datasets temperature scaling, "a single-parameter variant of Platt Scaling", is surprisingly effective. scikit-learn's documentation describes the same method for multiclass problems: T is learned by minimising log loss on a hold-out calibration set, and since it does not move the maximum of the softmax, it does not alter accuracy.
The relative with one more parameter is Platt scaling, which also fits an offset. That offset can move answers across 0.5, which is exactly the part that can change accuracy.
- Uniform overconfidence. If the model's 0.95s are right 85 percent of the time and its 0.05s are yes 15 percent of the time, a T above 1 pulls both in.
- Uniform underconfidence. If the 0.7s are right 90 percent of the time, a T below 1 pushes them out.
- Probabilities you want to use as probabilities. Expected-cost thresholds and averages over a batch need calibrated numbers; see thresholds when a wrong yes costs more.
In every one of these cases, a reliability diagram of your labelled answers shows the shape first: a curve flatter than the diagonal for overconfidence, steeper for underconfidence.
On 999 yes/no questions written after the model was finished, jevos-q4_k_m made 152 errors
saying yes when the answer was no and 91 the other way. The lean is not spread evenly: the mean
P(yes) on questions whose answer is no is 0.59 for arithmetic and 0.53 for dates, but 0.17 for
negation and 0.16 for tone.
A temperature cannot fix that, because it treats a wrong 0.7 on a sum and a right 0.7 on a fact the same way. A bias term shifts every answer at once, so a shift large enough to fix the sums starts saying no to facts the model had right. What we measured:
| Correction | Accuracy on the 999 questions |
|---|---|
| none | 0.757 |
| temperature and bias fitted on dev | 0.759 |
| best bias chosen on the 999 set itself (cheating on purpose) | 0.763 |
| fitted on half the scenarios, tested on the other half | no gain |
The cheating row is the ceiling of what any global shift could recover: 0.006. The fix for a directional error lives in the question, not the probability: keep arithmetic and dates in code and ask the model what it reads, as described in small LLMs and arithmetic in yes/no questions.
Only if your own labelled cases show a uniform miscalibration, and only with care:
- Fit T on cases that you do not then use to judge it. scikit-learn's guidance for calibrators in general is to fit them on data independent of what the model was fitted on, and the same logic applies to your test set.
- Fit it per question or per kind of question if they behave differently. One T for a fact question and a date question averages two different problems.
- Refit when the inputs change. A temperature fitted on last quarter's tickets describes last quarter's tickets.
A minimal fit by grid search, on labelled P(yes) values p and answers y (1 for yes):
import numpy as np
z = np.log(np.clip(p, 1e-6, 1 - 1e-6) / (1 - np.clip(p, 1e-6, 1 - 1e-6)))
def log_loss(T):
q = 1 / (1 + np.exp(-z / T))
return -np.mean(y * np.log(q) + (1 - y) * np.log(1 - q))
T = min(np.linspace(0.25, 4, 76), key=log_loss)If the best T comes out near 1, the model was already calibrated on that data, and the answer is to leave it alone.
What is temperature scaling? Dividing a model's logits by a fitted number T before the sigmoid or softmax, to make its probabilities match observed frequencies.
Does temperature scaling change accuracy? Not on its own: it never moves an answer across 0.5. Adding a bias term, as in Platt scaling, can.
Is this the same temperature as in text generation? It is the same operation on logits, used for a different purpose: sampling diversity there, calibration here.
Why did recalibration not help jevos on new questions? Because the errors were a lean toward yes on specific kinds of question, not a uniform overconfidence.
How many labelled cases do I need to fit T? One parameter is cheap to fit; a few hundred labelled cases is a reasonable start, and more if you fit per kind of question.
See also: expected calibration error, explained, why a small LLM says yes when the answer is no and LLM calibration explained.
- Our measurements: the 999-question set on
jevos-q4_k_m(accuracy, 152 vs 91 errors, mean P(yes) by kind) and the recalibration experiments on it. - The T table is illustrative arithmetic, not a measurement.
- Guo, Pleiss, Sun, Weinberger (2017), On Calibration of Modern Neural Networks, fetched 2026-09-29.
- scikit-learn, Probability calibration, temperature scaling and independent calibration data, fetched 2026-09-29.
From the notes of jev. We tried the textbook fix on our own weakness, it moved accuracy by two thousandths, and we wrote down why.
- Ask a local LLM a yes/no question and get P(yes)
- Zero-shot text classification with yes/no questions
- LLM policy decisions: put the rule in the question
- LLM as a judge on a CPU
- Why a small LLM says yes when the answer is no
- Small LLMs and arithmetic in yes/no questions
- Our held-out benchmark said 0.855, new questions said 0.757
- jevos vs Jev vs Laya for yes/no decisions
- An open-source alternative to Jev for yes/no decisions
- jevos vs the OpenAI API for yes/no classification
- jevos vs Ollama for yes/no decisions
- jevos vs bart-large-mnli for zero-shot classification
- A yes/no LLM vs a fine-tuned BERT classifier
- jevos vs SetFit: zero-shot vs few-shot classification
- jevos vs Llama Guard for content safety checks
- jev serve vs llama.cpp server for classification
- jevos vs LM Studio: a decision server, not a chat app
- Local vs hosted LLM decisions: latency, cost, privacy
- A yes/no LLM vs a business rules engine
- LLM decisions vs keyword rules and regex
- The fastest AI model for yes/no decisions
- What makes a local LLM fast on a CPU
- Why one forward pass beats generating an answer
- Prefill vs decode: where LLM latency comes from
- Why LLM latency grows with the length of the text
- Why a hosted LLM API cannot answer in 50 ms
- Many questions about one text: why the extra ones are cheap
- CPU or GPU for a small LLM
- Latency budgets: where a 200 ms model fits
- Measuring LLM latency: median, p90 and warm-up
- Q4_K_M vs Q8_0: speed and size for a small model
- Throughput vs latency for a decision server
- What P(yes) means, and what it does not
- LLM calibration explained with yes/no answers
- Expected calibration error (ECE), explained
- Temperature scaling for LLM probabilities
- Platt scaling for a yes/no model
- Reading a reliability diagram
- How to choose a threshold for P(yes)
- Thresholds when a wrong yes costs more than a wrong no
- Human in the loop AI with a review band
- Precision and recall at a P(yes) threshold
- Base rates: why a 0.9 yes can still be wrong often
- Combining yes/no answers with AND, OR and NOT
- Logits, log-odds and P(yes)
- LLM confidence scores: probabilities vs self-reports
- How to write yes/no questions an LLM answers well
- Negation in yes/no questions for an LLM
- One condition per question: splitting compound questions
- Ask whether the text says it at all
- Scores as yes/no thresholds: is it at least high?
- Sending JSON as the text: designing the state
- Why wording changes an LLM's answer, and how to test it
- Mainly about: questions for messages with several topics
- Yes/no questions about tone and emotion
- Asking about intent: what does the writer want?
- Yes/no questions about long documents
- Using an English-only LLM with other languages
- Content moderation with a local LLM
- A Discord moderation bot with a local LLM
- Spam detection with yes/no questions
- Review moderation with a local LLM
- Email triage with a local LLM
- Support ticket routing with yes/no questions
- Urgency detection in customer messages
- Sentiment analysis with yes/no questions
- Intent detection with a local LLM
- Lead qualification with yes/no questions
- Fraud case triage with a local LLM
- Phishing email screening with a local LLM
- Log and alert triage with a local LLM
- Checking text for personal data with yes/no questions
- Prompt injection screening with a small model
- Document classification with a local LLM
- Product categorization with yes/no questions
- Contract clause detection with a local LLM
- Refund request triage with a local LLM
- Detecting cancellation intent in customer messages
- RAG evaluation with yes/no questions
- RAG faithfulness check with a local LLM
- Hallucination detection with a local LLM
- LLM regression tests in CI with yes/no checks
- Rubric design for an LLM judge
- Pairwise comparison with a yes/no judge
- LLM judge bias and how to control it
- Evaluation metrics for yes/no classifiers
- Building a yes/no test set for your own data
- Accuracy by kind of question: why one number hides failures
- Generating test questions with answers computed by code
- Benchmark contamination and truly held-out tests
- An LLM router with yes/no questions
- A model cascade: small model first, large model on doubt
- Semantic routing vs yes/no questions
- Gating AI agent tool calls with yes/no checks
- AI agent guardrails with yes/no questions
- Stop conditions for AI agents
- Logging LLM decisions for audit
- Reducing LLM cost with local yes/no decisions
- Replacing chat LLM calls with yes/no questions
- Structured output vs a probability
- A Python client for local LLM decisions
- Calling a local LLM decision server from JavaScript
- Local LLM yes/no decisions in n8n
- A Slack bot that uses local LLM decisions
- Home Assistant automations with local LLM decisions
- A LangChain tool for local yes/no decisions
- Batch decisions from files with jev decide
- Running LLM yes/no checks in GitHub Actions
- Securing a local LLM server with an API key
- curl examples for a local LLM decision API
- Self-hosted AI for decisions
- A private LLM for text classification
- On-premise LLM for business decisions
- GDPR and automated decision-making with an LLM
- Offline AI for decisions: no network needed
- Edge AI decisions on a CPU
- Run an LLM locally without a GPU
- Small language models explained
- When a small model is enough, and when it is not
- An LLM on a laptop: what it can do in real time
- What is GGUF, for someone deploying a classifier
- GGUF quantization types explained: Q4_K_M, Q8_0 and others
- GGUF vs safetensors
- llama.cpp vs Ollama for a classification service
- llama-cpp-python vs calling llama.cpp through ctypes
- llama.cpp on Windows without compiling
- Running llama.cpp CPU only
- Using llama.cpp prebuilt binaries instead of building