-
Notifications
You must be signed in to change notification settings - Fork 4
/
run_bnn.py
378 lines (276 loc) · 13.2 KB
/
run_bnn.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
366
367
368
369
370
371
372
373
374
375
376
377
378
import argparse
import numpy as np
import torch
import torch.nn as nn
import os
import errno
from process_data import preprocess_data
parser = argparse.ArgumentParser(description='BNN with mixture normal prior')
# Basic Setting
parser.add_argument('--seed', default=1, type = int, help = 'set seed')
parser.add_argument('--data_name', default = 'Simulation', type = str, help = 'data name')
parser.add_argument('--base_path', default='./result/', type = str, help = 'base path for saving result')
parser.add_argument('--model_path', default='test_run/', type = str, help = 'folder name for saving model')
parser.add_argument('--fine_tune_path', default='fine_tune/', type = str, help = 'folder name for saving fine tune model')
parser.add_argument('--num_run', default = 1, type = int, help= 'Number of different initialization used to train the model')
# Network Architecture
parser.add_argument('--layer', default=3, type=int, help='number of hidden layer')
parser.add_argument('--unit', default=[6, 4, 3], type=int, nargs='+', help='number of hidden unit in each layer')
# Training Setting
parser.add_argument('--nepoch', default = 500, type = int, help = 'total number of training epochs')
parser.add_argument('--lr', default = 0.005, type = float, help = 'initial learning rate')
parser.add_argument('--momentum', default = 0, type = float, help = 'momentum in SGD')
parser.add_argument('--batch_train', default = 100, type = int, help = 'batch size for training')
parser.add_argument('--fine_tune_epoch', default = 500, type = int, help = 'total number of fine tuning epochs')
# Prior Setting
parser.add_argument('--sigma0', default = 0.0001, type = float, help = 'sigma_0^2 in prior')
parser.add_argument('--sigma1', default = 0.01, type = float, help = 'sigma_1^2 in prior')
parser.add_argument('--lambdan', default = 0.00001, type = float, help = 'lambda_n in prior')
args = parser.parse_args()
class Net(nn.Module):
def __init__(self, num_hidden, hidden_dim, input_dim, output_dim):
super(Net, self).__init__()
self.num_hidden = num_hidden
self.fc = nn.Linear(input_dim, hidden_dim[0])
self.fc_list = []
for i in range(num_hidden - 1):
self.fc_list.append(nn.Linear(hidden_dim[i], hidden_dim[i + 1]))
self.add_module('fc' + str(i + 2), self.fc_list[-1])
self.fc_list.append(nn.Linear(hidden_dim[-1], output_dim))
self.add_module('fc' + str(num_hidden + 1), self.fc_list[-1])
self.prune_flag = 0
self.mask = None
def forward(self, x):
if self.prune_flag == 1:
for name, para in self.named_parameters():
para.data[self.mask[name]] = 0
x = torch.tanh(self.fc(x))
for i in range(self.num_hidden - 1):
x = torch.tanh(self.fc_list[i](x))
x = self.fc_list[-1](x)
return x
def set_prune(self, user_mask):
self.mask = user_mask
self.prune_flag = 1
def cancel_prune(self):
self.prune_flag = 0
self.mask = None
def gradient(outputs, inputs, grad_outputs=None, retain_graph=None, create_graph=False):
'''
Compute the gradient of `outputs` with respect to `inputs`
gradient(x.sum(), x)
gradient((x * y).sum(), [x, y])
'''
if torch.is_tensor(inputs):
inputs = [inputs]
else:
inputs = list(inputs)
grads = torch.autograd.grad(outputs, inputs, grad_outputs,
allow_unused=True,
retain_graph=retain_graph,
create_graph=create_graph)
grads = [x if x is not None else torch.zeros_like(y) for x, y in zip(grads, inputs)]
return torch.cat([x.contiguous().view(-1) for x in grads])
def hessian(output, inputs, out=None, allow_unused=False, create_graph=False):
'''
Compute the Hessian of `output` with respect to `inputs`
hessian((x * y).sum(), [x, y])
'''
#assert output.ndimension() == 0
if torch.is_tensor(inputs):
inputs = [inputs]
else:
inputs = list(inputs)
n = sum(p.numel() for p in inputs)
if out is None:
out = output.new_zeros(n, n)
ai = 0
for i, inp in enumerate(inputs):
[grad] = torch.autograd.grad(output, inp, create_graph=True, allow_unused=allow_unused)
grad = torch.zeros_like(inp) if grad is None else grad
grad = grad.contiguous().view(-1)
for j in range(inp.numel()):
if grad[j].requires_grad:
row = gradient(grad[j], inputs[i:], retain_graph=True, create_graph=create_graph)[j:]
else:
row = grad[j].new_zeros(sum(x.numel() for x in inputs[i:]) - j)
out[ai, ai:].add_(row.type_as(out)) # ai's row
if ai + 1 < n:
out[ai + 1:, ai].add_(row[1:].type_as(out)) # ai's column
del row
ai += 1
del grad
return out
def main():
data_name = args.data_name
subn = args.batch_train
prior_sigma_0 = args.sigma0
prior_sigma_1 = args.sigma1
lambda_n = args.lambdan
num_hidden = args.layer
hidden_dim = args.unit
num_seed = args.num_run
num_epoch = args.nepoch
x_train, y_train, x_test, y_test = preprocess_data(data_name)
output_dim = 1
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
ntrain = x_train.shape[0]
dim = x_train.shape[1]
dim_list = np.zeros([num_seed])
BIC_list = np.zeros([num_seed])
num_selection_list = np.zeros([num_seed])
train_loss_list = np.zeros([num_seed])
test_loss_list = np.zeros([num_seed])
for my_seed in range(num_seed):
np.random.seed(my_seed)
torch.manual_seed(my_seed)
net = Net(num_hidden, hidden_dim, dim, output_dim)
net.to(device)
loss_func = nn.MSELoss()
step_lr = args.lr
momentum = args.momentum
optimization = torch.optim.SGD(net.parameters(), lr=step_lr, momentum=momentum)
sigma = torch.FloatTensor([1]).to(device)
c1 = np.log(lambda_n) - np.log(1 - lambda_n) + 0.5 * np.log(prior_sigma_0) - 0.5 * np.log(prior_sigma_1)
c2 = 0.5 / prior_sigma_0 - 0.5 / prior_sigma_1
threshold = np.sqrt(np.log((1 - lambda_n) / lambda_n * np.sqrt(prior_sigma_1 / prior_sigma_0)) / (
0.5 / prior_sigma_0 - 0.5 / prior_sigma_1))
PATH = args.base_path + args.data_name + '/' + args.model_path
if not os.path.isdir(PATH):
try:
os.makedirs(PATH)
except OSError as exc: # Python >2.5
if exc.errno == errno.EEXIST and os.path.isdir(PATH):
pass
else:
raise
para_path = []
para_gamma_path = []
for para in net.parameters():
para_path.append(np.zeros([num_epoch] + list(para.shape)))
para_gamma_path.append(np.zeros([num_epoch] + list(para.shape)))
train_loss_path = np.zeros([num_epoch])
test_loss_path = np.zeros([num_epoch])
index = np.arange(ntrain)
for epoch in range(num_epoch):
np.random.shuffle(index)
for iter in range(ntrain // subn):
subsample = index[(iter * subn):((iter + 1) * subn)]
net.zero_grad()
output = net(x_train[subsample,])
loss = loss_func(output, y_train[subsample,])
loss = loss.div(2 * sigma).add(sigma.log().mul(0.5))
loss.backward()
# prior gradient:
with torch.no_grad():
for para in net.parameters():
temp = para.pow(2).mul(c2).add(c1).exp().add(1).pow(-1)
temp = para.div(-prior_sigma_0).mul(temp) + para.div(-prior_sigma_1).mul(1 - temp)
prior_grad = temp.div(ntrain)
para.grad.data -= prior_grad
optimization.step()
print('epoch:', epoch)
with torch.no_grad():
output = net(x_train)
loss = loss_func(output, y_train)
print("train loss:", loss)
train_loss_path[epoch] = loss.cpu().data.numpy()
output = net(x_test)
loss = loss_func(output, y_test)
print("test loss:", loss)
test_loss_path[epoch] = loss.cpu().data.numpy()
for i, para in enumerate(net.parameters()):
para_path[i][epoch,] = para.cpu().data.numpy()
para_gamma_path[i][epoch,] = (para.abs() > threshold).cpu().data.numpy()
print('number of selected:', np.sum(np.max(para_gamma_path[0][epoch,], 0) > 0))
import pickle
filename = PATH + 'result' + str(my_seed)
f = open(filename, 'wb')
pickle.dump([para_path, para_gamma_path, train_loss_path, test_loss_path], f)
f.close()
num_selection_list[my_seed] = np.sum(np.max(para_gamma_path[0][-1,], 0) > 0)
temp_str = [str(int(x)) for x in np.max(para_gamma_path[0][-1,], 0) > 0]
temp_str = ' '.join(temp_str)
filename = PATH + 'selected_variable' + str(my_seed) + '.txt'
f = open(filename, 'w')
f.write(temp_str)
f.close()
with torch.no_grad():
for i, para in enumerate(net.parameters()):
para.data = torch.FloatTensor(para_path[i][-1,]).to(device)
user_mask = {}
for name, para in net.named_parameters():
user_mask[name] = para.abs() < threshold
net.set_prune(user_mask)
fine_tune_epoch = args.fine_tune_epoch
para_path_fine_tune = []
para_gamma_path_fine_tune = []
for para in net.parameters():
para_path_fine_tune.append(np.zeros([fine_tune_epoch] + list(para.shape)))
para_gamma_path_fine_tune.append(np.zeros([fine_tune_epoch] + list(para.shape)))
train_loss_path_fine_tune = np.zeros([fine_tune_epoch])
test_loss_path_fine_tune = np.zeros([fine_tune_epoch])
optimization = torch.optim.SGD(net.parameters(), lr=step_lr, momentum=momentum)
for epoch in range(fine_tune_epoch):
np.random.shuffle(index)
for iter in range(ntrain // subn):
subsample = index[(iter * subn):((iter + 1) * subn)]
net.zero_grad()
output = net(x_train[subsample,])
loss = loss_func(output, y_train[subsample,])
loss = loss.div(2 * sigma).add(sigma.log().mul(0.5))
loss.backward()
# prior gradient:
with torch.no_grad():
for para in net.parameters():
temp = para.pow(2).mul(c2).add(c1).exp().add(1).pow(-1)
temp = para.div(-prior_sigma_0).mul(temp) + para.div(-prior_sigma_1).mul(1 - temp)
prior_grad = temp.div(ntrain)
para.grad.data -= prior_grad
optimization.step()
print('fine tune epoch:', epoch)
with torch.no_grad():
output = net(x_train)
loss = loss_func(output, y_train)
print("train loss:", loss)
train_loss_path_fine_tune[epoch] = loss.cpu().data.numpy()
output = net(x_test)
loss = loss_func(output, y_test)
print("test loss:", loss)
test_loss_path_fine_tune[epoch] = loss.cpu().data.numpy()
for i, para in enumerate(net.parameters()):
para_path_fine_tune[i][epoch,] = para.cpu().data.numpy()
para_gamma_path_fine_tune[i][epoch,] = (
para.abs() > threshold).cpu().data.numpy()
print('number of selected:',
np.sum(np.max(para_gamma_path_fine_tune[0][epoch,], 0) > 0))
import pickle
filename = PATH + 'fine_tune_result' + str(my_seed)
f = open(filename, 'wb')
pickle.dump([para_path_fine_tune, para_gamma_path_fine_tune, train_loss_path_fine_tune,
test_loss_path_fine_tune], f)
f.close()
with torch.no_grad():
output = net(x_train)
loss = loss_func(output, y_train)
print("Train Loss:", loss)
train_loss_list[my_seed] = loss.cpu().data.numpy()
num_non_zero_element = 0
for name, para in net.named_parameters():
num_non_zero_element = num_non_zero_element + para.numel() - net.mask[name].sum()
BIC = (2 * ntrain * loss + np.log(ntrain) *num_non_zero_element).item()
BIC_list[my_seed] = BIC
dim_list[my_seed] = num_non_zero_element
output = net(x_test)
loss = loss_func(output, y_test)
print("Test Loss:", loss)
test_loss_list[my_seed] = loss.cpu().data.numpy()
print("number of non-zero connections:", num_non_zero_element)
print('BIC:', BIC)
import pickle
filename = PATH + 'Overall_result_with_different_initialization'
f = open(filename, 'wb')
pickle.dump([BIC_list, dim_list, num_selection_list, train_loss_list, test_loss_list], f)
f.close()
if __name__ == '__main__':
main()