Calibration
bensemble.calibration.scaling
TemperatureScaling
Bases: Module
Temperature Scaling for model calibration.
Divides the logits by a learnable scalar parameter T (temperature). This softens the probabilities and calibrates the model's confidence without changing its accuracy (the argmax remains the same).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init_temp | float | Initial value for temperature. Modern deep networks are typically overconfident, so starting with T > 1.0 is recommended. Defaults to 1.5. | 1.5 |
Source code in bensemble/calibration/scaling.py
fit
Finds the optimal temperature T using a validation set.
Uses the L-BFGS optimizer, which is the standard and most efficient algorithm for this 1D convex optimization problem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Unscaled logits from a hold-out validation set. Shape: [N, Num_classes]. | required |
labels | Tensor | Ground truth class indices. Shape: [N]. | required |
max_iter | int | Maximum number of L-BFGS iterations. Defaults to 50. | 50 |
Returns:
| Name | Type | Description |
|---|---|---|
TemperatureScaling | TemperatureScaling | The fitted model itself. |
Source code in bensemble/calibration/scaling.py
forward
Applies temperature scaling to the input logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Raw uncalibrated logits of shape[Batch, Num_classes]. | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Scaled logits of the same shape. |
Source code in bensemble/calibration/scaling.py
VectorScaling
Bases: Module
Vector Scaling (Multi-class extension of Platt Scaling).
Applies a per-class affine transformation to the uncalibrated logits: calibrated_logits = logits * a + b
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_classes | int | Number of classes in the classification task. | required |
Source code in bensemble/calibration/scaling.py
fit
Finds the optimal vectors 'a' and 'b' using a validation set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Unscaled logits from a hold-out validation set. Shape:[N, Num_classes]. | required |
labels | Tensor | Ground truth class indices. Shape: [N]. | required |
max_iter | int | Maximum number of L-BFGS iterations. Defaults to 50. | 50 |
Returns:
| Name | Type | Description |
|---|---|---|
PlattScaling | VectorScaling | The fitted model itself. |
Source code in bensemble/calibration/scaling.py
forward
Applies the learned affine transformation to the logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Raw uncalibrated logits of shape [Batch, Num_classes]. | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Calibrated logits of the same shape. |