Skip to main content

A fair loss function

Project description

A fair PyTorch loss function

REUSE status

The goal of this loss function is to take fairness into account during the training of a PyTorch model. It works by adding a fairness measure to a regular loss value, following this equation:

Installation

pip install fair-loss

Example

import torch
import torch.nn.functional as F
import numpy as np
from fair_loss import FairLoss


def accuracy(input, targs):
    return torch.true_divide((input == targs).sum(), input.shape[0])


model = torch.nn.Sequential(torch.nn.Linear(5, 1), torch.nn.ReLU())
data = np.random.randint(5, size=(100, 5)).astype("float")
data = torch.tensor(data, requires_grad=True, dtype=torch.float)
y_true = np.random.randint(5, size=(100, 1)).astype("float")
y_true = torch.tensor(y_true, requires_grad=True)
y_pred = model(data)
# Let's say the sensitive attribute is in the second dimension
dim = 1
loss = F.mse_loss(y_pred, y_true)
loss = FairLoss(data[:, dim], loss, y_pred, y_true, accuracy)
loss.backward()

Documentation

See the documentation.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fair_loss-0.2-py2.py3-none-any.whl (15.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file fair_loss-0.2-py2.py3-none-any.whl.

File metadata

  • Download URL: fair_loss-0.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.3

File hashes

Hashes for fair_loss-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 2985fe29cd590b9bfbba90014ea0b459033f507429a26793884fdfac4bd09340
MD5 caf16ed38894d3e4153d354ec092e769
BLAKE2b-256 762e0b627a63158b0527850f1782f4a13bce940faf66a953b1fc3e124e63f0c7

See more details on using hashes here.

Supported by

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