incerto.ood.Energy#

class incerto.ood.Energy(model, temperature=1.0)[source]#

Bases: OODDetector

Energy-based OOD detector.

Computes the free-energy score -T * logsumexp(logits / T) for each input. Lower energy is associated with in-distribution data, so the returned score (negated logsumexp) is higher for OOD inputs.

Reference:

Liu et al., “Energy-based Out-of-distribution Detection”, NeurIPS 2020.

Parameters:
  • model – A trained classifier returning logits of shape (batch, n_classes).

  • temperature (float) – Temperature T applied to logits before logsumexp. Default: 1.0.

__init__(model, temperature=1.0)[source]#

Initialize the OOD detector with a trained model.

The model is automatically: 1. Set to eval mode 2. Has gradients disabled (requires_grad=False)

Parameters:
  • model – A trained PyTorch model (nn.Module)

  • temperature (float)

Raises:

TypeError – If model is not an nn.Module.

Methods

__init__(model[, temperature])

Initialize the OOD detector with a trained model.

load(path, model, **kwargs)

Load detector state from a file.

load_state_dict(state)

Load temperature parameter.

predict(x, threshold)

Predict whether inputs are OOD using a threshold.

save(path)

Save detector state to a file (excluding the model).

score(x)

Compute energy-based OOD scores.

state_dict()

Save temperature parameter.

score(x)[source]#

Compute energy-based OOD scores.

Parameters:

x (Tensor) – Input batch passed to model.

Return type:

Tensor

Returns:

Tensor of shape (batch,) of energy scores; higher means more OOD.

state_dict()[source]#

Save temperature parameter.

Return type:

dict

load_state_dict(state)[source]#

Load temperature parameter.

Parameters:

state (dict)

Return type:

None