Skip to main content

fairret - a fairness library in PyTorch

Licence PyPI - Version Static Badge Static Badge

The goal of fairret is to serve as an open-source library for measuring and mitigating statistical fairness in PyTorch models.

The library is designed to be

  1. flexible in how fairness is defined and pursued.
  2. easy to integrate into existing PyTorch pipelines.
  3. 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!

Quickstart

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 $N$ elements, then sens should be a tensor of floats with shape $(N, d_s)$, with $d_s$ the number of sensitive features. Like any categorical feature, it is expected that categorical sensitive features are one-hot encoded.

A notebook with a full example pipeline is provided here: simple_pipeline.ipynb.

We also host documentation.

Installation

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 .

Warning: AI fairness != fairness

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.

Future plans

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 maarten.buyl@ugent.be.

Citation

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}
}

Metadata

Release files for fairret 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fairret 0.1.3
File Size Uploaded
fairret-0.1.3.tar.gz 136.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fairret 0.1.3
File Interpreter ABI Platform
fairret-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 157.2 kB

Release files / fairret-0.1.3.tar.gz

Download URL fairret-0.1.3.tar.gz
Size 136.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e8684f362fd3da65080172ef43e80e50592d02d6e5855834707c9974056e08c3
BLAKE2b-256 checksum
How to use checksums
bd74d4941064187008d409cf48867728d526f77a083ea39fd8cb4d4d59c4deac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / fairret-0.1.3-py3-none-any.whl

Download URL fairret-0.1.3-py3-none-any.whl
Size 20.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de47b7b50c3dbfb241b4b6e686d49543ab6c62ac3ea98a4b0c5cca765ca2b686
BLAKE2b-256 checksum
How to use checksums
3b4da4ccaeab9e3d1b2d035981f45a891d2eda0ec18dddba5d53bb3afb2f1166
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page