fairness_training
Guaranteed fairness constraints for PyTorch neural networks
Why fairness_training?
Most fairness methods either encourage fairness (soft penalties) or correct predictions after training (post-hoc). Both leave the door open for violations.
fairness_training uses a differentiable convex optimization layer appended to any neural network. This layer projects predictions onto the set satisfying your fairness constraints — making violations mathematically impossible on every batch.
| Approach | Guarantee | Differentiable | Works with any architecture |
|---|---|---|---|
| Penalty / regularization | No | Yes | Yes |
| Post-hoc calibration | No | No | Yes |
| fairness_training | Yes | Yes | Yes |
Features
- Hard constraints — fairness gaps are bounded by your chosen ε on every training batch
- Drop-in wrapper —
FairModel.wrap(your_model, ...)works with anynn.Module - Online inference — primal-dual algorithm provides aggregate guarantees for streaming / small-batch deployment
- Three built-in metrics — demographic parity, mean residual fairness, equalized odds
- Extensible — subclass
FairnessMetricto define custom affine fairness constraints
Installation
pip install fairness_training[train,verify] # training + fairness layer (recommended)
pip install fairness_training[train,verify,viz] # + matplotlib visualization utilities
pip install fairness_training[full] # everything
Quick Example
import torch.nn as nn
import torch.optim as optim
from fairness_training import FairModel, FairTrainer, create_stratified_dataloaders
# Any standard PyTorch backbone
backbone = nn.Sequential(
nn.Linear(20, 64), nn.ReLU(),
nn.Linear(64, 32), nn.ReLU(),
nn.Linear(32, 1), nn.Sigmoid(),
)
# Wrap with fairness constraints — bounds inferred automatically
model = FairModel.wrap(
backbone,
protected_attr_idx=0, # column index of protected attribute in X
fairness_tolerance=0.05, # ε: max allowed group mean prediction gap
fairness_metric='mean_pred',
)
# Stratified batching keeps group ratios constant → per-batch constraints = aggregate fairness
train_loader, val_loader, test_loader = create_stratified_dataloaders(
X_train, y_train, X_val, y_val, X_test, y_test,
protected_attr_idx=0, batch_size_train=256,
)
trainer = FairTrainer(model, nn.BCELoss(), optim.Adam(model.parameters()))
history = trainer.fit(train_loader, val_loader, epochs=50)
metrics = trainer.evaluate(test_loader)
print(f"Test loss: {metrics['test_loss']:.4f}")
print(f"Fairness gap: {metrics['weighted_avg_fairness_gap']:.4f} (target ≤ 0.05)")
Supported Fairness Metrics
| Metric | Constraint | String alias |
|---|---|---|
| Mean Prediction Parity | ` | E[ŷ |
| Mean Residual Fairness | ` | E[y−ŷ |
| Equalized Odds | ` | E[ŷ |
| Custom | Subclass FairnessMetric |
— |
Documentation
Full documentation — concepts, API reference, and end-to-end examples — is available at the project's GitHub Pages site.
Citation
If you use fairness_training in your research, please cite:
@inproceedings{troxell2026fairness,
title = {Differentiable Optimization Layers for Guaranteed Fairness in Deep Learning},
author = {Troxell, David and Roemer, Noah and Mont{\'u}far, Guido},
booktitle = {Proceedings of the 43rd International Conference on Machine Learning},
year = {2026},
note = {To appear}
}
This library builds on the excellent cvxpylayers package:
@inproceedings{agrawal2019differentiable,
title={Differentiable Convex Optimization Layers},
author={Agrawal, Akshay and Amos, Brandon and Barratt, Shane and Boyd, Stephen and Diamond, Steven and Kolter, Zico},
booktitle={Advances in Neural Information Processing Systems},
volume={32},
year={2019}
}
License
MIT
Metadata
Release files for fairness-training 0.1.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 |
|---|---|---|---|---|
| fairness_training-0.1.0.tar.gz | 39.5 kB | With provenance | Details for fairness_training-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fairness_training-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for fairness_training-0.1.0-py3-none-any.whl |
Total release size: 74.2 kB
Release files / fairness_training-0.1.0.tar.gz
| Download URL | fairness_training-0.1.0.tar.gz |
|---|---|
| Size | 39.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e3f927b9c905df453d53226b62ddb54339974262736cb196f9563647ffeebd9f
|
|
BLAKE2b-256 checksum How to use checksums |
0b478b937aaed51cf051652914e9cedc565db47b9b3a4e81021106f3a5c71591
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 13, 2026.
Transparency logRelease files / fairness_training-0.1.0-py3-none-any.whl
| Download URL | fairness_training-0.1.0-py3-none-any.whl |
|---|---|
| Size | 34.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b05a6de71c916d51682cafb2878c8c1c3c7060f5a622816a0b01422723401a7a
|
|
BLAKE2b-256 checksum How to use checksums |
5976394a261ba1372e155512e9a223fa13f40231244a0a5e7fcf2b8bc3dad93f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 13, 2026.
Transparency logSecurity for fairness-training 0.1.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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for fairness_training-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 13, 2026.
Transparency logA 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.