Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


All Contributors


FairTorch won 1st prize at the Global PyTorch Summer Hackathon 2020, Responsible AI section.

PyTorch implementation of parity loss as constraints function to realize the fairness of machine learning.

Demographic parity loss and equalied odds loss are available.

This project is a part of PyTorch Summer Hackathon 2020. visit our project page.


dp_loss = DemographicParityLoss(sensitive_classes=[0, 1], alpha=100)
criterion = nn.BCEWithLogitsLoss()


logit = model(x_train)
loss = criterion(logit.view(-1), y_train)
loss = loss + dp_loss(x_train, logit, sensitive_features)


pip version

pip install fairtorch

newest version

git clone
cd fairtorch
pip install .


In recent years, machine learning-based algorithms and softwares have rapidly spread in society. However, cases have been found where these algorithms unintentionally suggest discriminatory decisions[1]. For example, allocation harms can occur when AI systems extend or withhold opportunities, resources, or information. Some of the key applications are in hiring, school admissions, and lending[2]. Since Pytorch didn't have a library to achieve fairness yet, we decided to create one.

What it does

Fairtorch provides tools to mitigate inequities in classification and regression. Classification is only available in binary classification. A unique feature of this tool is that you can add a fairness constraint to your model by simply adding a few lines of code.

Challenges we ran into

In the beginning, we attempted to develop FairTorch based on the Fairlearn[3]’s reduction algorithm. However, it was implemented based on scikit-learn and was not a suitable algorithm for deep learning. It requires ensemble training of the model, which would be too computationally expensive to be used for deep learning. To solve that problem, we implemented a constrained optimization without ensemble learning to fit the existing Fairlearn algorithm for deep learning.

How we built it

We employ a method called group fairness, which is formulated by a constraint on the predictor's behavior called a parity constraint, where is the feature vector used for prediction, is a single sensitive feature (such as age or race), and is the true label. A parity constraint is expressed in terms of an expected value about the distribution on .

In order to achieve the above, constrained optimization is adopted. We implemented loss as a constraint. The loss corresponds to parity constraints.

Demographic Parity and Equalized Odds are applied to the classification algorithm.

We consider a binary classification setting where the training examples consist of triples , where X is a feature value, $A$ is a protected attribute, and is a label.A classifier that predicts from is .

The demographic parity is shown below.

Next, the equalized odds are shown below.

We consider learning a classifier by pytorch that satisfies these fairness conditions. The is a parameter. As an inequality-constrained optimization problem, we convert (1) and (2) to inequalities in order to train the classifier.

Thus, the study of the classifier is as follows. s error subject to To apply this problem to pytorch's gradient method-based parameter optimization, we make the inequality constraint a constraint term R.

Accomplishments that we're proud of

We confirmed by experiment that inequality is reduced just adding 2 lines of code.

What We learned

What we learn is how to create criteria of fairness, the mathematical formulations to achieve it.

What's next for FairTorch

As the current optimization algorithm is not yet refined in FairTorch, we plan to implement a more efficient constrained optimization algorithm. Other definitions of fairness have also been proposed besides demographic parity and equalized odds. In the future, we intend to implement other kinds of fairness.


  1. the keynote by K. Crawford at NeurIPS 2017
  2. A Reductions Approach to Fair Classification (Alekh Agarwal et al.,2018)
  3. Fairlearn: A toolkit for assessing and improving fairness in AI (Bird et al., 2020)


poetry install




Contributors ✨

Thanks goes to these wonderful people (emoji key):

Masashi Sode

πŸš‡ ⚠️ πŸ’» 🚧 βœ… πŸ‘€


πŸ’» ⚠️ 🚧

yoko yabe

πŸ“† πŸ€”

This project follows the all-contributors specification. Contributions of any kind welcome!


PyTorch implementation of parity loss as constraints function to realize the fairness of machine learning.







No packages published