spacecutter-torch
spacecutter-torch is a library for implementing ordinal regression models in PyTorch. The library consists of models and loss functions.
Installation
pip install spacecutter-torch
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 torch
from torch import nn
from spacecutter.models import OrdinalLogisticHead
X = torch.tensor([[0.5, 0.1, -0.1],
[1.0, 0.2, 0.6],
[-2.0, 0.4, 0.8]]).float()
y = torch.tensor([0, 1, 2]).reshape(-1, 1).long()
num_features = X.shape[1]
num_classes = len(torch.unique(y))
model = nn.Sequential(
nn.Linear(num_features, num_features),
nn.ReLU(),
nn.Linear(num_features, 1),
OrdinalLogisticHead(num_classes),
)
y_pred = model(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
The following shows how to train the model from the previous section using cumulative link loss:
import torch
from spacecutter.callbacks import AscensionCallback
from spacecutter.losses import CumulativeLinkLoss
def train(model, optimizer, X, y, num_epochs = 10) -> list:
"""
you can bring your own training loop if you want, but we provide a very simple one here.
"""
model.train()
on_batch_end_callbacks = [AscensionCallback()]
loss_fn = CumulativeLinkLoss()
losses = []
for epoch in range(num_epochs):
optimizer.zero_grad()
y_pred = model(X)
loss = loss_fn(y_pred, y)
loss.backward()
optimizer.step()
losses.append(loss.item())
with torch.no_grad():
for callback in on_batch_end_callbacks:
model.apply(callback)
return losses
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
losses = train(model, optimizer, 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-torch 1.0.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 |
|---|---|---|---|---|
| spacecutter_torch-1.0.0.tar.gz | 5.3 kB | Without provenance | Details for spacecutter_torch-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spacecutter_torch-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for spacecutter_torch-1.0.0-py3-none-any.whl |
Total release size: 11.9 kB
Release files / spacecutter_torch-1.0.0.tar.gz
| Download URL | spacecutter_torch-1.0.0.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b564914c966498a1df9f8f5d852cb284cda3a325b0b0f605f31883488a0eee42
|
|
BLAKE2b-256 checksum How to use checksums |
609b134d3654fec1611c704fd3438330eb4bb1d46929accaa6731e573ef33958
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Release files / spacecutter_torch-1.0.0-py3-none-any.whl
| Download URL | spacecutter_torch-1.0.0-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fbf9534fd1ae37e67600feca4f2c4aec16e68df73cc32ddfdcce46bfc4ed9cc0
|
|
BLAKE2b-256 checksum How to use checksums |
5f2291552cd44bc4453d494cb3c37f7682a2a8aaea108dc884394bfc05d3cc93
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.17
|
Security for spacecutter-torch 1.0.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.