-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel_zoo.py
111 lines (99 loc) · 3.03 KB
/
model_zoo.py
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
"""
Model architectures and parameters.
"""
import numpy as np
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.linear_model import LogisticRegression, LogisticRegressionCV
from sklearn.neural_network import MLPClassifier
from fairlearn.reductions import (
GridSearch,
ExponentiatedGradient,
ErrorRateParity,
DemographicParity,
EqualizedOdds,
)
from fairlearn.postprocessing import (
ThresholdOptimizer
)
import diffprivlib.models as dp
lr_setup = dict(penalty="l2", solver="lbfgs", n_jobs=1, max_iter=500)
model_zoo = {
"lr": lambda: LogisticRegression(**lr_setup),
"lr_dm_threshold": lambda: ThresholdOptimizer(
estimator=LogisticRegression(**lr_setup),
constraints="demographic_parity",
),
"lr_eo_threshold": lambda: ThresholdOptimizer(
estimator=LogisticRegression(**lr_setup),
constraints="equalized_odds",
),
"lr_dm_expgrad": lambda: ExponentiatedGradient(
estimator=LogisticRegression(**lr_setup),
constraints=DemographicParity(difference_bound=0.01),
),
"lr_eo_expgrad": lambda: ExponentiatedGradient(
estimator=LogisticRegression(**lr_setup),
constraints=EqualizedOdds(difference_bound=0.01),
),
"nn_6": lambda: MLPClassifier(
activation="relu",
hidden_layer_sizes=[6],
alpha=0.01,
solver="adam",
max_iter=200,
),
"nn_8": lambda: MLPClassifier(
activation="relu",
hidden_layer_sizes=[8],
alpha=0.01,
solver="adam",
max_iter=200,
),
"nn_32": lambda: MLPClassifier(
activation="relu",
hidden_layer_sizes=[32],
alpha=0.01,
solver="adam",
max_iter=200,
),
"nn_100": lambda: MLPClassifier(
activation="relu",
hidden_layer_sizes=[100],
solver="sgd",
max_iter=1000,
learning_rate_init=0.1,
),
"nn_500": lambda: MLPClassifier(
activation="relu",
hidden_layer_sizes=[500],
solver="sgd",
max_iter=1000,
learning_rate_init=0.1,
),
}
class DpClassifierFactory:
def __init__(self, eps, data_norm):
self.eps = eps
self.data_norm = data_norm
def __call__(self):
return Pipeline([
("scaler", MinMaxScaler()),
("clf", dp.LogisticRegression(max_iter=lr_setup["max_iter"],
epsilon=self.eps,
data_norm=self.data_norm))
])
renaming_dict = dict(
lr="Logistic Regression (LR)",
lr_dm_threshold="Fair LR (Dem. Parity)",
lr_eo_threshold="Fair LR (Equalized Odds)",
lr_dm_expgrad="Fair LR (Dem. Parity, Exp. Grad)",
lr_eo_expgrad="Fair LR (Equalized Odds, Exp. Grad)",
nn_6="6-Neuron NN",
nn_8="8-Neuron NN",
nn_32="32-Neuron NN",
nn_100="100-Neuron NN",
nn_500="500-Neuron NN",
)