VectorScaler#
- class torch_uncertainty.post_processing.VectorScaler(num_classes, model=None, init_temperature=1, lr=0.1, max_iter=200, eps=1e-08, device=None)[source]#
Vector scaling post-processing for calibrated probabilities.
Generalises temperature scaling by learning a per-class temperature vector \(\mathbf{T} \in \mathbb{R}^C_{>0}\):
\[\tilde{\mathbf{p}}(\mathbf{x}) = \mathrm{softmax}\!\left(\mathbf{z}(\mathbf{x}) \oslash \mathbf{T}\right),\]where \(\oslash\) is element-wise division. The \(C\) temperatures are fit jointly by minimising the cross-entropy on a held-out calibration set.
- Parameters:
num_classes (
int) – Number of classes \(C\).model (
Module|None) – Model to calibrate.init_temperature (
float|Tensor) – Initial value for the per-class temperature. A scalar broadcasts to all classes. Defaults to1.lr (
float) – Learning rate for the optimizer. Defaults to0.1.max_iter (
int) – Maximum number of iterations for the optimizer. Defaults to200.eps (
float) – Small value for stability. Defaults to1e-8.device (
Union[Literal['cpu','cuda'],device,None]) – Device to use for optimization. Defaults toNone.
References
Warning
For binary models, a sigmoid is applied before the prediction is transposed to the 2-class case.
- fit(dataloader, save_logits=False, progress=True)#
Fit the temperature parameters to the calibration data.
- Parameters:
dataloader (
DataLoader) – Dataloader with the logits and target of the calibration data.save_logits (
bool) – Whether to save the logits and labels in memory. Defaults toFalse.progress (
bool) – Whether to show a progress bar. Defaults toTrue.
- Return type:
None
Warning
Please provide logits and not probabilities/likelihoods within the dataloader, otherwise the Scaler might converge to negative temperatures.
- set_model(model)#
Attach a model to the post-processing module.
- Return type:
None