A fair loss function
Project description
A fair PyTorch loss function
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, accuracy
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 Distribution
fair_loss-0.4.tar.gz
(29.4 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
fair_loss-0.4-py3-none-any.whl
(16.0 kB
view details)
File details
Details for the file fair_loss-0.4.tar.gz.
File metadata
- Download URL: fair_loss-0.4.tar.gz
- Upload date:
- Size: 29.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eeb7a3b049bc4f753e5807fcdf13b57a438be0deeb5da984003d754333b0407a
|
|
| MD5 |
104980ecb2978e69f8badf409e55a226
|
|
| BLAKE2b-256 |
bcc799f55acd2d8cf74f85bc235b8c9dd149cab6e9c01c5cc6c43e9f8215decc
|
File details
Details for the file fair_loss-0.4-py3-none-any.whl.
File metadata
- Download URL: fair_loss-0.4-py3-none-any.whl
- Upload date:
- Size: 16.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
770745953619a55077eb93eb7f618efa9d346397949b4e69a61c165afda1b0a5
|
|
| MD5 |
d68b75986b4d13078d36bbee51be2f4c
|
|
| BLAKE2b-256 |
1574be8935785465a29da2a74fc210fc28961937937f31c84c83e08eef52c748
|