Skip to content

bnn #

Basic building blocks for Bayesian neural networks.

Classes:

Name Description
TemperatureScaler

Temperature scaling.

TemperatureScaler #

TemperatureScaler(
    loss_fn: Module = CrossEntropyLoss(),
    lr: float = 0.1,
    max_iter: int = 100,
    tolerance_grad: float = 1e-07,
    tolerance_change: float = 1e-09,
    history_size: int = 100,
)

Bases: Module

Temperature scaling.

Tunes the temperature parameter of the model in the last layer to minimize the negative log likelihood of the validation set.

Based on On Calibration of Modern Neural Networks.

Parameters:

Name Type Description Default
loss_fn Module

The loss function to be used for calibration.

CrossEntropyLoss()
lr float

Learning rate for the optimizer.

0.1
max_iter int

Maximum number of iterations per optimization step.

100
tolerance_grad float

Tolerance for the gradient.

1e-07
tolerance_change float

Tolerance for the change in the loss function / parameters.

1e-09
history_size int

Size of the history for the LBFGS optimizer.

100

Methods:

Name Description
optimize

Optimizes the temperature of the model.

Attributes:

Name Type Description
history_size
loss_fn
lr
max_iter
tolerance_change
tolerance_grad

history_size #

history_size = history_size

loss_fn #

loss_fn = loss_fn

lr #

lr = lr

max_iter #

max_iter = max_iter

tolerance_change #

tolerance_change = tolerance_change

tolerance_grad #

tolerance_grad = tolerance_grad

optimize #

optimize(model: BNNModule, dataloader: DataLoader) -> None

Optimizes the temperature of the model.

Parameters:

Name Type Description Default
model BNNModule

The model to be calibrated, assumed to return logits.

required
dataloader DataLoader

The dataloader for the dataset to calibrate on (typically the validation set).

required