calpit.nn.models

Networks for Cal-PIT and the contract every Cal-PIT network follows.

A Cal-PIT network models r(alpha; x) = P(PIT <= alpha | x). Its forward takes the coverage levels and the features as two tensors and returns one value per row:

forward(alpha, x) -> output

alpha: shape (batch,), the coverage levels in [0, 1]. x: shape (batch, n_features), the features. output: shape (batch,), the logit of r, or r itself.

The network says which it returns in its output attribute, “logit” (the default when the attribute is absent) or “probability”. A logit is trained with BCEWithLogitsLoss and can be any real number; a probability must lie in [0, 1]. A network can also define forward_curves(alpha, x), with alpha of shape (batch, n_alpha) and output of shape (batch, n_alpha), to predict many alpha per object at once; calpit.nn.predict_pit_cdf then uses it. CalPIT.transform needs r to be non-decreasing in alpha. Networks that are monotone by construction, like IsplineNN and MonotonicNN, give that exactly; for any other network CalPIT rearranges the predictions (calpit.coverage.rearrange).

Attributes

OUTPUT_TYPES

Classes

ConcatAlpha

Adapts a network that takes one input tensor to the Cal-PIT contract.

MLP

Multi-layer perceptron of [alpha, x] with PReLU activations.

MonotonicNN

Unconstrained monotonic neural network, monotone in alpha by construction.

Functions

output_type(→ str)

Returns what a Cal-PIT network outputs, "logit" or "probability".

_init_weights(→ None)

Module Contents

OUTPUT_TYPES = ('logit', 'probability')[source]
output_type(model: torch.nn.Module) → str[source]

Returns what a Cal-PIT network outputs, “logit” or “probability”.

Parameters:

model – The network. Its output attribute, if any, must be one of OUTPUT_TYPES.

Returns:

The output type, “logit” when the network has no output attribute.

_init_weights(module: torch.nn.Module) → None[source]
class ConcatAlpha(network: torch.nn.Module, output: str = 'logit')[source]

Bases: torch.nn.Module

Adapts a network that takes one input tensor to the Cal-PIT contract.

The wrapped network gets alpha and the features joined into one tensor, shape (batch, n_features + 1), with alpha in column 0.

Parameters:
  • network – The network to wrap. Its output must have batch elements, such as shape (batch,) or (batch, 1).

  • output – What the network returns, “logit” or “probability”.

Variables:
  • network – The wrapped network.

  • output – What the network returns.

network[source]
output = 'logit'[source]
forward(alpha: torch.Tensor, x: torch.Tensor) → torch.Tensor[source]

Evaluates the network on [alpha, x].

Parameters:
  • alpha – The coverage levels, shape (batch,).

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

Returns:

The network’s output, shape (batch,).

class MLP(n_features: int, hidden_layers: list[int], output: str = 'logit')[source]

Bases: torch.nn.Module

Multi-layer perceptron of [alpha, x] with PReLU activations.

The MLP is not monotone in alpha, so CalPIT rearranges its predictions.

Parameters:
  • n_features – The number of features, not counting alpha.

  • hidden_layers – The widths of the hidden layers.

  • output – “logit” for a linear output layer, or “probability” to end with a sigmoid.

Variables:
  • layers – The layers, from [alpha, x] to the output.

  • output – What the network returns.

output = 'logit'[source]
layers[source]
forward(alpha: torch.Tensor, x: torch.Tensor) → torch.Tensor[source]

Evaluates the network.

Parameters:
  • alpha – The coverage levels, shape (batch,).

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

Returns:

The logit or probability that the PIT is at most alpha, shape (batch,).

class MonotonicNN(n_features: int, hidden_layers: list[int], nb_steps: int = 50, output: str = 'logit')[source]

Bases: torch.nn.Module

Unconstrained monotonic neural network, monotone in alpha by construction.

The output is offset(x) + scale(x) * integral from 0 to alpha of a positive function of (t, x), learned with the UMNN of Wehenkel & Louppe (2019), https://arxiv.org/abs/1908.05164.

Parameters:
  • n_features – The number of features, not counting alpha.

  • hidden_layers – The widths of the hidden layers of the integrand and of the network that sets the offset and scale.

  • nb_steps – The number of integration steps.

  • output – “logit” for the integral itself, or “probability” to pass it through a sigmoid.

Variables:
  • network – The UMNN, which takes [alpha, x].

  • output – What the network returns.

output = 'logit'[source]
network[source]
forward(alpha: torch.Tensor, x: torch.Tensor) → torch.Tensor[source]

Evaluates the network.

Parameters:
  • alpha – The coverage levels, shape (batch,).

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

Returns:

The logit or probability that the PIT is at most alpha, shape (batch,).