Note
Go to the end to download the full example code. or to run this example in your browser via JupyterLite
Adversarial Mitigation on a Synthetic Toy Problem#
This example reproduces the toy experiment from Zhang et al.[1]
with AdversarialFairnessClassifier.
The data is small and fully synthetic, so we know exactly how the sensitive feature leaks into the features and the label. That makes it easy to see what the adversary changes: the predictor is a logistic regression, so we can read off its weights before and after mitigation.
The data#
Each sample has a binary sensitive feature \(r\). A hidden variable \(v\) is centered on \(r\), and both the observed feature \(u\) and the variable \(w\) that decides the label are noisy copies of \(v\):
The model sees \(r\) and \(u\). The label depends on \(r\) only through \(v\), but because \(u\) is a noisy measurement of \(v\), \(r\) still helps predict \(y\), and samples with \(r = 1\) get a positive label more often.
import numpy as np
from sklearn.model_selection import train_test_split
rng = np.random.default_rng(123)
n = 100_000
r = rng.integers(0, 2, size=n)
v = rng.normal(loc=r, scale=1)
u = rng.normal(loc=v, scale=1)
w = rng.normal(loc=v, scale=1)
X = np.column_stack((r, u))
y = (w > 0).astype(int)
X_train, X_test, y_train, y_test, r_train, r_test = train_test_split(
X, y, r, test_size=0.2, random_state=42
)
To compare models we look at accuracy and selection rate for each group, and at the demographic parity difference: the gap between the two groups’ selection rates.
from sklearn.metrics import accuracy_score
from fairlearn.metrics import MetricFrame, demographic_parity_difference, selection_rate
def evaluate(y_pred):
mf = MetricFrame(
metrics={"accuracy": accuracy_score, "selection_rate": selection_rate},
y_true=y_test,
y_pred=y_pred,
sensitive_features=r_test,
)
print(mf.by_group)
dpd = demographic_parity_difference(y_test, y_pred, sensitive_features=r_test)
print(f"Overall accuracy: {mf.overall['accuracy']:.3f}")
print(f"Demographic parity difference: {dpd:.3f}")
Unmitigated model#
A plain logistic regression puts a large positive weight on \(r\), because it really is useful for predicting \(y\).
from sklearn.linear_model import LogisticRegression
baseline = LogisticRegression().fit(X_train, y_train)
print("weights (r, u):", baseline.coef_.ravel().round(2))
evaluate(baseline.predict(X_test))
weights (r, u): [0.68 0.69]
accuracy selection_rate
sensitive_feature_0
0 0.668296 0.500299
1 0.782678 0.918504
Overall accuracy: 0.725
Demographic parity difference: 0.418
Adversarial mitigation#
Now we train the same kind of model with an adversary. We build both networks ourselves in PyTorch so we can look at them afterwards. The predictor is a single linear layer followed by a sigmoid, which is a logistic regression. The adversary has the same shape: it gets the predictor’s output and tries to recover \(r\) from it.
We use constraints="demographic_parity". With the default "auto"
losses, a binary label and a binary sensitive feature both use binary
cross-entropy, so the adversary is itself a small logistic regression. If the
sensitive feature were continuous, Fairlearn would use a squared-error loss
for the adversary instead, and with predictor_loss and
adversary_loss you can pass your own.
alpha sets how hard the predictor works against the adversary. A small
weight decay keeps the weights from drifting.
Two passes over the training data are enough here.
import torch
from fairlearn.adversarial import AdversarialFairnessClassifier
def optimizer(model):
return torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=0.01)
torch.manual_seed(123)
predictor = torch.nn.Sequential(torch.nn.Linear(2, 1), torch.nn.Sigmoid())
adversary = torch.nn.Sequential(torch.nn.Linear(1, 1), torch.nn.Sigmoid())
mitigator = AdversarialFairnessClassifier(
backend="torch",
predictor_model=predictor,
adversary_model=adversary,
predictor_optimizer=optimizer,
adversary_optimizer=optimizer,
constraints="demographic_parity",
alpha=1.0,
epochs=2,
batch_size=32,
random_state=123,
)
mitigator.fit(X_train, y_train, sensitive_features=r_train)
The mitigator trains the predictor we passed in, so we can read the
weights of its linear layer the same way as before.
print("weights (r, u):", predictor[0].weight.detach().numpy().ravel().round(2))
evaluate(mitigator.predict(X_test))
weights (r, u): [-0.52 0.57]
accuracy selection_rate
sensitive_feature_0
0 0.664206 0.530527
1 0.653869 0.549719
Overall accuracy: 0.659
Demographic parity difference: 0.019
What changed#
The weight on \(r\) is now negative. Since \(u\) is higher on average for \(r = 1\), a model that only used \(u\) would still favor that group. To give both groups the same selection rate, the predictor uses \(r\) to undo the part of \(u\) that comes from group membership. The demographic parity difference drops close to zero, and we pay for it with some accuracy.
Adversarial training is sensitive to its settings. If you change alpha,
the learning rate, or the number of steps, check the metrics again;
AdversarialFairness Fine Tuning shows how to adjust them during training with
callbacks.
Keep in mind that this is a toy problem. It shows how the method works, not when to use it. Whether demographic parity is the right goal, and whether using the sensitive feature this way is acceptable, depends on the real deployment context and the harms involved. Synthetic data can’t answer those questions.
References#
Total running time of the script: (0 minutes 21.565 seconds)
Estimated memory usage: 702 MB