Output Layers

class dlordinal.output_layers.CLM(num_classes: int, link_function: Literal['logit', 'probit', 'cloglog'], min_distance: int = 0.0, **kwargs)[source]

Implementation of the cumulative link models from Vargas et al.[1] as a torch layer. Different link functions can be used, including logit, probit and cloglog.

Parameters:
  • num_classes (int) – The number of classes.

  • link_function (str) – The link function to use. Can be 'logit', 'probit' or 'cloglog'.

  • min_distance (float, default=0.0) – The minimum distance between thresholds

num_classes

The number of classes.

Type:

int

The link function to use. Can be 'logit', 'probit' or 'cloglog'.

Type:

str

min_distance

The minimum distance between thresholds

Type:

float

dist_

The normal (0,1) distribution used to compute the probit link function.

Type:

torch.distributions.Normal

thresholds_b_

The torch parameter for the first threshold.

Type:

torch.nn.Parameter

thresholds_a_

The torch parameter for the alphas of the thresholds.

Type:

torch.nn.Parameter

Example

>>> import torch
>>> from dlordinal.output_layers import CLM
>>> inp = torch.randn(10, 5)
>>> fc = torch.nn.Linear(5, 1)
>>> clm = CLM(5, "logit")
>>> output = clm(fc(inp))
>>> print(output)
tensor([[0.7944, 0.1187, 0.0531, 0.0211, 0.0127],
        [0.4017, 0.2443, 0.1862, 0.0987, 0.0690],
        [0.4619, 0.2381, 0.1638, 0.0814, 0.0548],
        [0.4636, 0.2378, 0.1632, 0.0809, 0.0545],
        [0.4330, 0.2419, 0.1746, 0.0893, 0.0612],
        [0.5006, 0.2309, 0.1495, 0.0716, 0.0473],
        [0.6011, 0.2027, 0.1138, 0.0504, 0.0320],
        [0.5995, 0.2032, 0.1144, 0.0507, 0.0322],
        [0.4014, 0.2443, 0.1863, 0.0988, 0.0691],
        [0.6922, 0.1672, 0.0838, 0.0351, 0.0217]], grad_fn=<CopySlices>)
forward(x)[source]
Parameters:

x (torch.Tensor) – The input tensor.

Returns:

output – The output tensor.

Return type:

Tensor

class dlordinal.output_layers.COPOC(phi: ~typing.Callable[[~torch.Tensor], ~torch.Tensor] = <function COPOC.<lambda>>, psi: ~typing.Callable[[~torch.Tensor], ~torch.Tensor] = <function COPOC.<lambda>>)[source]

Implements the Conformal Predictions for OC (COPOC) output layer(s) from Dey et al.[2], which enforce unimodality in the output probabilities in a non-parametric way.

Parameters:
  • phi (Callable[[Tensor], Tensor]) – Non-negative transformation function. Default is absolute value function \phi(x)=|x|.

  • psi (Callable[[Tensor], Tensor]) – Strictly monotonic decreasing bijective function. Default is negative absolute value function \psi(x)=-|x|.

Example

>>> import torch
>>> from dlordinal.output_layers import COPOC
>>> inp = torch.randn(10, 5)
>>> fc = torch.nn.Linear(5, 5)
>>> copoc = COPOC()
>>> output = torch.nn.functional.softmax(copoc(fc(inp)),dim=1)
>>> print(output)
tensor([[0.1898, 0.1901, 0.2568, 0.2196, 0.1436],
        [0.4538, 0.3191, 0.1412, 0.0529, 0.0330],
        [0.3371, 0.2554, 0.2151, 0.1047, 0.0876],
        [0.1859, 0.2073, 0.2658, 0.1889, 0.1520],
        [0.3306, 0.2195, 0.1982, 0.1303, 0.1214],
        [0.2132, 0.3768, 0.1590, 0.1278, 0.1232],
        [0.1531, 0.1544, 0.2094, 0.2451, 0.2381],
        [0.4986, 0.2240, 0.1689, 0.0590, 0.0495],
        [0.5838, 0.2201, 0.1289, 0.0507, 0.0166],
        [0.1639, 0.1969, 0.2100, 0.2347, 0.1946]], grad_fn=<SoftmaxBackward0>)
forward(x: Tensor) Tensor[source]
Parameters:

x (torch.Tensor) – Input tensor of shape (batch_size, num_classes).

Returns:

probs – Logits of the unimodal output layer (batch_size, num_classes).

Return type:

torch.Tensor

class dlordinal.output_layers.ResNetOrdinalFullyConnected(input_size: int, num_classes: int)[source]

ResNetOrdinalFullyConnected implements the ordinal fully connected layer

Parameters:
  • input_size (int) – Input size

  • num_classes (int) – Number of classes

forward(x: Tensor) Tensor[source]
Parameters:

x (torch.Tensor) – Input tensor

class dlordinal.output_layers.StickBreakingLayer(input_shape: int, num_classes: int)[source]

Base class to implement the stick breaking layer from Liu et al.[3].

Parameters:
  • input_shape (int) – Input shape, which refers to the number of neurons in the last fully connected layer

  • num_classes (int) – Number of classes

forward(x) Tensor[source]
Parameters:

x (torch.Tensor) – Input tensor

Returns:

logits – Logits of the stick breaking layer

Return type:

torch.Tensor

get_stick_logits(x: Tensor)[source]
Parameters:

x (torch.Tensor) – Input tensor

Returns:

logits – Logits of the stick breaking layer

Return type:

torch.Tensor

class dlordinal.output_layers.VGGOrdinalFullyConnected(input_size: int, num_classes: int, activation_function: Callable[[], Module])[source]

VGGOrdinalFullyConnected implements the ordinal fully connected layer

Parameters:
  • input_size (int) – Input size

  • num_classes (int) – Number of classes

  • activation_function (Callable[[], nn.Module]) – Activation function

forward(x: Tensor) Tensor[source]
Parameters:

x (torch.Tensor) – Input tensor