Skip to main content

A fairness library in PyTorch.

Project description

fairret - a fairness library in PyTorch

Licence PyPI - Version Static Badge Static Badge

Description

The goal of fairret is to serve as an open-source Python 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.

The central to the library is the paradigm of the fairness regularization term (fairrets) that quantify unfairness as differentiable PyTorch loss functions. These can then be optimized together with e.g. the binary cross-entropy error such that the classifier improves both its accuracy and fairness.

The library is still in very early development. Documentation, installation instructions, and more examples will be added in the near future.

Installation

The fairret library can be installed via PyPi:

pip install fairret

Dependencies

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 .

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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fairret-0.1.1.tar.gz (128.6 kB view details)

Uploaded Source

Built Distribution

fairret-0.1.1-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file fairret-0.1.1.tar.gz.

File metadata

  • Download URL: fairret-0.1.1.tar.gz
  • Upload date:
  • Size: 128.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for fairret-0.1.1.tar.gz
Algorithm Hash digest
SHA256 cfa94cd7f0f53c5a839edcf2f033ae1639c136d94f3be4cdf04ca9ddb6431fd5
MD5 9d6ff5d60f8700232b7acdba1ed8cae0
BLAKE2b-256 2f9d6231af49cbfe100338a26c79c99626ebeb5965325c08ca24ce60aa1fb251

See more details on using hashes here.

File details

Details for the file fairret-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fairret-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for fairret-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2934b2ab4204a7dd8c137ea8f01f5c39b37821f2eb076790314a39cca3928319
MD5 95ab4c6b0e1f914d0edda792b4f8e4be
BLAKE2b-256 ddb3125402856a5e9bb8f7bd243437f85c0b586a06b8d577236a8af124156b5a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page