calpit.nn.training

Loss and prediction for Cal-PIT networks, usable in any training loop.

Functions

coverage_loss(→ torch.Tensor)

Binary cross entropy of a Cal-PIT network's output against 1{PIT <= alpha}.

_device_of(→ torch.device)

predict_pit_cdf(→ numpy.ndarray)

Predicts r(alpha; x) = P(PIT <= alpha | x) with a Cal-PIT network.

Module Contents

coverage_loss(output: torch.Tensor, target: torch.Tensor, output_type: str = 'logit', reduction: str = 'mean') → torch.Tensor[source]

Binary cross entropy of a Cal-PIT network’s output against 1{PIT <= alpha}.

Parameters:
  • output – The network’s output, shape (batch,).

  • target – The targets, 1.0 if the PIT is at most alpha and 0.0 otherwise, shape (batch,).

  • output_type – What the network returns, “logit” or “probability” (calpit.nn.output_type gives it). Probabilities are clamped to [0, 1] first, and their loss is computed in float32 outside any autocast region, where torch refuses binary_cross_entropy.

  • reduction – “mean”, “sum” or “none”, as in torch.nn.BCELoss.

Returns:

The loss, a scalar unless reduction is “none”.

_device_of(model: torch.nn.Module) → torch.device[source]
predict_pit_cdf(model: torch.nn.Module, x: numpy.typing.ArrayLike, alpha: numpy.typing.ArrayLike, batch_size: int = 2048, device: torch.device | str | None = None) → numpy.ndarray[source]

Predicts r(alpha; x) = P(PIT <= alpha | x) with a Cal-PIT network.

The network is evaluated in eval mode without gradients, and restored to the mode it was in. A network with a forward_curves(alpha, x) method (see calpit.nn.models) is called once per object with all its alpha. Logits go through a sigmoid; the results are clipped to [0, 1]. They are not rearranged (see calpit.coverage.rearrange).

Parameters:
  • model – The network, which follows the contract in calpit.nn.models.

  • x – The features, shape (n_objects, n_features).

  • alpha – The coverage levels, shape (n_alpha,) to use the same levels for every object, or (n_objects, n_alpha).

  • batch_size – The number of objects per forward pass; each pass has batch_size * n_alpha rows.

  • device – The device to evaluate on; the model is moved there. None uses the device of the model’s parameters.

Returns:

The predicted PIT CDF, shape (n_objects, n_alpha).