Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
saist1993 authored Oct 2, 2023
1 parent 7989db8 commit 58074f5
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,18 @@ epsilon (float, optional): The slack which is allowed for the final fairness lev
fairness_rate (float, optional): Parameter which intertwines current fairness weights with sum of previous fairness rates.
```

Below is a small example snippet. For a fully worked out example is available [here](https://github.com/saist1993/fairgrad/blob/main/examples/simple_classification_dataset.py)
```python
# Note this is short snippet. One still needs to models and iterators.
# Full worked out example is available [here](https://github.com/saist1993/fairgrad/blob/main/examples/simple_classification_dataset.py)

import torch
from fairgrad.torch import CrossEntropyLoss

# define cross entropy loss
criterion = CrossEntropyLoss(fairness_related_meta_data=fairness_related_meta_data)

# Train loop

for inputs, labels, protected_attributes in train_iterator:
model.train()
optimizer.zero_grad()
output = model(inputs)
loss = criterion(output, labels, protected_attributes, mode='train')
loss.backward()
optimizer.step()
input = torch.randn(10, 5, requires_grad=True)
target = torch.empty(10, dtype=torch.long).random_(2)
s = torch.empty(10, dtype=torch.long).random_(2) # protected attribute
loss = CrossEntropyLoss(y_train = target, s_train = s, fairness_measure = 'equal_odds')
output = loss(input, target, s, mode='train')
output.backward()
```


We highly recommend to **standardize features** by removing the mean and scaling to unit variance.
This can be done using standard scalar module in sklearn.

Expand Down

0 comments on commit 58074f5

Please sign in to comment.