-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
365 lines (281 loc) · 14.9 KB
/
utils.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
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
import torch
import torch.nn as nn
import torch.nn.functional as F ## F.softmax, F.log_softmax
import torch.nn.init as init
import math
import numpy as np
import gmm
from models.encoder import *
from models.decoder import *
## dataset
from data_preprocess.load_health import load_health
from data_preprocess.load_utkface import load_utkface
from data_preprocess.load_nlps import load_twitter
from data_preprocess.load_german import load_german
def one_hot_encoding(label, num_classes, use_cuda=False):
label_cpu = label.cpu().data
onehot = torch.zeros(label_cpu.size(0), num_classes).scatter_(1, label_cpu.unsqueeze(1), 1.).float()
return onehot.cuda() if use_cuda else onehot
def pairwise_kl(mu, sigma, add_third_term=False):
# k = K.shape(sigma)[1]
# k = K.cast(k, 'float32')
d = float(sigma.size(1))
# var = K.square(sigma) + 1e-8
var = sigma**2 + 1e-8
# var_inv = K.tf.reciprocal(var)
# var_diff = K.dot(var, K.transpose(var_inv))
var_inv = 1./var
var_diff = torch.matmul(var, torch.t(var_inv)) ## tr(S2^-1 S1)
# r = K.dot(mu * mu, K.transpose(var_inv))
# r2 = K.sum(mu * mu * var_inv, axis=1)
# mu_var_mu = 2 * K.dot(mu, K.transpose(mu * var_inv))
# mu_var_mu = r - mu_var_mu + K.transpose(r2)
r = torch.matmul(mu**2, torch.t(var_inv)) ## batch x batch
r2 = torch.sum(mu*mu*var_inv, dim=1, keepdim=True) ## batch x 1
mu_var_mu = 2 * torch.matmul(mu, torch.t(mu * var_inv))
mu_var_mu = r - mu_var_mu + torch.t(r2)
if add_third_term: # this term cancels out for examples in a batch (meaning = 0)
log_det = torch.sum(torch.log(var), dim=1, keepdim=True)
log_def_diff = log_det - torch.t(log_det)
else:
log_det_diff = 0.
KL = 0.5 * (var_diff + mu_var_mu + log_det_diff - d) ## batch x batch
return KL
def kl_conditional_and_marg(mu, sigma):
b = float(sigma.size(0))
d = float(sigma.size(1))
### H(z|x)
H = 0.5*(torch.sum(torch.log(sigma**2 + 1e-8), dim=1)
+ d*(1 + math.log(2 * math.pi))) ## d/2*log(2e\pi det)
KL = pairwise_kl(mu, sigma)
return 1.0/b * torch.mean(torch.sum(KL, dim=1) + (b - 1) * H - math.log(b))
def information_bottleneck(mu, sigma):
return F.relu(torch.mean(pairwise_kl(mu, sigma))) ## to guarantee the estimation is larger than 0
def conditional_gaussian_entropy(mu, sigma):
d = float(sigma.size(1))
H = 0.5*(torch.sum(torch.log(sigma**2 + 1e-8), dim=1)
+ d*(1 + math.log(2 * math.pi)))
return torch.mean(H)
def variational_mutual_information_estimator(mu, sigma, s, sensitive_num_classes, iters=10):
KL = F.relu(pairwise_kl(mu, sigma))
KL_exp = torch.exp(-KL)
N = float(s.size(0))
mutual_info = 0
for slabel in range(sensitive_num_classes):
slabel_mask = torch.eq(s, slabel).float()
total_slabel = torch.sum(slabel_mask)
if total_slabel > 0:
slabel_mask_matrix = slabel_mask.repeat(KL_exp.size(0), 1)
phi = slabel_mask_matrix/N/float(total_slabel)
for iter in range(iters):
psi = 1./N*phi.t()/(torch.sum(phi, dim=1) + 1e-16)
inter_mat = psi*slabel_mask_matrix.t()*KL_exp
phi = (1./float(total_slabel)*inter_mat.t())/(torch.sum(inter_mat, dim=1) + 1e-16)
phi, psi = phi.detach(), psi.detach()
mutual_info += torch.sum(phi.t()*slabel_mask_matrix.t()*KL)
return mutual_info/float(sensitive_num_classes)
def mcmc_mutual_information_estimator(mu, sigma, s, sensitive_num_classes, n=50000):
sigma = np.square(sigma)
weights = np.ones(mu.shape[0])/mu.shape[0]
data_gmm = gmm.GMM(mu, sigma, weights)
mutual_info = 0
for slabel in range(sensitive_num_classes):
means, covars = [], []
counter = 0
for i in range(s.shape[0]):
if s[i] == slabel:
means.append(mu[i])
covars.append(sigma[i])
counter += 1
means, covars = np.array(means), np.array(covars)
weights = np.ones(means.shape[0])/means.shape[0]
s_data_gmm = gmm.GMM(means, covars, weights)
samples = s_data_gmm.sample(n)
mutual_info += 1./n * np.sum(s_data_gmm.log_likelihood(samples) - data_gmm.log_likelihood(samples)) * counter
return mutual_info/float(s.shape[0])
def batch_diag(a):
b = torch.eye(a.size(1), device=a.device)
return a.unsqueeze(2).expand(*a.size(), a.size(1))*b
def gaussian_mutual_information_estimator(mu, sigma, s, sensitive_num_classes):
d = float(mu.size(1))
mu_1 = torch.mean(mu, dim=0)
mu_d = mu-mu_1.repeat(mu.size(0), 1)
Sigma = batch_diag(sigma) + torch.bmm(mu_d.unsqueeze(2), mu_d.unsqueeze(1))
sigma_1 = torch.mean(Sigma, dim=0)
mutual_info = 0
for slabel in range(sensitive_num_classes):
slabel_mask = torch.eq(s, slabel).float()
total_slabel = torch.sum(slabel_mask)
mu_2 = torch.sum(slabel_mask*mu, dim=0)/(total_slabel+1e-8)
sigma_2 = torch.sum(slabel_mask*Sigma, dim=0)/(total_slabel+1e-8)
sigma_2_inverse = torch.inverse(sigma_1)
mutual_info += 0.5*(torch.log(F.relu(sigma_2.det()/(sigma_1.det()+1e-8))+1e-8) - d +
torch.trace(sigma_2_inverse * sigma_1) +
torch.mm(torch.mm((mu_2 - mu_1).unsqueeze(0), sigma_2_inverse),
(mu_2 - mu_1).unsqueeze(1)))
return mutual_info
def fair_pred_mutual_information(output, s, sensitive_num_classes, T=0.1):
mutual_info = 0
gumbel_pred = F.gumbel_softmax(output, tau=T)
class_prob = torch.sum(gumbel_pred, dim=0)/torch.sum(gumbel_pred)
for slabel in range(sensitive_num_classes):
slabel_mask = torch.eq(s, slabel).float()
total_slabel = torch.sum(slabel_mask)
slabel_pred = slabel_mask.unsqueeze(1)*gumbel_pred
cond_prob = torch.sum(slabel_pred, dim=0)/(torch.sum(slabel_pred)+ 1e-8)
mutual_info += torch.sum(cond_prob*torch.log(cond_prob/(class_prob + 1e-8) + 1e-8))
return mutual_info/float(sensitive_num_classes)
def fairness_metric(sensitive_class_count, sensitive_num_classes):
sensitive_total_count = [0]*len(sensitive_class_count[0])
sensitive_class_freq = {}
for slabel in range(sensitive_num_classes):
sensitive_class_freq[slabel] = np.array(sensitive_class_count[slabel])/sum(sensitive_class_count[slabel])
sensitive_total_count = [sensitive_class_count[slabel][i]+sensitive_total_count[i] for i in range(len(sensitive_total_count))]
sensitive_total_freq = np.array(sensitive_total_count)/sum(sensitive_total_count)
mutual_info = 0
for slabel in range(sensitive_num_classes):
mutual_info += np.sum(sensitive_class_freq[slabel] * np.log(sensitive_class_freq[slabel]/(sensitive_total_freq+1e-8)+1e-8))
return sensitive_class_freq, mutual_info/float(sensitive_num_classes)
def spd_metric(sensitive_class_freq, sensitive_num_classes):
total_spd = 0
max_spd = 0
count = 0
sensitive_class_freq_list = []
for slabel in range(sensitive_num_classes):
sensitive_class_freq_list.append(sensitive_class_freq[slabel])
# sensitive_class_freq_array = np.concatenate(sensitive_class_freq_list, axis=0)
for i in range(sensitive_num_classes):
for j in range(i+1, sensitive_num_classes):
abs_spd_arr = np.abs(sensitive_class_freq_list[i] - sensitive_class_freq_list[j])
if np.max(abs_spd_arr) > max_spd:
max_spd = np.max(abs_spd_arr)
total_spd += np.sum(abs_spd_arr)
count += abs_spd_arr.shape[0]
return max_spd, total_spd/count
def print_params_names(model):
for name, param in model.named_parameters():
if param.requires_grad:
print(name, '\n', param.data.size(), '\n', param.data)
def load_data_model(args, info_model=False, return_decoder=False):
print('randomize the representations or not: {}'.format(info_model))
if args.dataset == 'german':
train_loader, test_loader, input_dim, target_num_classes, sensitive_num_classes = load_german(attr=args.sensitive_attr,
random_seed=args.seed,
batch_size=args.batch_size)
print('input dimension: {} \t target classes: {} \t sensitive classes: {}'.format(input_dim, target_num_classes, sensitive_num_classes), flush=True)
### create models
feature_learner = mlp_encoder(in_dim=input_dim, out_dim=args.num_features, drop_rate=args.drop_rate, info_model=info_model) ## learn q(z|x)
if return_decoder:
feature_decoder = mlp_decoder(in_dim=args.num_features+sensitive_num_classes, out_dim=input_dim, drop_rate=args.drop_rate)
if args.dataset == 'health':
train_loader, test_loader, input_dim, target_num_classes, sensitive_num_classes = load_health(attr=args.sensitive_attr,
random_seed=args.seed,
binarize=False,
batch_size=args.batch_size)
print('input dimension: {} \t target classes: {} \t sensitive classes: {}'.format(input_dim, target_num_classes, sensitive_num_classes), flush=True)
### create models
feature_learner = mlp_encoder(in_dim=input_dim, out_dim=args.num_features, drop_rate=args.drop_rate, info_model=info_model) ## learn q(z|x)
if return_decoder:
feature_decoder = mlp_decoder(in_dim=args.num_features+sensitive_num_classes, out_dim=input_dim, drop_rate=args.drop_rate)
elif args.dataset == 'utkface':
train_loader, test_loader, img_dim, target_num_classes, sensitive_num_classes = load_utkface(target_attr=args.target_attr,
sensitive_attr=args.sensitive_attr,
random_seed=args.seed,
batch_size=args.batch_size)
print('image dimension: {} \t target classes: {} \t sensitive classes: {}'.format(img_dim, target_num_classes, sensitive_num_classes), flush=True)
feature_learner = lenet_encoder(img_dim=img_dim, out_dim=args.num_features, drop_rate=args.drop_rate, info_model=info_model)
if return_decoder:
feature_decoder = lenet_decoder(input_dim=args.num_features+sensitive_num_classes, img_dim=img_dim, drop_rate=args.drop_rate)
elif args.dataset == 'twitter':
train_loader, test_loader, voc_size, target_num_classes, sensitive_num_classes = load_twitter(sensitive_attr=args.sensitive_attr,
random_seed=args.seed,
batch_size=args.batch_size)
print('vocabulary size: {} \t target classes: {} \t sensitive classes: {}'.format(voc_size, target_num_classes, sensitive_num_classes), flush=True)
feature_learner = lstm_encoder(voc_size=voc_size, embedding_dim=32,
out_dim=args.num_features, drop_rate=args.drop_rate, info_model=info_model)
if return_decoder:
feature_decoder = lstm_decoder(voc_size=voc_size, fea_dim=args.num_features+sensitive_num_classes,
hidden_dim=args.num_features, drop_rate=args.drop_rate)
if return_decoder:
return train_loader, test_loader, feature_learner, feature_decoder
else:
return train_loader, test_loader, feature_learner
def normal_weight_init(m):
'''
Usage:
model = Model()
model.apply(normal_weight_init)
'''
if isinstance(m, nn.Conv1d):
init.normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.Conv2d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.Conv3d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose1d):
init.normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose2d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.ConvTranspose3d):
init.xavier_normal_(m.weight.data)
if m.bias is not None:
init.normal_(m.bias.data)
elif isinstance(m, nn.BatchNorm1d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm2d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.BatchNorm3d):
init.normal_(m.weight.data, mean=1, std=0.02)
init.constant_(m.bias.data, 0)
elif isinstance(m, nn.Linear):
init.xavier_normal_(m.weight.data)
init.normal_(m.bias.data)
elif isinstance(m, nn.LSTM):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.LSTMCell):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.GRU):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
elif isinstance(m, nn.GRUCell):
for param in m.parameters():
if len(param.shape) >= 2:
init.orthogonal_(param.data)
else:
init.normal_(param.data)
def check_args(args):
if args.dataset == 'health':
assert args.target_attr == 'charlson'
assert (args.sensitive_attr == 'gender' or args.sensitive_attr == 'age')
elif args.dataset == 'twitter':
assert args.target_attr == 'age'
assert (args.sensitive_attr == 'gender' or args.sensitive_attr == 'identity')
elif args.dataset == 'utkface':
assert args.target_attr == 'age'
assert (args.sensitive_attr == 'gender' or args.sensitive_attr == 'race')
elif args.dataset == 'german':
assert args.target_attr == 'credit'
assert (args.sensitive_attr == 'gender' or args.sensitive_attr == 'age')