The goal of fairret is to serve as an open-source library for measuring and pursuing statistical fairness in PyTorch models.
The library is designed to be
- flexible in how fairness is defined and pursued.
- easy to integrate into existing PyTorch pipelines.
- clear in what its tools can and cannot do.
Central to the library is the paradigm of the fairness regularization term (fairrets) that quantify unfairness as differentiable PyTorch loss functions.
These can be minimized jointly with other losses, like the binary cross-entropy error, by just adding them together!
It suffices to simply choose a statistic that should be equalized across groups and a fairret that quantifies the gap.
The model can then be trained as follows:
import torch.nn.functional as F
from fairret.statistic import PositiveRate
from fairret.loss import NormLoss
statistic = PositiveRate()
norm_fairret = NormLoss(statistic)
def train(model, optimizer, train_loader):
for feat, sens, target in train_loader:
optimizer.zero_grad()
logit = model(feat)
bce_loss = F.binary_cross_entropy_with_logits(logit, target)
fairret_loss = norm_fairret(logit, sens)
loss = bce_loss + fairret_loss
loss.backward()
optimizer.step()
No special data structure is required for the sensitive features. If the training batch contains sens
should be a tensor of floats with shape
A notebook with a full example pipeline is provided here: [Simple Pipeline.ipynb](/examples/Simple Pipeline.ipynb).
We also host documentation.
The fairret library can be installed via PyPi:
pip install fairret
A minimal list of dependencies is provided in pyproject.toml.
If the library is installed locally, the required packages can be installed via pip install .
There are many ways in which technical approaches to AI fairness, such as this library, are simplistic and limited in actually achieving fairness in real-world decision processes.
More information on these limitations can be found here or here.
The library maintains a core focus on only fairrets for now, yet we plan to add more fairness tools that align with the design principles in the future. These may involve breaking changes. At the same time, we'll keep reviewing the role of this library within the wider ecosystem of fairness toolkits.
Want to help? Please don't hesitate to open an issue, draft a pull request, or shoot an email to [email protected].
This framework will be presented as a paper at ICLR 2024. If you found this library useful in your work, please consider citing it as follows:
@inproceedings{buyl2024fairret,
title={fairret: a Framework for Differentiable Fairness Regularization Terms},
author={Buyl, Maarten and Defrance, Marybeth and De Bie, Tijl},
booktitle={International Conference on Learning Representations},
year={2024}
}