Guaranteed fairness constraints for PyTorch neural networks via differentiable optimization layers.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fairness_training-0.1.0.tar.gz.
File metadata
- Download URL: fairness_training-0.1.0.tar.gz
- Upload date:
- Size: 39.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e3f927b9c905df453d53226b62ddb54339974262736cb196f9563647ffeebd9f
|
|
| MD5 |
9d14bf6291aeea7bcb3e1ffe0fcd277e
|
|
| BLAKE2b-256 |
0b478b937aaed51cf051652914e9cedc565db47b9b3a4e81021106f3a5c71591
|
Provenance
The following attestation bundles were made for fairness_training-0.1.0.tar.gz:
Publisher:
publish.yml on dtroxell19/fairness_training
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fairness_training-0.1.0.tar.gz -
Subject digest:
e3f927b9c905df453d53226b62ddb54339974262736cb196f9563647ffeebd9f - Sigstore transparency entry: 1525551331
- Sigstore integration time:
-
Permalink:
dtroxell19/fairness_training@8074350d75970020ef33bbf829514d9c973e69c6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dtroxell19
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8074350d75970020ef33bbf829514d9c973e69c6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file fairness_training-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fairness_training-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b05a6de71c916d51682cafb2878c8c1c3c7060f5a622816a0b01422723401a7a
|
|
| MD5 |
26299435eeb6ff4ebafadcf00fd9116d
|
|
| BLAKE2b-256 |
5976394a261ba1372e155512e9a223fa13f40231244a0a5e7fcf2b8bc3dad93f
|
Provenance
The following attestation bundles were made for fairness_training-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on dtroxell19/fairness_training
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fairness_training-0.1.0-py3-none-any.whl -
Subject digest:
b05a6de71c916d51682cafb2878c8c1c3c7060f5a622816a0b01422723401a7a - Sigstore transparency entry: 1525551350
- Sigstore integration time:
-
Permalink:
dtroxell19/fairness_training@8074350d75970020ef33bbf829514d9c973e69c6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dtroxell19
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8074350d75970020ef33bbf829514d9c973e69c6 -
Trigger Event:
release
-
Statement type: