-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_law.py
executable file
·298 lines (287 loc) · 11.3 KB
/
main_law.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
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
import argparse
import time
import csv
import os
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from dataset import LawSchoolDataset
from models import MLPNet, WassersteinNet, CENet
from utils import conditional_mse_errors
from utils import get_logger
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--name", help="Name used to save the log file.", type=str, default="law")
parser.add_argument("-s", "--seed", help="Random seed.", type=int, default=42)
parser.add_argument("-u", "--mu", help="Hyperparameter of the coefficient of the adversarial loss",
type=float, default=0.0)
parser.add_argument("-e", "--epoch", help="Number of training epochs", type=int, default=100)
parser.add_argument("-r", "--lr", type=float, help="Learning rate of optimization", default=0.2)
parser.add_argument("-b", "--batch_size", help="Batch size during training", type=int, default=128)
parser.add_argument("-m", "--model", type=str,
help="Which model to run: [mlp|wmlp|CENet]",
default="mlp")
parser.add_argument("-c", '--clip', type=float, default=0.2, help="parameters in WassersteinNet")
# Compile and configure all the model parameters.
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.set_num_threads(8)
logger = get_logger(args.name)
# Set random number seed.
np.random.seed(args.seed)
torch.manual_seed(args.seed)
dtype = np.float32
logger.info("--------------------------------------------------")
logger.info("Running Law School data set regression analysis")
logger.info("seed: {}".format(args.seed))
# Load Law School dataset.
time_start = time.time()
law_train = LawSchoolDataset(root_dir='data', phase='train')
law_test = LawSchoolDataset(root_dir='data', phase='test')
train_loader = DataLoader(law_train, batch_size=args.batch_size, shuffle=True)
test_loader = DataLoader(law_test, batch_size=args.batch_size, shuffle=False)
time_end = time.time()
logger.info("Time used to load all the data sets: {} seconds.".format(time_end - time_start))
input_dim = law_train.xdim
num_groups = 2
use_sigmoid = True
configs = {"num_groups": num_groups,
"num_epochs": args.epoch,
"batch_size": args.batch_size,
"lr": args.lr,
"mu": args.mu,
"use_sigmoid": use_sigmoid,
"input_dim": input_dim,
"weight_clipping": args.clip, # parameters in Wass' Net
"hidden_layers": [10],
"adversary_layers": [10]}
num_epochs = configs["num_epochs"]
batch_size = configs["batch_size"]
lr = configs["lr"]
if args.model == "mlp":
# Train MLPNet to get baseline results.
logger.info("Experiment without debiasing:")
logger.info("Hyperparameter setting = {}.".format(configs))
# Train MLPNet without debiasing.
time_start = time.time()
net = MLPNet(configs).to(device)
logger.info("Model architecture: {}".format(net))
optimizer = optim.Adadelta(net.parameters(), lr=lr)
net.train()
for t in range(num_epochs):
running_loss, total = 0.0, 0
for xs, ys, attrs in train_loader:
xs, ys, attrs = xs.to(device), ys.to(device), attrs.to(device)
optimizer.zero_grad()
ypreds = net(xs)
loss = F.mse_loss(ypreds, ys)
running_loss += loss.item() * len(ys)
total += len(ys)
loss.backward()
optimizer.step()
running_loss = running_loss / total
logger.info("Iteration {}, loss value = {}".format(t, running_loss))
time_end = time.time()
logger.info("Time used for training = {} seconds.".format(time_end - time_start))
# inference
net.eval()
running_loss, total = 0.0, 0
ypreds_numpy, ys_numpy, attrs_numpy = [], [], []
for xs, ys, attrs in test_loader:
xs, ys, attrs = xs.to(device), ys.to(device), attrs.to(device)
ypreds = net(xs)
loss = F.mse_loss(ypreds, ys)
# logging and saving
running_loss += loss.item() * len(ys)
total += len(ys)
ypreds_numpy.append(ypreds.detach().cpu().numpy())
ys_numpy.append(ys.cpu().numpy())
attrs_numpy.append(attrs.cpu().numpy())
# summation and logging
running_loss = running_loss / total
ypreds_numpy = np.concatenate(ypreds_numpy, axis=0).squeeze()
ys_numpy = np.concatenate(ys_numpy, axis=0).squeeze()
attrs_numpy = np.concatenate(attrs_numpy, axis=0)
cls_error, error_0, error_1 = conditional_mse_errors(ypreds_numpy, ys_numpy, attrs_numpy)
logger.info("Inference, loss value = {}".format(running_loss))
logger.info("Overall predicted error = {}, Err|A=0 = {}, Err|A=1 = {}".format(cls_error, error_0, error_1))
logger.info("Error gap = {}".format(np.abs(error_0-error_1)))
ys_var = np.var(ys_numpy)
r_squared = 1 - cls_error/ys_var
logger.info("R squared = {}".format(r_squared))
nmse = cls_error/ys_var
# save data to csv
csv_data = {"cls_error": cls_error,
"error_0": error_0,
"error_1": error_1,
"err_gap": np.abs(error_0-error_1),
"R^2": r_squared,
"nmse": nmse
}
csv_fn = args.name + ".csv"
with open(csv_fn, "a") as csv_file:
fieldnames = ["cls_error", "error_0", "error_1", "err_gap", "R^2", "nmse"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if os.path.exists(csv_fn):
pass # no need to write headers
else:
writer.writeheader()
writer.writerow(csv_data)
elif args.model == "wmlp":
# train wass MLP
logger.info("Experiment with Wasserstein mlp: {} ".format(args.model))
logger.info("Hyperparameter setting = {}.".format(configs))
time_start = time.time()
net = WassersteinNet(configs).to(device)
logger.info("Model architecture: {}".format(net))
optimizer = optim.Adadelta(net.parameters(), lr=lr)
mu = args.mu
net.train()
for t in range(num_epochs):
running_loss, running_adv_loss, total = 0.0, 0.0, 0
for xs, ys, attrs in train_loader:
## add clip norm in advesarial network to achieve Lipschitzness ##
for p in net.adversaries.parameters():
p.data.clamp_(-args.clip, args.clip)
for p in net.sensitive_output_layer.parameters():
p.data.clamp_(-args.clip, args.clip)
# forward and calculate loss
xs, ys, attrs = xs.to(device), ys.to(device), attrs.to(device)
optimizer.zero_grad()
ypreds, advesary_out = net(xs, ys)
idx = attrs == 0 # index of sensitive '0'
fw_0 = torch.mean(advesary_out[idx], dim=0).squeeze()
fw_1 = torch.mean(advesary_out[~idx], dim=0).squeeze()
loss = F.mse_loss(ypreds, ys)
adv_loss = torch.abs(fw_0 - fw_1)
running_loss += loss.item() * len(ys)
running_adv_loss += adv_loss.item() * len(ys)
total += len(ys)
loss -= mu * adv_loss
loss.backward()
optimizer.step()
running_loss = running_loss / total
running_adv_loss = running_adv_loss / total
logger.info("Iteration {}, loss value = {}, adv_loss value = {}".format(t, running_loss, running_adv_loss))
time_end = time.time()
logger.info("Time used for training = {} seconds.".format(time_end - time_start))
# inference
net.eval()
running_loss, total = 0.0, 0
ypreds_numpy, ys_numpy, attrs_numpy = [], [], []
for xs, ys, attrs in test_loader:
xs, ys, attrs = xs.to(device), ys.to(device), attrs.to(device)
ypreds = net.inference(xs)
loss = F.mse_loss(ypreds, ys)
# logging and saving
running_loss += loss.item() * len(ys)
total += len(ys)
ypreds_numpy.append(ypreds.detach().cpu().numpy())
ys_numpy.append(ys.cpu().numpy())
attrs_numpy.append(attrs.cpu().numpy())
# summation and logging
running_loss = running_loss / total
ypreds_numpy = np.concatenate(ypreds_numpy, axis=0).squeeze()
ys_numpy = np.concatenate(ys_numpy, axis=0).squeeze()
attrs_numpy = np.concatenate(attrs_numpy, axis=0)
cls_error, error_0, error_1 = conditional_mse_errors(ypreds_numpy, ys_numpy, attrs_numpy)
logger.info("Inference, loss value = {}".format(running_loss))
logger.info("Overall predicted error = {}, Err|A=0 = {}, Err|A=1 = {}".format(cls_error, error_0, error_1))
logger.info("Error gap = {}".format(np.abs(error_0-error_1)))
ys_var = np.var(ys_numpy)
r_squared = 1 - cls_error/ys_var
logger.info("R squared = {}".format(r_squared))
nmse = cls_error/ys_var
# save data to csv
csv_data = {"cls_error": cls_error,
"error_0": error_0,
"error_1": error_1,
"err_gap": np.abs(error_0-error_1),
"R^2": r_squared,
"nmse": nmse
}
csv_fn = args.name + ".csv"
with open(csv_fn, "a") as csv_file:
fieldnames = ["cls_error", "error_0", "error_1", "err_gap", "R^2", "nmse"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if os.path.exists(csv_fn):
pass # no need to write headers
else:
writer.writeheader()
writer.writerow(csv_data)
elif args.model == "CENet":
logger.info("Experiment with CENet: {} ".format(args.model))
logger.info("Hyperparameter setting = {}.".format(configs))
time_start = time.time()
net = CENet(configs).to(device)
logger.info("Model architecture: {}".format(net))
optimizer = optim.Adadelta(net.parameters(), lr=lr)
mu = args.mu
net.train()
for t in range(num_epochs):
running_loss, running_adv_loss, total = 0.0, 0.0, 0
for xs, ys, attrs in train_loader:
xs, ys, attrs = xs.to(device), ys.to(device), attrs.to(device)
optimizer.zero_grad()
ypreds, apreds = net(xs, ys)
# Compute both the prediction loss and the adversarial loss
loss = F.mse_loss(ypreds, ys)
adv_loss = F.nll_loss(apreds, attrs)
running_loss += loss.item() * len(ys)
running_adv_loss += adv_loss.item() * len(ys)
total += len(ys)
loss += mu * adv_loss
loss.backward()
optimizer.step()
running_loss = running_loss / total
running_adv_loss = running_adv_loss / total
logger.info("Iteration {}, loss value = {}, adv_loss value = {}".format(t, running_loss, running_adv_loss))
time_end = time.time()
logger.info("Time used for training = {} seconds.".format(time_end - time_start))
# inference
net.eval()
running_loss, total = 0.0, 0
ypreds_numpy, ys_numpy, attrs_numpy = [], [], []
for xs, ys, attrs in test_loader:
xs, ys, attrs = xs.to(device), ys.to(device), attrs.to(device)
ypreds = net.inference(xs)
loss = F.mse_loss(ypreds, ys)
# logging and saving
running_loss += loss.item() * len(ys)
total += len(ys)
ypreds_numpy.append(ypreds.detach().cpu().numpy())
ys_numpy.append(ys.cpu().numpy())
attrs_numpy.append(attrs.cpu().numpy())
# summation and logging
running_loss = running_loss / total
ypreds_numpy = np.concatenate(ypreds_numpy, axis=0).squeeze()
ys_numpy = np.concatenate(ys_numpy, axis=0).squeeze()
attrs_numpy = np.concatenate(attrs_numpy, axis=0)
cls_error, error_0, error_1 = conditional_mse_errors(ypreds_numpy, ys_numpy, attrs_numpy)
logger.info("Inference, loss value = {}".format(running_loss))
logger.info("Overall predicted error = {}, Err|A=0 = {}, Err|A=1 = {}".format(cls_error, error_0, error_1))
logger.info("Error gap = {}".format(np.abs(error_0-error_1)))
ys_var = np.var(ys_numpy)
r_squared = 1 - cls_error/ys_var
logger.info("R squared = {}".format(r_squared))
nmse = cls_error/ys_var
# save data to csv
csv_data = {"cls_error": cls_error,
"error_0": error_0,
"error_1": error_1,
"err_gap": np.abs(error_0-error_1),
"R^2": r_squared,
"nmse": nmse
}
csv_fn = args.name + ".csv"
with open(csv_fn, "a") as csv_file:
fieldnames = ["cls_error", "error_0", "error_1", "err_gap", "R^2", "nmse"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if os.path.exists(csv_fn):
pass # no need to write headers
else:
writer.writeheader()
writer.writerow(csv_data)
else:
raise NotImplementedError