Wrappers

This module containing wrappers to implement some ordinal methodologies.

class dlordinal.wrappers.CORNClassifierWrapper(classifier, threshold=0.5)[source]

A wrapper that converts the output of a standard multi-output binary classifier into rank-consistent ordinal class predictions using the CORN (Classification and Ordinal Regression) transformation logic.

This wrapper handles the final prediction steps: 1. Applies the Sigmoid and Cumulative Product to the raw logits (N, J-1). 2. Determines the final ordinal class label (0 to J-1) based on a probability threshold (default 0.5).

Parameters:
  • classifier (object) – The base classifier instance (e.g., a PyTorch neural network module) that implements the predict and predict_proba methods. Its predict_proba must return raw logits of shape (N, J-1).

  • threshold (float, optional) – The probability threshold (between 0 and 1) used to decide if the cumulative probability P(y >= k) corresponds to the next class level. The default is 0.5, based on standard binary classification practice, but can be tuned.

Raises:

ValueError – If the wrapped classifier does not implement both ‘predict’ and ‘predict_proba’ methods.

predict(X)[source]

Predicts the final ordinal class labels for the input data X.

Parameters:

X (array-like or torch.Tensor) – The input data to make predictions on.

Returns:

An array of integer class labels (0 to J-1) of shape (N,).

Return type:

numpy.ndarray

predict_proba(X)[source]

Applies the CORN transformation logic to the raw logits to produce one-hot encoded ordinal class probabilities.

The ordinal class is determined by counting the number of cumulative probabilities P(y >= k) that exceed the set threshold.

Parameters:

X (array-like or torch.Tensor) – The input data.

Returns:

An array of one-hot encoded probabilities of shape (N, J), where J is the total number of classes.

Return type:

numpy.ndarray

class dlordinal.wrappers.OBDECOCModel(num_classes: int, base_classifier: Module, base_n_outputs: int)[source]

Ordinal Binary Decomposition (OBD) wrapper model from Barbero-Gómez et al.[1]. It transforms the output of the provided base classifier into num_classes - 1 outputs bounded between 0 and 1, representing the class threshold probabilities \(P(y > q)\).

Parameters:
  • num_classes (int) – Number of classes.

  • base_classifier (nn.Module) – Base classifier that will be wrapped.

  • base_n_outputs – Number of outputs of the base classifier. The models implemented in torchvision have 1000 as the default

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

x (torch.Tensor) – Input to the model

Returns:

threshold_probas – Predicted threshold probabilities

Return type:

torch.Tensor

predict_from_inputs(x)[source]
Parameters:

x (torch.Tensor) – Input to the model

Returns:

  • An object with the following attributes.

  • scores (Tensor) – The negative distance to each class ideal vector, to use as class scores.

  • probas (Tensor) – The predicted probability of belonging to each class \(P(y = q)\).

  • labels (Tensor) – The predicted integer label according to the ECOC assignment scheme.