-
Notifications
You must be signed in to change notification settings - Fork 2
/
fair_ml.Rmd
1008 lines (804 loc) · 33.9 KB
/
fair_ml.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Creating fair machine learning models with Generative Adversarial Networks"
author: "Hamaad Musharaf Shah"
output:
pdf_document: default
html_notebook: default
html_document:
df_print: paged
---
# License
Copyright 2020 Hamaad Musharaf Shah
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
# Creating fair machine learning models with Generative Adversarial Networks
## Author: Hamaad Shah
## Disclaimer
This work is not necessarily a representation of any past or current employer of mine.
## Introduction
- Ensuring fairness in machine learning is a key aspect especially in the financial services domain.
- Think of a mortgage underwriting machine learning pipeline that perhaps might be sensitive to attributes such as gender and race without explicitly being trained on such features.
- This can potentially cause a hidden cost of capital charge from an operational risk perspective if customers are not treated fairly.
- Clearly a machine learning pipeline that makes unfair decisions given the same feature vector across multiple groups such as gender and race is unethical.
- To overcome this potential issue we discuss a method published in the 31st conference on Neural Information Processing Systems, i.e., NeurIPS 2017.
- The title of that paper: "Learning to Pivot with Adversarial Networks".
- I first came across the aforementioned NeurIPS paper in this blogpost.
- <https://blog.godatadriven.com/fairness-in-ml>
- My lecture will discuss the aforementioned paper and Generative Adversarial Networks (GAN) in detail. It will also provide a R version of the code associated in the aforementioned blogpost.
## Income Prediction
- We use the "Census Income" dataset to predict whether a person's income is greater than $50,000 per year. The link to the dataset is here.
- <https://archive.ics.uci.edu/ml/datasets/Adult>
- The features used are denoted as below.
- $x \sim P_{X}(x)$
- The sensitive attribute(s) are denoted as below.
- $z \sim P_{Z}(z)$
- The target variable is denoted as below.
- $y \sim P_{Y}(y)$
## Classifier and Adversarial Model
- The classifier is a deep learner denoted as below.
- $C(x | \theta_{C}): x \rightarrow y$
- Note that we are not explicitly training the model on the sensitive attributes. However it is entirely possible that there might be some hidden bias within our features that perhaps act as proxies for the sensitive attributes.
- The sensitive attributes for this exercise are race and gender. These are binary variables.
- For each of these sensitive attributes we want to ensure the following holds.
- $P(C(x | \theta_{C})|z = 1) = P(C(x | \theta_{C})|z = 0) \text{ } \forall x \sim P_{X}(x)$
- We introduce the concept of an adversarial deep learner model which takes in the predictions of the classifier in order to predict the sensitive attributes as below.
- $R(C(x | \theta_{C}) | \theta_{R}): C(x | \theta_{C}) \rightarrow z$
- In an ideal scenario the classifier will perform at its optimal level while simultaneously the adversarial model will be completely unable to predict sensitive attributes given the predictions from the optimal classifier. Therefore, $C(x | \theta_{C}^\ast)$ and $Z$ will be independent random variables.
- We now introduce the GAN model in which the generator plays the role of the classifier and the discriminator plays the role of the adversarial model.
## Generative Adversarial Networks
- The purpose of deep learning is to learn a representation of high dimensional and noisy data using a sequence of differentiable functions, i.e., geometric transformations, that can perhaps be used for supervised learning tasks among others.
- It has had great success in discriminative models while generative models have fared worse due to the limitations of explicit maximum likelihood estimation (MLE).
- Adversarial learning as presented in the GAN model aims to overcome these problems by using implicit MLE.
## Generative Adversarial Networks
- There are 2 main components to a GAN, the generator and the discriminator, that play an adversarial game against each other.
- In doing so the generator learns how to create realistic synthetic samples from noise, i.e., the latent space $z$, while the discriminator learns how to distinguish between a real sample and a synthetic sample.
- The representation learnt by the discriminator can later on be used for other supervised learning tasks, i.e., automatic feature engineering or representation learning.
## Generative Adversarial Networks
### Generator
- Assume that we have a prior belief on where the latent space $z$ lies: $p(z)$.
- Given a draw from this latent space the generator $G$, a deep learner parameterized by $\theta_{G}$, outputs a synthetic sample.
- $G(z|\theta_{G}): z \rightarrow x_{synthetic}$
## Generative Adversarial Networks
### Discriminator
- The discriminator $D$ is another deep learner parameterized by $\theta_{D}$ and it aims to classify if a sample is real or synthetic, i.e., if a sample is from the real data distribution: $p_{\text{data}}(x)$
- Or the synthetic data distribution: $p_{G}(x)$
- Let us denote the discriminator $D$ as follows.
- $D(x|\theta_{D}): x \rightarrow [0, 1]$
- Here we assume that the positive examples are from the real data distribution while the negative examples are from the synthetic data distribution.
## Generative Adversarial Networks
### Game
- A GAN simultaneously trains the discriminator to correctly classify real and synthetic examples while training the generator to create synthetic examples such that the discriminator incorrectly classifies real and synthetic examples.
- This 2 player minimax game has the following objective function.
\tiny
$$
\min_{G(z|\theta_{G})} \max_{D(x|\theta_{D})} V(D(x|\theta_{D}), G(z|\theta_{G})) = \mathbb{E}_{x \sim p_{\text{data}}(x)} \log{D(x|\theta_{D})} + \mathbb{E}_{z \sim p(z)} \log{(1 - D(G(z|\theta_{G})|\theta_{D}))}
$$
## Generative Adversarial Networks
### Game
- Please note that the above expression is basically the objective function of the discriminator.
\tiny
$$
\mathbb{E}_{x \sim p_{\text{data}}(x)} \log{D(x|\theta_{D})} + \mathbb{E}_{x \sim p_{G}(x)} \log{(1 - D(x|\theta_{D}))}
$$
\normalsize
- It is clear from how the game has been set up that we are trying to obtain a solution $\theta_{D}$ for $D$ such that it maximizes $V(D, G)$ while simultaneously we are trying to obtain a solution $\theta_{G}$ for $G$ such that it minimizes $V(D, G)$.
## Generative Adversarial Networks
### Game
- We do not simultaneously train $D$ and $G$. We train them alternately: Train $D$ and then train $G$ while freezing $D$. We repeat this for a fixed number of steps.
- If the synthetic samples taken from the generator $G$ are realistic then implicitly we have learnt the distribution $p_{G}(x)$. In other words, $p_{G}(x)$ can be seen as a good estimation of $p_{\text{data}}(x)$.
- The optimal solution will be as follows.
$$
p_{G}(x)=p_{\text{data}}(x)
$$
## Generative Adversarial Networks
### Game
- To show this let us find the optimal discriminator $D^\ast$ given a generator $G$ and sample $x$.
\tiny
\begin{align*}
V(D, G) &= \mathbb{E}_{x \sim p_{\text{data}}(x)} \log{D(x|\theta_{D})} + \mathbb{E}_{x \sim p_{G}(x)} \log{(1 - D(x|\theta_{D}))} \\
&= \int_{x} p_{\text{data}}(x) \log{D(x|\theta_{D})} dx + \int_{x} p_{G}(x) \log{(1 - D(x|\theta_{D}))} dx \\
&= \int_{x} \underbrace{p_{\text{data}}(x) \log{D(x|\theta_{D})} + p_{G}(x) \log{(1 - D(x|\theta_{D}))}}_{J(D(x|\theta_{D}))} dx
\end{align*}
## Generative Adversarial Networks
### Game
- Let us take a closer look at the discriminator's objective function for a sample $x$.
\tiny
\begin{align*}
J(D(x|\theta_{D})) &= p_{\text{data}}(x) \log{D(x|\theta_{D})} + p_{G}(x) \log{(1 - D(x|\theta_{D}))} \\
\frac{\partial J(D(x|\theta_{D}))}{\partial D(x|\theta_{D})} &= \frac{p_{\text{data}}(x)}{D(x|\theta_{D})} - \frac{p_{G}(x)}{(1 - D(x|\theta_{D}))} \\
0 &= \frac{p_{\text{data}}(x)}{D^\ast(x|\theta_{D^\ast})} - \frac{p_{G}(x)}{(1 - D^\ast(x|\theta_{D^\ast}))} \\
p_{\text{data}}(x)(1 - D^\ast(x|\theta_{D^\ast})) &= p_{G}(x)D^\ast(x|\theta_{D^\ast}) \\
p_{\text{data}}(x) - p_{\text{data}}(x)D^\ast(x|\theta_{D^\ast})) &= p_{G}(x)D^\ast(x|\theta_{D^\ast}) \\
p_{G}(x)D^\ast(x|\theta_{D^\ast}) + p_{\text{data}}(x)D^\ast(x|\theta_{D^\ast})) &= p_{\text{data}}(x) \\
D^\ast(x|\theta_{D^\ast}) &= \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}
\end{align*}
## Generative Adversarial Networks
### Game
- We have found the optimal discriminator given a generator. Let us focus now on the generator's objective function which is essentially to minimize the discriminator's objective function.
\tiny
\begin{align*}
J(G(x|\theta_{G})) &= \mathbb{E}_{x \sim p_{\text{data}}(x)} \log{D^\ast(x|\theta_{D^\ast})} + \mathbb{E}_{x \sim p_{G}(x)} \log{(1 - D^\ast(x|\theta_{D^\ast}))} \\
&= \mathbb{E}_{x \sim p_{\text{data}}(x)} \log{\bigg( \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}} \bigg) + \mathbb{E}_{x \sim p_{G}(x)} \log{\bigg(1 - \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}\bigg)} \\
&= \mathbb{E}_{x \sim p_{\text{data}}(x)} \log{\bigg( \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}} \bigg) + \mathbb{E}_{x \sim p_{G}(x)} \log{\bigg(\frac{p_{G}(x)}{p_{\text{data}}(x) + p_{G}(x)}\bigg)} \\
&= \int_{x} p_{\text{data}}(x) \log{\bigg( \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}} \bigg) dx + \int_{x} p_{G}(x) \log{\bigg(\frac{p_{G}(x)}{p_{\text{data}}(x) + p_{G}(x)}\bigg)} dx
\end{align*}
\normalsize
- We will note the Kullback–Leibler (KL) divergences in the above objective function for the generator:
\tiny
$$
D_{KL}(P||Q) = \int_{x} p(x) \log\bigg(\frac{p(x)}{q(x)}\bigg) dx
$$
## Generative Adversarial Networks
### Game
- Recall the definition of a $\lambda$ divergence.
\tiny
$$
D_{\lambda}(P||Q) = \lambda D_{KL}(P||\lambda P + (1 - \lambda) Q) + (1 - \lambda) D_{KL}(Q||\lambda P + (1 - \lambda) Q)
$$
\normalsize
- If $\lambda$ takes the value of 0.5 this is then called the Jensen-Shannon (JS) divergence. This divergence is symmetric and non-negative.
\tiny
$$
D_{JS}(P||Q) = 0.5 D_{KL}\bigg(P\bigg|\bigg|\frac{P + Q}{2}\bigg) + 0.5 D_{KL}\bigg(Q\bigg|\bigg|\frac{P + Q}{2}\bigg)
$$
## Generative Adversarial Networks
### Game
- Keeping this in mind let us take a look again at the objective function of the generator.
\tiny
\begin{align*}
J(G(x|\theta_{G})) &= \int_{x} p_{\text{data}}(x) \log{\bigg( \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}} \bigg) dx + \int_{x} p_{G}(x) \log{\bigg(\frac{p_{G}(x)}{p_{\text{data}}(x) + p_{G}(x)}\bigg)} dx \\
&= \int_{x} p_{\text{data}}(x) \log{\bigg(\frac{2}{2}\frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}} \bigg) dx + \int_{x} p_{G}(x) \log{\bigg(\frac{2}{2}\frac{p_{G}(x)}{p_{\text{data}}(x) + p_{G}(x)}\bigg)} dx \\
&= \int_{x} p_{\text{data}}(x) \log{\bigg(\frac{1}{2}\frac{1}{0.5}\frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)}} \bigg) dx + \int_{x} p_{G}(x) \log{\bigg(\frac{1}{2}\frac{1}{0.5}\frac{p_{G}(x)}{p_{\text{data}}(x) + p_{G}(x)}\bigg)} dx \\
&= \int_{x} p_{\text{data}}(x) \bigg[ \log(0.5) + \log{\bigg(\frac{p_{\text{data}}(x)}{0.5 (p_{\text{data}}(x) + p_{G}(x))}} \bigg) \bigg] dx \\ &+ \int_{x} p_{G}(x) \bigg[\log(0.5) + \log{\bigg(\frac{p_{G}(x)}{0.5 (p_{\text{data}}(x) + p_{G}(x))}\bigg) \bigg] } dx \\
\end{align*}
## Generative Adversarial Networks
### Game
\tiny
\begin{align*}
&= \log\bigg(\frac{1}{4}\bigg) + \int_{x} p_{\text{data}}(x) \bigg[\log{\bigg(\frac{p_{\text{data}}(x)}{0.5 (p_{\text{data}}(x) + p_{G}(x))}} \bigg) \bigg] dx \\
&+ \int_{x} p_{G}(x) \bigg[\log{\bigg(\frac{p_{G}(x)}{0.5 (p_{\text{data}}(x) + p_{G}(x))}\bigg) \bigg] } dx \\
&= -\log(4) + D_{KL}\bigg(p_{\text{data}}(x)\bigg|\bigg|\frac{p_{\text{data}}(x) + p_{G}(x)}{2}\bigg) + D_{KL}\bigg(p_{G}(x)\bigg|\bigg|\frac{p_{\text{data}}(x) + p_{G}(x)}{2}\bigg) \\
&= -\log(4) + 2 \bigg(0.5 D_{KL}\bigg(p_{\text{data}}(x)\bigg|\bigg|\frac{p_{\text{data}}(x) + p_{G}(x)}{2}\bigg) + 0.5 D_{KL}\bigg(p_{G}(x)\bigg|\bigg|\frac{p_{\text{data}}(x) + p_{G}(x)}{2}\bigg)\bigg) \\
&= -\log(4) + 2D_{JS}(p_{\text{data}}(x)||p_{G}(x))
\end{align*}
## Generative Adversarial Networks
### Game
- It is clear from the objective function of the generator above that the global minimum value attained is $-\log(4)$ which occurs when the following holds.
$$
p_{G}(x)=p_{\text{data}}(x)
$$
- When the above holds the Jensen-Shannon divergence, i.e., $D_{JS}(p_{\text{data}}(x)||p_{G}(x))$, will be zero. Hence we have shown that the optimal solution is as follows.
$$
p_{G}(x)=p_{\text{data}}(x)
$$
## Generative Adversarial Networks
### Game
- Note that the above implies that the optimal value for the discriminator is as follows.
\tiny
\begin{align*}
D^\ast(x|\theta_{D^\ast}) &= \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{G}(x)} \\
&= \frac{p_{\text{data}}(x)}{p_{\text{data}}(x) + p_{\text{data}}(x)} \\
&= \frac{1}{2}
\end{align*}
\normalsize
- The optimal discriminator cannot distinguish between real and synthetic data and this is precisely what we need for the adversarial model to do as well: To not be able to distinguish between sensitive attributes.
## Classifier and Adversarial Model
- Returning back to our problem at hand, we use adversarial training to find a classifier such that its predictions cannot be used to predict for sensitive attributes by the adversarial model.
- This 2 player minimax game has the following objective function. Note that $\tilde{y}$ are predictions from the classifier.
\tiny
\begin{align*}
&\max_{C(x|\theta_{C})} \min_{R(C(x|\theta_{C})|\theta_{R})} V(C(x|\theta_{C}), R(C(x|\theta_{C})|\theta_{R})) \\
&= \underbrace{\mathbb{E}_{x \sim P_{X}(x)} \mathbb{E}_{y \sim P_{Y|X}(y|x)} \log{C(x|\theta_{C})}}_\text{Classifier maximizes this term} - \underbrace{\mathbb{E}_{x \sim P_{X}(x)} \mathbb{E}_{\tilde{y} \sim P_{\tilde{Y}|X}(\tilde{y}|x)} \mathbb{E}_{z \sim P_{Z|\tilde{Y}}(z|\tilde{y})} \log{R(C(x|\theta_{C})|\theta_{R})}}_\text{Classifier minimizes this term and the adversarial maximizes this term}
\end{align*}
\normalsize
## Classifier and Adversarial Model
- We note that the classifier is aiming to maximize it's likelihood while minimizing that of the adversarial while the adversarial is aiming to maximize it's own likelihood.
- The solution to this 2 player game theoretically, albeit not empirically as we shall observe, results in a classifier that is both optimal and fair.
- For the optimal classifier $C(x|\theta_{C}^\ast)$ to be fair as well it has to be shown to be independent from the sensitive attributes $Z$.
- Essentially this means that the adversarial model $R(C(x|\theta_{C}^\ast)|\theta_{R})$ is not able to predict the sensitive attributes, i.e., it essentially becomes an uninformative prior on the sensitive attributes as opposed to an informative posterior since the predictions, i.e., new information, from the classifier do not help in updating this uninformative prior, i.e., $P(Z)$.
## Classifier and Adversarial Model
- In light of the above information, the objective function of the 2 player game is bounded above as follows.
- The upper bound holds when $C(x|\theta_{C}^\ast) \perp \!\!\! \perp Z$.
- This basically means that $R(C(x|\theta_{C}^\ast)|\theta_{R})$ cannot be used to predict for the sensitive attributes, i.e., we force the adversarial model towards the uninformative prior $P(Z)$.
\tiny
\begin{align*}
&\max_{C(x|\theta_{C})} \min_{R(C(x|\theta_{C})|\theta_{R})} V(C(x|\theta_{C}), R(C(x|\theta_{C})|\theta_{R})) \\
&= \underbrace{\mathbb{E}_{x \sim P_{X}(x)} \mathbb{E}_{y \sim P_{Y|X}(y|x)} \log{C(x|\theta_{C})}}_\text{Classifier maximizes this term} - \underbrace{\mathbb{E}_{x \sim P_{X}(x)} \mathbb{E}_{\tilde{y} \sim P_{\tilde{Y}|X}(\tilde{y}|x)} \mathbb{E}_{z \sim P_{Z|\tilde{Y}}(z|\tilde{y})} \log{R(C(x|\theta_{C})|\theta_{R})}}_\text{Classifier minimizes this term and the adversarial maximizes this term} \\
&\leq \mathbb{E}_{x \sim P_{X}(x)} \mathbb{E}_{y \sim P_{Y|X}(y|x)} \log{C(x|\theta_{C}^\ast)} - \mathbb{E}_{z \sim P_{Z}(z)} \log{R(C(x|\theta_{C}^\ast)|\theta_{R})}
\end{align*}
\normalsize
- The optimal classifier $C(x|\theta_{C}^\ast)$ maximizes it's own likelihood and minimizes the likelihood of the adversarial such that the upper bound holds, i.e., $R(C(x|\theta_{C}^\ast)|\theta_{R})$ is forced to move towards the uninformative prior $P(Z)$. Hence the classifier is theoretically both optimal and fair.
- Empirically the upper bound is not strict therefore a trade off has to be made between the classifier being optimal and fair. We shall see this in the following experiment using the "Census Income" data.
## Fairness Metric
- To assess a classifier's performance as optimal we can use the AUROC metric.
- To assess a classifier's performance as fair we can use the p-rule metric. This is defined as follows.
- $\min \left(\frac{\mu(\mathbb{\text{1}}_{C(x|\theta_{C}^\ast) > \tau}) | z = 1)}{\mu(\mathbb{\text{1}}_{C(x|\theta_{C}^\ast) > \tau}) | z = 0)}, \frac{\mu(\mathbb{\text{1}}_{C(x|\theta_{C}^\ast) > \tau}) | z = 0)}{\mu(\mathbb{\text{1}}_{C(x|\theta_{C}^\ast) > \tau}) | z = 1)} \right) \geq \frac{p} {100}$
- The intuition above is that at least p% of the time the optimal classifier should give the same predicted outcome label under different levels of the sensitive attribute(s), i.e., think gender and race.
- The optimal classifier is deemed fair p% of the time if it's predicted outcome label does not deviate across gender or race.
- Ideally one would want a high AUROC and a high p-rule. In the experiment on "Census Income" we observe that there is a trade-off to be made between having an optimal classifier and a fair classifier.
- Let me emphatically emphasize that trading off some degree of classifier performance to generate a sufficiently fair classifier, say that has a p-rule score of 80\%, is absolutely worth it. This represents a clear opportunity for machine learning to remove the hidden bias in data and make fair and simultaneously optimal decisions.
\newpage
## R Code
Load up R libraries.
```{r}
library(package = fastDummies)
library(package = data.table)
library(package = dplyr)
library(package = keras)
library(package = hmeasure)
library(package = ggplot2)
library(package = scales)
```
Load up data, do some basic preprocessing and create the set of features, sensitives attributes and target variable.
---
```{r}
data <- fread(
input = "/home/hamaad/Downloads/adult.data",
col.names = c(
"age",
"workclass",
"fnlwgt",
"education",
"education_num",
"marital_status",
"occupation",
"relationship",
"race",
"sex",
"capital_gain",
"capital_loss",
"hours_per_week",
"country",
"target"
),
na.strings = "?"
)
data <- data[which(data$race %in% c("White", "Black"))]
sensitive.attribs <- c("race", "sex")
data[, race := ifelse(race == "White", 1, 0)]
data[, sex := ifelse(sex == "Male", 1, 0)]
# 0 is black and 1 is white.
# 0 is female and 1 is male.
Z <- data[, sensitive.attribs, with = FALSE]
y <- data[, target := ifelse(target == ">50K", 1, 0)][, target]
X <- data[, ":="(target = NULL,
race = NULL,
sex = NULL)]
is.missing.ind <- is.na(X)
X[is.missing.ind] <- "Unknown"
X <- dummy_cols(.data = X, select_columns = c(
"workclass",
"education",
"marital_status",
"occupation",
"relationship",
"country"
), remove_first_dummy = TRUE)
X <- X[, ":="(workclass = NULL,
education = NULL,
marital_status = NULL,
occupation = NULL,
relationship = NULL,
country = NULL)]
```
Split the data into train and test sets.
---
```{r}
tr.ind <- sample(
x = 1:dim(X)[1],
size = as.integer(0.5 * dim(X)[1]),
replace = FALSE
)
ts.ind <- setdiff(
x = 1:dim(X)[1],
y = tr.ind
)
```
Standardise the data.
---
```{r}
mu <- apply(
X = X[tr.ind],
MARGIN = 2,
FUN = mean
)
std <- apply(
X = X[tr.ind],
MARGIN = 2,
FUN = sd
) + .Machine$double.eps
standardised.X.tr <- sweep(
x = X[tr.ind],
MARGIN = 2,
STATS = mu,
FUN = "-"
)
standardised.X.tr <- sweep(
x = standardised.X.tr,
MARGIN = 2,
STATS = std,
FUN = "/"
)
standardised.X.ts <- sweep(
x = X[ts.ind],
MARGIN = 2,
STATS = mu,
FUN = "-"
)
standardised.X.ts <- sweep(
x = standardised.X.ts,
MARGIN = 2,
STATS = std,
FUN = "/"
)
```
Create the classifier.
---
```{r}
classifier.input.layer <- layer_input(
shape = c(dim(standardised.X.tr)[2]),
name = "classifier.input"
)
classifier.hidden.layers <- classifier.input.layer %>%
layer_dense(
units = 32,
activation = "relu",
name = "classifier.hidden.layer.1"
) %>%
layer_dropout(
rate = 0.2,
name = "classifier.dropout.layer.1"
) %>%
layer_dense(
units = 32,
activation = "relu",
name = "classifier.hidden.layer.2"
) %>%
layer_dropout(
rate = 0.2,
name = "classifier.dropout.layer.2"
) %>%
layer_dense(
units = 32,
activation = "relu",
name = "classifier.hidden.layer.3"
) %>%
layer_dropout(
rate = 0.2,
name = "classifier.dropout.layer.3"
)
classifier.output.layer <- classifier.hidden.layers %>%
layer_dense(
units = 1,
activation = "sigmoid",
name = "classifier.output"
)
classifier.model <- keras_model(
inputs = classifier.input.layer,
outputs = classifier.output.layer
)
classifier.model %>% compile(
optimizer = "adam",
loss = "binary_crossentropy"
)
summary(classifier.model)
```
Create the adversarial model.
---
```{r}
adversarial.input.layer <- layer_input(
shape = c(1),
name = "adversarial.input"
)
adversarial.hidden.layers <- adversarial.input.layer %>%
layer_dense(
units = 32,
activation = "relu",
name = "adversarial.hidden.layer.1"
) %>%
layer_dense(
units = 32,
activation = "relu",
name = "adversarial.hidden.layer.2"
) %>%
layer_dense(
units = 32,
activation = "relu",
name = "adversarial.hidden.layer.3"
)
race.layer <- adversarial.hidden.layers %>%
layer_dense(
units = 1,
activation = "sigmoid",
name = "race.layer.output"
)
sex.layer <- adversarial.hidden.layers %>%
layer_dense(
units = 1,
activation = "sigmoid",
name = "sex.layer.output"
)
adversarial.model <- keras_model(
inputs = adversarial.input.layer,
outputs = c(
race.layer,
sex.layer
)
)
summary(adversarial.model)
```
We freeze the classifier weights and unfreeze the adversarial model weights in order to train the adversarial model.
---
```{r}
unfrozen.adversarial.model <- keras_model(
inputs = classifier.input.layer,
outputs = adversarial.model(object = classifier.output.layer)
)
freeze_weights(
object = classifier.model,
from = "classifier.input",
to = "classifier.output"
)
unfreeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "race.layer.output"
)
unfreeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "sex.layer.output"
)
unfrozen.adversarial.model %>% compile(
optimizer = "adam",
loss = c(
"binary_crossentropy",
"binary_crossentropy"
)
)
summary(unfrozen.adversarial.model)
```
We freeze the adversarial weights and unfreeze the classifier model weights in order to train the classifier model.
---
```{r}
frozen.adversarial.model <- keras_model(
inputs = classifier.input.layer,
outputs = c(
classifier.output.layer,
adversarial.model(object = classifier.output.layer)
)
)
unfreeze_weights(
object = classifier.model,
from = "classifier.input",
to = "classifier.output"
)
freeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "race.layer.output"
)
freeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "sex.layer.output"
)
summary(frozen.adversarial.model)
frozen.adversarial.model %>% compile(
optimizer = "adam", metrics = NULL,
loss_weights = c(1, -100, -50),
loss = c(
"binary_crossentropy",
"binary_crossentropy",
"binary_crossentropy"
)
)
```
Pre-train the classifier.
---
```{r}
unfreeze_weights(
object = classifier.model,
from = "classifier.input",
to = "classifier.output"
)
classifier.model %>% fit(
x = as.matrix(standardised.X.tr),
y = y[tr.ind],
epochs = 25,
batch_size = 100,
verbose = 1,
shuffle = TRUE
)
```
Pre-train the adversarial model.
---
```{r}
freeze_weights(
object = classifier.model,
from = "classifier.input",
to = "classifier.output"
)
unfreeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "race.layer.output"
)
unfreeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "sex.layer.output"
)
unfrozen.adversarial.model %>% fit(
x = as.matrix(standardised.X.tr),
y = list(
as.matrix(Z[tr.ind, "race", with = FALSE]),
as.matrix(Z[tr.ind, "sex", with = FALSE])
),
epochs = 25,
batch_size = 100,
verbose = 1,
shuffle = TRUE
)
```
Make some basic plots and check the AUC and P-rule measures to check whether the model is optimal and fair.
---
```{r}
pred.test <- classifier.model %>% predict(x = as.matrix(standardised.X.ts))
plot.out <- data.table(
pred.test,
Z[ts.ind]
)
colnames(plot.out) <- c(
"predictions",
sensitive.attribs
)
thrs <- 0.5
y.z.1 <- ifelse(test = plot.out$predictions[which(plot.out$race == 1)] > thrs,
yes = 1.0,
no = 0.0
)
y.z.0 <- ifelse(test = plot.out$predictions[which(plot.out$race == 0)] > thrs,
yes = 1.0,
no = 0.0
)
odds <- mean(x = y.z.1) / mean(x = y.z.0)
out.race <- min(x = c(odds, 1 / odds))
y.z.1 <- ifelse(test = plot.out$predictions[which(plot.out$sex == 1)] > thrs,
yes = 1.0,
no = 0.0
)
y.z.0 <- ifelse(test = plot.out$predictions[which(plot.out$sex == 0)] > thrs,
yes = 1.0,
no = 0.0
)
odds <- mean(x = y.z.1) / mean(x = y.z.0)
out.sex <- min(x = c(odds, 1.0 / odds))
output.plot <- ggplot(data = plot.out) +
geom_density(aes(
x = predictions,
fill = factor(race)
),
alpha = 0.5
) +
ggtitle(paste(
"Predictions for income being higher than $50,000 per year\nGrouped by race\nP-rule:",
percent(out.race),
"\nAUROC:",
percent(HMeasure(
true.class = y[ts.ind],
scores = pred.test
)$metrics$AUC)
)) +
scale_fill_discrete(
name = "Race",
labels = c("Black", "White")
) +
scale_x_continuous(labels = percent) +
xlab(label = "Classifier predictions") +
ylab(label = "Density")
ggsave(
filename = "/home/hamaad/Projects/fair_ml_R/pre_train_result_race.png",
plot = output.plot
)
output.plot <- ggplot(data = plot.out) +
geom_density(aes(
x = predictions,
fill = factor(sex)
),
alpha = 0.5
) +
ggtitle(paste(
"Predictions for income being higher than $50,000 per year\nGrouped by gender\nP-rule:",
percent(out.sex),
"\nAUROC:",
percent(HMeasure(
true.class = y[ts.ind],
scores = pred.test
)$metrics$AUC)
)) +
scale_fill_discrete(
name = "Gender",
labels = c("Female", "Male")
) +
scale_x_continuous(labels = percent) +
xlab(label = "Classifier predictions") +
ylab(label = "Density")
ggsave(
filename = "/home/hamaad/Projects/fair_ml_R/pre_train_result_gender.png",
plot = output.plot
)
```
\newpage
## Initial model performance
![Initial model performance with regards to optimality and fairness grouped by race](pre_train_result_race.png){height=250px}
## Initial model performance
![Initial model performance with regards to optimality and fairness grouped by gender](pre_train_result_gender.png){height=250px}
Train the classifier and the adversarial model alternately.
---
```{r}
for (i in 1:500) {
freeze_weights(
object = classifier.model,
from = "classifier.input",
to = "classifier.output"
)
unfreeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "race.layer.output"
)
unfreeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "sex.layer.output"
)
batch.ind <- sample(x = 1:length(tr.ind), size = 100, replace = FALSE)
unfrozen.adversarial.model %>% train_on_batch(
x = as.matrix(standardised.X.tr)[batch.ind, ],
y = list(
as.matrix(Z[tr.ind, "race", with = FALSE])[batch.ind, ],
as.matrix(Z[tr.ind, "sex", with = FALSE])[batch.ind, ]
)
)
pred.test <- classifier.model %>% predict(as.matrix(standardised.X.ts))
plot.out <- data.table(pred.test, Z[ts.ind])
colnames(plot.out) <- c(
"predictions",
sensitive.attribs
)
y.z.1 <- ifelse(test = plot.out$predictions[which(plot.out$race == 1)] > thrs,
yes = 1.0,
no = 0.0
)
y.z.0 <- ifelse(test = plot.out$predictions[which(plot.out$race == 0)] > thrs,
yes = 1.0,
no = 0.0
)
odds <- mean(x = y.z.1) / mean(x = y.z.0)
out.race <- min(x = c(odds, 1 / odds))
y.z.1 <- ifelse(test = plot.out$predictions[which(plot.out$sex == 1)] > thrs,
yes = 1.0,
no = 0.0
)
y.z.0 <- ifelse(test = plot.out$predictions[which(plot.out$sex == 0)] > thrs,
yes = 1.0,
no = 0.0
)
odds <- mean(x = y.z.1) / mean(x = y.z.0)
out.sex <- min(x = c(odds, 1.0 / odds))
unfreeze_weights(
object = classifier.model,
from = "classifier.input",
to = "classifier.output"
)
freeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "race.layer.output"
)
freeze_weights(
object = adversarial.model,
from = "adversarial.input",
to = "sex.layer.output"
)
batch.ind <- sample(
x = 1:length(tr.ind),
size = 100,
replace = FALSE
)
frozen.adversarial.model %>% train_on_batch(
x = as.matrix(standardised.X.tr)[batch.ind, ],
y = list(
as.matrix(data.table(y)[tr.ind])[batch.ind, ],
as.matrix(Z[tr.ind, "race", with = FALSE])[batch.ind, ],
as.matrix(Z[tr.ind, "sex", with = FALSE])[batch.ind, ]
)
)
}
```
Final results.
---
```{r}
output.plot <- ggplot(data = plot.out) +
geom_density(aes(
x = predictions,
fill = factor(race)
),
alpha = 0.5
) +
ggtitle(paste(
"Predictions for income being higher than $50,000 per year\nGrouped by race\nP-rule:",
percent(out.race),
"\nAUROC:",
percent(HMeasure(true.class = y[ts.ind], scores = pred.test)$metrics$AUC)
)) +
scale_fill_discrete(
name = "Race",
labels = c("Black", "White")
) +
scale_x_continuous(labels = percent) +
xlab(label = "Classifier predictions") +
ylab(label = "Density")
ggsave(
filename = "/home/hamaad/Projects/fair_ml_R/post_train_result_race.png",
plot = output.plot
)
output.plot <- ggplot(data = plot.out) +
geom_density(aes(
x = predictions,
fill = factor(sex)
),
alpha = 0.5
) +
ggtitle(paste(
"Predictions for income being higher than $50,000 per year\nGrouped by gender\nP-rule:",
percent(out.sex),
"\nAUROC:",
percent(HMeasure(true.class = y[ts.ind], scores = pred.test)$metrics$AUC)
)) +
scale_fill_discrete(
name = "Gender",
labels = c("Female", "Male")
) +
scale_x_continuous(labels = percent) +
xlab(label = "Classifier predictions") +
ylab(label = "Density")
ggsave(
filename = "/home/hamaad/Projects/fair_ml_R/post_train_result_gender.png",
plot = output.plot
)
```
\newpage
## Final model performance
![Final model performance with regards to optimality and fairness grouped by race](post_train_result_race.png){height=250px}
## Final model performance
![Final model performance with regards to optimality and fairness grouped by gender](post_train_result_gender.png){height=250px}
\newpage
## Conclusion
- We have shown how to use adversarial training, inspired by the GAN model, in order to provide a trade off between an optimal classifier and a fair classifier, i.e., one that is not sensitive to attributes such as race and gender.
- The results shows that machine learning might actually be used to train fair decision making pipelines such that the data's hidden bias is not reflected in this decision making, even though sensitive attributes might have been explicitly removed.
- This is a powerful and interesting way of using the idea of adversarial training derived from the GAN model for a goal that is highly desirable from an ethics perspective.
## References
1. Goodfellow, I., Bengio, Y. and Courville A. (2016). Deep Learning (MIT Press).
2. Geron, A. (2017). Hands-On Machine Learning with Scikit-Learn & Tensorflow (O'Reilly).
3. Radford, A., Luke, M. and Chintala, S. (2015). Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks (<https://arxiv.org/abs/1511.06434>).
4. Goodfellow, I., Pouget-Abadie, J., Mirza, M., Xu, B., Warde-Farley, D., Ozair, S., Courville, A., Bengio, Y. (2014). Generative Adversarial Networks (<https://arxiv.org/abs/1406.2661>).
5. Chollet, F., Allaire, J. J. (2018). Deep Learning with R (Manning).
6. <https://blog.godatadriven.com/fairness-in-ml>
7. Louppe, G., Kagan, M., Kranmer, K. (2017). Learning to Pivot with Adversarial Networks (<https://papers.nips.cc/paper/6699-learning-to-pivot-with-adversarial-networks.pdf>)
8. Zafar, M. B., Valera, I., Rodriguez, M. G., Gummadi, K. P. (2017). Fairness Constraints: Mechanisms for Fair Classification (<https://arxiv.org/pdf/1507.05259.pdf>)
9. <https://towardsdatascience.com/automatic-feature-engineering-using-generative-adversarial-networks-8e24b3c16bf3>
## Blog on Medium and Code on GitHub
- On GitHub:
- <https://github.com/hamaadshah/autoencoders_tensorflow>
- <https://github.com/hamaadshah/gan_tensorflow>
- <https://github.com/hamaadshah/market_risk_gan_tensorflow>