inFairness is a Python package to train and audit individually fair PyTorch models
Project description
Individual Fairness and inFairness
Intuitively, an individually fair Machine Learning (ML) model treats similar inputs similarly. Formally, the leading notion of individual fairness is metric fairness (Dwork et al., 2011); it requires:
$$ d_y (h(x_1), h(x_2)) \leq L d_x(x_1, x_2) \quad \forall \quad x_1, x_2 \in X $$
Here, $h: X \rightarrow Y$ is a ML model, where $X$ and $Y$ are input and output spaces; $d_x$ and $d_y$ are metrics on the input and output spaces, and $L \geq 0$ is a Lipchitz constant. This constrained optimization equation states that the distance between the model predictions for inputs $x_1$ and $x_2$ is upper-bounded by the fair distance between the inputs $x_1$ and $x_2$. Here, the fair metric $d_x$ encodes our intuition of which samples should be treated similarly by the ML model, and in designing so, we ensure that for input samples considered similar by the fair metric $d_x$, the model outputs will be similar as well.
inFairness is a PyTorch package that supports auditing, training, and post-processing ML models for individual fairness. At its core, the library implements the key components of individual fairness pipeline: $d_x$ - distance in the input space, $d_y$ - distance in the output space, and the learning algorithms to optimize for the equation above.
For an in-depth tutorial of Individual Fairness and the inFairness package, please watch this tutorial. Also, take a look at the examples folder for illustrative use-cases and try the Fairness Playground demo. For more group fairness examples see AIF360.
Installation
inFairness can be installed using pip
:
pip install inFairness
Alternatively, if you wish to install the latest development version, you can install directly by cloning this repository:
git clone <git repo url>
cd inFairness
pip install -e .
Features
inFairness currently supports:
- Learning individually fair metrics : [Docs]
- Training of individually fair models : [Docs]
- Auditing pre-trained ML models for individual fairness : [Docs]
- Post-processing for Individual Fairness : [Docs]
- Individually fair ranking : [Docs]
Contributing
We welcome contributions from the community in any form - whether it is through the contribution of a new fair algorithm, fair metric, a new use-case, or simply reporting an issue or enhancement in the package. To contribute code to the package, please follow the following steps:
- Clone this git repository to your local system
- Setup your system by installing dependencies as:
pip3 install -r requirements.txt
andpip3 install -r build_requirements.txt
- Add your code contribution to the package. Please refer to the
inFairness
folder for an overview of the directory structure - Add appropriate unit tests in the
tests
folder - Once you are ready to commit code, check for the following:
- Coding style compliance using:
flake8 inFairness/
. This command will list all stylistic violations found in the code. Please try to fix as much as you can - Ensure all the test cases pass using:
coverage run --source inFairness -m pytest tests/
. All unit tests need to pass to be able merge code in the package.
- Coding style compliance using:
- Finally, commit your code and raise a Pull Request.
Tutorials
The examples
folder contains tutorials from different fields illustrating how to use the package.
Minimal example
First, you need to import the relevant packages
from inFairness import distances
from inFairness.fairalgo import SenSeI
The inFairness.distances
module implements various distance metrics on the input and the output spaces, and the inFairness.fairalgo
implements various individually fair learning algorithms with SenSeI
being one particular algorithm.
Thereafter, we instantiate and fit the distance metrics on the training data, and
distance_x = distances.SVDSensitiveSubspaceDistance()
distance_y = distances.EuclideanDistance()
distance_x.fit(X_train=data, n_components=50)
# Finally instantiate the fair algorithm
fairalgo = SenSeI(network, distance_x, distance_y, lossfn, rho=1.0, eps=1e-3, lr=0.01, auditor_nsteps=100, auditor_lr=0.1)
Finally, you can train the fairalgo
as you would train your standard PyTorch deep neural network:
fairalgo.train()
for epoch in range(EPOCHS):
for x, y in train_dl:
optimizer.zero_grad()
result = fairalgo(x, y)
result.loss.backward()
optimizer.step()
Authors
Mikhail Yurochkin |
Mayank Agarwal |
Aldo Pareja |
Onkar Bhardwaj |
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
Built Distribution
File details
Details for the file inFairness-0.2.3.tar.gz
.
File metadata
- Download URL: inFairness-0.2.3.tar.gz
- Upload date:
- Size: 34.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a50caed2253ce0ab42d3f2a73d39871682ce2e3617160823aa72db43c5bb158b |
|
MD5 | 05a6e38ede6aa9c3579b4485ea47582b |
|
BLAKE2b-256 | 5d6c312f52fac23c579678c4e4e2ede03083739ae5f644318baa9a4fdf0d36c2 |
File details
Details for the file inFairness-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: inFairness-0.2.3-py3-none-any.whl
- Upload date:
- Size: 45.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85a39fc0a68d5b895a6794e722cf0548ff0f4fd52751ed3661e7a6f6365f8a22 |
|
MD5 | 9c5323ba4b3cc0c3979dac24ce3bec4f |
|
BLAKE2b-256 | 282660e9a18cfbfdd0e3f4c38d4d2707664e161dd9cddceeaa1ce1b5c001f9f6 |