DeepOrdinal
Ordinal regression for PyTorch, TensorFlow/Keras, and JAX/Flax: an OrdinalOutput layer that turns features into K ordered class probabilities, and the loss functions of Rennie & Srebro (2005) to train it. The three backends share one API.
Install
pip install "deepordinal[torch]" # PyTorch
pip install "deepordinal[tf]" # TensorFlow/Keras
pip install "deepordinal[jax]" # JAX/Flax NNX, Python 3.11+
Quick start
Every backend follows the same pattern. Train on the raw logit head.logit(h) and the thresholds head.interior_thresholds, and read probabilities from head(h).
PyTorch
import torch
import torch.nn as nn
from deepordinal.torch import OrdinalOutput, ordinal_loss
features = nn.Sequential(nn.Linear(8, 16), nn.ReLU())
head = OrdinalOutput(input_dim=16, output_dim=4)
optimizer = torch.optim.Adam([*features.parameters(), *head.parameters()])
loss = ordinal_loss(head.logit(features(x)), y, head.interior_thresholds)
optimizer.zero_grad()
loss.backward()
optimizer.step()
probs = head(features(x)) # (batch, 4)
TensorFlow/Keras
import tensorflow as tf
from deepordinal.tf import OrdinalOutput, ordinal_loss
features = tf.keras.layers.Dense(16, activation="relu")
head = OrdinalOutput(output_dim=4)
optimizer = tf.keras.optimizers.Adam()
with tf.GradientTape() as tape:
loss = ordinal_loss(head.logit(features(x)), y, head.interior_thresholds)
params = features.trainable_variables + head.trainable_variables
optimizer.apply_gradients(zip(tape.gradient(loss, params), params))
probs = head(features(x)) # (batch, 4)
JAX/Flax NNX
import jax
import optax
from flax import nnx
from deepordinal.jax import OrdinalOutput, ordinal_loss
class Model(nnx.Module):
def __init__(self, rngs):
self.features = nnx.Sequential(nnx.Linear(8, 16, rngs=rngs), jax.nn.relu)
self.head = OrdinalOutput(input_dim=16, output_dim=4, rngs=rngs)
def __call__(self, x):
return self.head(self.features(x))
model = Model(nnx.Rngs(0))
optimizer = nnx.Optimizer(model, optax.adam(1e-3), wrt=nnx.Param)
@nnx.jit
def train_step(model, optimizer, x, y):
def loss_fn(model):
return ordinal_loss(model.head.logit(model.features(x)), y, model.head.interior_thresholds)
loss, grads = nnx.value_and_grad(loss_fn)(model)
optimizer.update(model, grads)
return loss
loss = train_step(model, optimizer, x, y)
probs = model(x) # (batch, 4)
The layer
OrdinalOutput projects its input to one logit z and compares it with K-1 ordered thresholds t:
P(y = k | x) = sigmoid(t[k] - z) - sigmoid(t[k-1] - z)
The first term is 1 for the last class, and the second is 0 for the first class.
- The thresholds are ordered by construction. The trainable
raw_thresholdshold the first threshold and the inverse softplus of each gap, so no training step can reorder them or produce a negative probability. - The projection has no bias. Shifting
zand every threshold together leaves the model unchanged, so the thresholds absorb it. - The thresholds start at
logit(k/K), so a zero logit gives every class probability1/K.
| Constructor | |
|---|---|
| PyTorch | OrdinalOutput(input_dim, output_dim) |
| TensorFlow/Keras | OrdinalOutput(output_dim) |
| JAX/Flax NNX | OrdinalOutput(input_dim, output_dim, rngs=rngs) |
| Member | Shape | |
|---|---|---|
layer(h) |
(..., K) |
class probabilities |
layer.log_probs(h) |
(..., K) |
log class probabilities, accurate at any logit |
layer.logit(h) |
(..., 1) |
raw logit, the first argument to every loss |
layer.interior_thresholds |
(K-1,) |
ordered thresholds, the third argument to ordinal_loss and cumulative_link_loss |
Losses
Every loss takes the raw logit ((batch,) or (batch, 1)), integer targets in [0, K), and returns the mean over the batch.
| Loss | What it fits | Use it when |
|---|---|---|
ordinal_loss(logits, targets, thresholds, construction="all", penalty="logistic") |
A margin penalty for each threshold on the wrong side of the logit (paper eqs 12 and 13) | You care about the predicted label. The default all-threshold logistic loss performed best in the paper. |
cumulative_link_loss(logits, targets, thresholds) |
The likelihood of the probabilities OrdinalOutput outputs |
You use the probabilities themselves, for example for calibration or expected-cost decisions. |
ordistic_loss(logits, targets, means, log_priors=None) |
The likelihood of the paper's ordistic model, with one mean per class (section 4) | You want the paper's probabilistic alternative and supply the (K,) class means yourself. The paper fixes the first mean at -1 and the last at 1. |
ordinal_loss options
construction="all"sums a penalty over every threshold, so the loss grows with the distance between the predicted and true labels (eq 13).construction="immediate"penalizes only the two thresholds that bound the target class (eq 12).
penalty |
f(m) for margin m |
Paper |
|---|---|---|
"logistic" |
log(1 + exp(-m)) |
eq 9 |
"hinge" |
max(0, 1 - m) |
eq 5 |
"smooth_hinge" |
0 if m >= 1, (1 - m)^2 / 2 if 0 < m < 1, 0.5 - m otherwise |
eq 6 |
"modified_least_squares" |
0 if m >= 1, (1 - m)^2 otherwise |
eq 7 |
cumulative_link_loss numerics
The log-probabilities come from softplus and log1mexp terms, never from a difference of sigmoids. In float32, that difference rounds to exactly 0 once |logit| passes about 17. layer.log_probs and layer(h) use the same computation.
For interior classes, the loss equals the immediate-threshold logistic loss plus -log(1 - exp(-gap)), where gap is the width of the target class's interval. For the first and last classes, the two losses are equal.
Examples
Each notebook trains the same model on the same synthetic data:
examples/example_torch.ipynb: PyTorchexamples/example_tf.ipynb: TensorFlow/Kerasexamples/example_jax.ipynb: JAX/Flax NNX (also needsoptax)
Development
pip install -e ".[torch,tf,jax]"
pytest
Each test in tests/test_deepordinal.py runs once per installed backend and checks the result against an independent float64 NumPy reference in tests/reference.py.
Citation
Rennie, J. D. M. & Srebro, N. (2005). Loss Functions for Preference Levels: Regression with Discrete Ordered Labels. Proceedings of the IJCAI Multidisciplinary Workshop on Advances in Preference Handling.
Metadata
Release files for deepordinal 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| deepordinal-0.3.0.tar.gz | 14.2 kB | Without provenance | Details for deepordinal-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| deepordinal-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for deepordinal-0.3.0-py3-none-any.whl |
Total release size: 27.0 kB
Release files / deepordinal-0.3.0.tar.gz
| Download URL | deepordinal-0.3.0.tar.gz |
|---|---|
| Size | 14.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8586fa1f237b1ea8490e00039bc5fb3920925702657b609c28676332f41e4fc8
|
|
BLAKE2b-256 checksum How to use checksums |
24c5c8766c0f6a94eaa16635301a24b99de908acbfe2a40dd0603dd64794094a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / deepordinal-0.3.0-py3-none-any.whl
| Download URL | deepordinal-0.3.0-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c8007fbab82caa8743ec5f4379b208205834fdcd42dcb27e3b663b1dfca237b
|
|
BLAKE2b-256 checksum How to use checksums |
63b80db1b5678ddb0011b9e2f770aa9213460d4a26040ae8746a9283a5d519c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for deepordinal 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.