TemperatureScaling<T>

Models & Types · AiDotNet.UncertaintyQuantification.Calibration

Implements temperature scaling for probability calibration.

For Beginners

Temperature scaling is a simple but effective way to calibrate neural network probabilities.

Neural networks often output probabilities that don’t match reality:

  • They might be overconfident: predicting 99% when accuracy is only 70%
  • Or underconfident: predicting 60% when accuracy is actually 95%

Temperature scaling fixes this by dividing the network’s logits (pre-softmax values) by a learned temperature parameter before applying softmax:

  • Temperature > 1: Makes predictions less confident (softer)
  • Temperature < 1: Makes predictions more confident (sharper)
  • Temperature = 1: No change

This is calibrated on a held-out validation set to find the temperature that makes the predicted probabilities match actual frequencies.

Example: If a model predicts 80% confidence for 100 cases, and 80 of them are actually correct, the model is well-calibrated. If only 60 are correct, the model needs calibration.

Constructors

Constructor Summary
TemperatureScaling(double initialTemperature) Initializes a new instance of the TemperatureScaling class.

Properties

Property Summary
T Temperature { get; set; } Gets or sets the temperature parameter.

Methods

Method Summary
Tensor<T> ScaleLogits(Tensor<T> logits) Applies temperature scaling to logits.
void Calibrate(Matrix<T> logits, Vector<int> labels, double learningRate, int maxIterations) Calibrates the temperature parameter using validation data.