spacecutter
spacecutter is a library for implementing ordinal regression models in PyTorch. The library consists of models and loss functions. It is recommended to use skorch to wrap the models to make them compatible with scikit-learn.
Installation
pip install spacecutter
Usage
Models
Define any PyTorch model you want that generates a single, scalar prediction value. This will be our predictor model. This model can then be wrapped with spacecutter.models.OrdinalLogisticModel which will convert the output of the predictor from a single number to an array of ordinal class probabilities. The following example shows how to do this for a two layer neural network predictor for a problem with three ordinal classes.
import numpy as np
import torch
from torch import nn
from spacecutter.models import OrdinalLogisticModel
X = np.array([[0.5, 0.1, -0.1],
[1.0, 0.2, 0.6],
[-2.0, 0.4, 0.8]],
dtype=np.float32)
y = np.array([0, 1, 2]).reshape(-1, 1)
num_features = X.shape[1]
num_classes = len(np.unique(y))
predictor = nn.Sequential(
nn.Linear(num_features, num_features),
nn.ReLU(),
nn.Linear(num_features, 1)
)
model = OrdinalLogisticModel(predictor, num_classes)
y_pred = model(torch.as_tensor(X))
print(y_pred)
# tensor([[0.2325, 0.2191, 0.5485],
# [0.2324, 0.2191, 0.5485],
# [0.2607, 0.2287, 0.5106]], grad_fn=<CatBackward>)
Training
It is recommended to use skorch to train spacecutter models. The following shows how to train the model from the previous section using cumulative link loss with skorch:
from skorch import NeuralNet
from spacecutter.callbacks import AscensionCallback
from spacecutter.losses import CumulativeLinkLoss
skorch_model = NeuralNet(
module=OrdinalLogisticModel,
module__predictor=predictor,
module__num_classes=num_classes,
criterion=CumulativeLinkLoss,
train_split=None,
callbacks=[
('ascension', AscensionCallback()),
],
)
skorch_model.fit(X, y)
Note that we must add the AscensionCallback. This ensures that the ordinal cutpoints stay in ascending order. While ideally this constraint would be factored directly into the model optimization, spacecutter currently hacks an SGD-compatible solution by utilizing a post-backwards-pass callback to clip the cutpoint values.
Metadata
Release files for spacecutter 0.2.1
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 |
|---|---|---|---|---|
| spacecutter-0.2.1.tar.gz | 5.7 kB | Without provenance | Details for spacecutter-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spacecutter-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for spacecutter-0.2.1-py3-none-any.whl |
Total release size: 12.4 kB
Release files / spacecutter-0.2.1.tar.gz
| Download URL | spacecutter-0.2.1.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a2fe53acbd891aa704d163c1bc65e4ab64eb7f2a95a9d715756ceafac03fbf96
|
|
BLAKE2b-256 checksum How to use checksums |
1ac5b18e89321324b1379d20ad8a24c137ee14bd78653ef042b4d0eb4d01b5fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.8
|
Release files / spacecutter-0.2.1-py3-none-any.whl
| Download URL | spacecutter-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f4e2540f0cc44622eedba2b5f8fa520673ffb41fa9100799f12ff7bb9606ba6
|
|
BLAKE2b-256 checksum How to use checksums |
678de9d62b3b0617fd92c1c67fca112c3ac931238a4be2612229a97bc97acb22
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.8
|
Security for spacecutter 0.2.1
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.