Temperature and vector scaling for multiclass probability calibration.
Classes
Multiclass calibration by dividing logits by a single temperature.
__init__( self, max_iter: int = 200, tol: float = 1e-06, log_t_bounds: tuple = ()) -> None, )
Overview
- Collect logits on a held-out calibration set.
- Fit a single temperature T by minimising the negative
- At transform time, apply \text{softmax}(z / T).
Theory
The calibrated probability of class k is
and T minimises the calibration-set cross-entropy
which is convex in 1/T, so a golden-section search on \log T finds the global optimum without gradients.
Parameters
max_iter
tol
log(T).
log_t_bounds
T in [0.018, 54.6].
Attributes
temperature_
n_iter_
classes_
fit.
fitted_
fit has been called.
Notes
Complexity. O(n \cdot c) per iteration for n samples and c classes; the number of iterations is fixed by tol, not by n.
When to use. Use temperature scaling for any multiclass model whose ranking must not change — the accuracy-preserving property is the reason it is preferred over per-class isotonic calibration for deep networks. It cannot fix class-dependent bias; reach for VectorScaler when different classes are miscalibrated in different directions.
References
See Also
>>> import numpy as np
>>> from tuiml.uncertainty import TemperatureScaler
>>> from tuiml.uncertainty import expected_calibration_error
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 3, 300)
>>> noisy = np.where(rng.random(300) < 0.3, (y + 1) % 3, y)
>>> logits = np.eye(3)[noisy] * 6.0 + rng.normal(0, 1.0, (300, 3))
>>> scaler = TemperatureScaler().fit(logits, y)
>>> bool(scaler.temperature_ > 1.0) # the model was over-confident
True
>>> proba = scaler.transform(logits)
>>> bool(expected_calibration_error(y, proba) < 0.1)
True
Methods
fit
(self, scores: np.ndarray, y: np.ndarray) -> 'TemperatureScaler'
fit
(self, scores: np.ndarray, y: np.ndarray) -> 'TemperatureScaler'
Fit the temperature on held-out logits.
Parameters
scores
log.
y
Returns
self
transform
(self, scores: np.ndarray) -> np.ndarray
transform
(self, scores: np.ndarray) -> np.ndarray
Apply the fitted temperature and return calibrated probabilities.
Parameters
scores
Returns
proba
Multiclass calibration with a per-class scale and bias.
TemperatureScaler by learning one weight and one bias per class, z_k \mapsto w_k z_k + b_k. It can correct class-dependent miscalibration that a single temperature cannot, at the cost of 2c parameters and the loss of the accuracy-preserving guarantee.__init__( self, max_iter: int = 500, learning_rate: float = 0.05, tol: float = 1e-07) -> None, )
Overview
- Collect logits on a held-out calibration set.
- Fit w and b by gradient descent on the cross-entropy.
- At transform time, apply \text{softmax}(w \odot z + b).
Theory
The calibration map is
with the objective convex in (w, b), so plain gradient descent with a decaying step reaches the optimum. The gradient of the mean cross-entropy is
Parameters
max_iter
learning_rate
tol
Attributes
weights_
bias_
n_iter_
classes_
fit.
fitted_
fit has been called.
Notes
Complexity. O(n \cdot c) per iteration.
When to use. Use vector scaling when different classes are miscalibrated in different directions — typically under class imbalance. On small calibration sets it overfits where a single temperature would not, so compare the two with expected_calibration_error on a third split.
References
>>> import numpy as np
>>> from tuiml.uncertainty import VectorScaler
>>> rng = np.random.default_rng(0)
>>> y = rng.integers(0, 3, 300)
>>> logits = np.eye(3)[y] * 5.0 + rng.normal(0, 1.0, (300, 3))
>>> scaler = VectorScaler(max_iter=200).fit(logits, y)
>>> proba = scaler.transform(logits)
>>> bool(np.allclose(proba.sum(axis=1), 1.0))
True
Methods
fit
(self, scores: np.ndarray, y: np.ndarray) -> 'VectorScaler'
fit
(self, scores: np.ndarray, y: np.ndarray) -> 'VectorScaler'
Fit per-class scale and bias on held-out logits.
Parameters
scores
y
Returns
self
transform
(self, scores: np.ndarray) -> np.ndarray
transform
(self, scores: np.ndarray) -> np.ndarray
Apply the fitted scale and bias and return calibrated probabilities.
Parameters
scores
Returns
proba