Calibration
bensemble.calibration.scaling
TemperatureScaling
Bases: Module
Temperature Scaling for model calibration.
Divides logits by a single learnable scalar parameter T (temperature). This softens probabilities and calibrates confidence without changing classification accuracy (argmax remains identical).
Initializes the TemperatureScaling module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
init_temp | float | Initial value for the temperature scalar. Defaults to 1.5. | 1.5 |
Source code in bensemble/calibration/scaling.py
fit
Finds the optimal temperature T using a validation set.
Optimizes the negative log-likelihood via L-BFGS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Unscaled logits from a hold-out validation set of shape (N, num_classes). | required |
labels | Tensor | Ground truth class indices of shape (N,). | required |
max_iter | int | Maximum number of L-BFGS iterations. Defaults to 50. | 50 |
Returns:
| Name | Type | Description |
|---|---|---|
TemperatureScaling | TemperatureScaling | The fitted instance 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_size, num_classes). | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Scaled logits of shape (batch_size, num_classes). |
Source code in bensemble/calibration/scaling.py
VectorScaling
Bases: Module
Vector Scaling for multi-class calibration (extension of Platt Scaling).
Applies a per-class affine transformation to uncalibrated logits: calibrated_logits = logits * a + b
Initializes the VectorScaling module.
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 optimal scaling vectors 'a' and 'b' using a validation set.
Optimizes the negative log-likelihood via L-BFGS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Unscaled logits from a hold-out validation set of shape (N, num_classes). | required |
labels | Tensor | Ground truth class indices of shape (N,). | required |
max_iter | int | Maximum number of L-BFGS iterations. Defaults to 50. | 50 |
Returns:
| Name | Type | Description |
|---|---|---|
VectorScaling | VectorScaling | The fitted instance itself. |
Source code in bensemble/calibration/scaling.py
forward
Applies learned affine transformation to the input logits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits | Tensor | Raw uncalibrated logits of shape (batch_size, num_classes). | required |
Returns:
| Type | Description |
|---|---|
Tensor | torch.Tensor: Calibrated logits of shape (batch_size, num_classes). |