forked from CW-Huang/BayesianHypernet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBHNs.py
475 lines (378 loc) · 17.3 KB
/
BHNs.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
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
# -*- coding: utf-8 -*-
"""
Created on Sun May 14 17:58:58 2017
@author: Chin-Wei
"""
from modules import LinearFlowLayer, IndexLayer, PermuteLayer, SplitLayer
from modules import CoupledDenseLayer, stochasticDenseLayer2, \
stochasticConv2DLayer
from utils import log_normal
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
RSSV = T.shared_randomstreams.RandomStateSharedVariable
floatX = theano.config.floatX
import lasagne
from lasagne import nonlinearities
rectify = nonlinearities.rectify
softmax = nonlinearities.softmax
from lasagne.layers import get_output
from lasagne.objectives import categorical_crossentropy as cc
import numpy as np
class Base_BHN(object):
max_norm = 10
clip_grad = 5
def __init__(self,
lbda=1.,
perdatapoint=False,
srng = RandomStreams(seed=427),
prior = log_normal):
self.lbda = lbda
self.perdatapoint = perdatapoint
self.srng = srng
self.prior = prior
self._get_theano_variables()
if perdatapoint:
self.wd1 = self.input_var.shape[0]
else:
self.wd1 = 1
print('\tbuilding hyper net')
self._get_hyper_net()
print('\tbuilding primary net')
self._get_primary_net()
print('\tgetting params')
self._get_params()
print('\tgetting elbo')
self._get_elbo()
print('\tgetting grads')
self._get_grads()
print('\tgetting train funcs')
self._get_train_func()
print('\tgetting useful funcs')
self._get_useful_funcs()
def _get_theano_variables(self):
self.input_var = T.matrix('input_var')
self.target_var = T.matrix('target_var')
self.dataset_size = T.scalar('dataset_size')
self.learning_rate = T.scalar('learning_rate')
def _get_hyper_net(self):
"""
hypernet outputing weight parameters of the primary net.
structure to be specified.
DEFINE h_net, weights, logdets
"""
raise NotImplementedError("BaseBayesianHypernet does not implement"
"the _get_hyper_net() method")
def _get_primary_net(self):
"""
main structure of the predictive network (to be specified).
DEFINE p_net, y
"""
raise NotImplementedError("BaseBayesianHypernet does not implement"
"the _get_primary_net() method")
def _get_params(self):
params = lasagne.layers.get_all_params([self.h_net,self.p_net])
self.params = list()
for param in params:
if type(param) is not RSSV:
self.params.append(param)
def _get_elbo(self):
"""
negative elbo, an upper bound on NLL
"""
logdets = self.logdets
logqw = - logdets
"""
originally...
logqw = - (0.5*(ep**2).sum(1)+0.5*T.log(2*np.pi)*num_params+logdets)
--> constants are neglected in this wrapperfrom utils import log_laplace
"""
logpw = self.prior(self.weights,0.,-T.log(self.lbda)).sum(1)
"""
using normal prior centered at zero, with lbda being the inverse
of the variance
"""
kl = (logqw - logpw).mean()
logpyx = - cc(self.y,self.target_var).mean()
self.loss = - (logpyx - kl/T.cast(self.dataset_size,floatX))
def _get_grads(self):
grads = T.grad(self.loss, self.params)
mgrads = lasagne.updates.total_norm_constraint(grads,
max_norm=self.max_norm)
cgrads = [T.clip(g, -self.clip_grad, self.clip_grad) for g in mgrads]
self.updates = lasagne.updates.adam(cgrads, self.params,
learning_rate=self.learning_rate)
def _get_train_func(self):
train = theano.function([self.input_var,
self.target_var,
self.dataset_size,
self.learning_rate],
self.loss,updates=self.updates)
self.train_func = train
def _get_useful_funcs(self):
pass
class MLPWeightNorm_BHN(Base_BHN):
"""
Hypernet with dense coupling layer outputing posterior of rescaling
parameters of weightnorm MLP
"""
# 784 -> 20 -> 10
weight_shapes = [(784, 200),
(200, 10)]
num_params = sum(ws[1] for ws in weight_shapes)
def __init__(self,
lbda=1,
perdatapoint=False,
srng = RandomStreams(seed=427),
prior = log_normal,
coupling=True):
self.coupling = coupling
super(MLPWeightNorm_BHN, self).__init__(lbda=lbda,
perdatapoint=perdatapoint,
srng=srng,
prior=prior)
def _get_hyper_net(self):
# inition random noise
ep = self.srng.normal(size=(self.wd1,
self.num_params),dtype=floatX)
logdets_layers = []
h_net = lasagne.layers.InputLayer([None,self.num_params])
# mean and variation of the initial noise
layer_temp = LinearFlowLayer(h_net)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
if self.coupling:
# add more to introduce more correlation if needed
layer_temp = CoupledDenseLayer(h_net,200)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
h_net = PermuteLayer(h_net,self.num_params)
layer_temp = CoupledDenseLayer(h_net,200)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
self.h_net = h_net
self.weights = lasagne.layers.get_output(h_net,ep)
self.logdets = sum([get_output(ld,ep) for ld in logdets_layers])
def _get_primary_net(self):
t = np.cast['int32'](0)
p_net = lasagne.layers.InputLayer([None,784])
inputs = {p_net:self.input_var}
for ws in self.weight_shapes:
# using weightnorm reparameterization
# only need ws[1] parameters (for rescaling of the weight matrix)
num_param = ws[1]
w_layer = lasagne.layers.InputLayer((None,ws[1]))
weight = self.weights[:,t:t+num_param].reshape((self.wd1,ws[1]))
inputs[w_layer] = weight
p_net = stochasticDenseLayer2([p_net,w_layer],ws[1])
print p_net.output_shape
t += num_param
p_net.nonlinearity = nonlinearities.softmax # replace the nonlinearity
# of the last layer
# with softmax for
# classification
y = T.clip(get_output(p_net,inputs), 0.001, 0.999) # stability
self.p_net = p_net
self.y = y
def _get_useful_funcs(self):
self.predict_proba = theano.function([self.input_var],self.y)
self.predict = theano.function([self.input_var],self.y.argmax(1))
class Conv2D_BHN(Base_BHN):
"""
Hypernet with dense coupling layer outputing posterior of weight
parameters of filters for Conv2D layer.
The last layer is fully connected with weightnorm reparameterization
as in `MLPWeightNorm_BHN`.
"""
weight_shapes = [(16,1,5,5), # -> (None, 16, 14, 14)
(16,16,5,5), # -> (None, 16, 7, 7)
(16,16,5,5)] # -> (None, 16, 4, 4)
# [num_filters, filter_size, stride, pad, nonlinearity]
# needs to be consistent with weight_shapes
args = [[16,5,2,'same',rectify],
[16,5,2,'same',rectify],
[16,5,2,'same',rectify]]
num_classes = 10
num_params = sum(np.prod(ws) for ws in weight_shapes) + num_classes
# 10 classes
# need to be
# specified in
# _get_primary_net
def __init__(self,
lbda=1,
perdatapoint=False,
srng = RandomStreams(seed=427),
prior = log_normal,
coupling=True):
self.coupling = coupling
super(Conv2D_BHN, self).__init__(lbda=lbda,
perdatapoint=perdatapoint,
srng=srng,
prior=prior)
def _get_theano_variables(self):
# redefine a 4-d tensor for convnet
self.input_var = T.tensor4('input_var')
self.target_var = T.matrix('target_var')
self.dataset_size = T.scalar('dataset_size')
self.learning_rate = T.scalar('learning_rate')
def _get_hyper_net(self):
# inition random noise
ep = self.srng.normal(size=(self.wd1,
self.num_params),dtype=floatX)
logdets_layers = []
h_net = lasagne.layers.InputLayer([None,self.num_params])
# mean and variation of the initial noise
layer_temp = LinearFlowLayer(h_net)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
if self.coupling:
# add more to introduce more correlation if needed
layer_temp = CoupledDenseLayer(h_net,200)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
h_net = PermuteLayer(h_net,self.num_params)
layer_temp = CoupledDenseLayer(h_net,200)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
self.h_net = h_net
self.weights = lasagne.layers.get_output(h_net,ep)
self.logdets = sum([get_output(ld,ep) for ld in logdets_layers])
def _get_primary_net(self):
nc = self.num_classes
t = np.cast['int32'](0)
p_net = lasagne.layers.InputLayer([None,1,28,28])
inputs = {p_net:self.input_var}
for ws, args in zip(self.weight_shapes,self.args):
num_param = np.prod(ws)
weight = self.weights[:,t:t+num_param].reshape(ws)
num_filters = args[0]
filter_size = args[1]
stride = args[2]
pad = args[3]
nonl = args[4]
p_net = stochasticConv2DLayer([p_net,weight],
num_filters,filter_size,stride,pad,
nonlinearity=nonl)
print p_net.output_shape
t += num_param
w_layer = lasagne.layers.InputLayer((None,nc))
weight = self.weights[:,t:t+nc].reshape((self.wd1,nc))
inputs[w_layer] = weight
p_net = stochasticDenseLayer2([p_net,w_layer],nc,
nonlinearity=nonlinearities.softmax)
y = T.clip(get_output(p_net,inputs), 0.001, 0.999) # stability
self.p_net = p_net
self.y = y
def _get_useful_funcs(self):
self.predict_proba = theano.function([self.input_var],self.y)
self.predict = theano.function([self.input_var],self.y.argmax(1))
class Conv2D_shared_BHN(Base_BHN):
"""
Hypernet with dense coupling layer outputing posterior of weight
parameters of filters for Conv2D layer.
The last layer is fully connected with weightnorm reparameterization
as in `MLPWeightNorm_BHN`.
"""
weight_shapes = [(16,1,5,5), # -> (None, 16, 14, 14)
(16,16,5,5), # -> (None, 16, 7, 7)
(16,16,5,5)] # -> (None, 16, 4, 4)
n_kernels = np.array(weight_shapes)[:,1].sum()
kernel_shape = weight_shapes[0][:1]+weight_shapes[0][2:]
# make sure it's the same
# across kernels
# [num_filters, filter_size, stride, pad, nonlinearity]
# needs to be consistent with weight_shapes
args = [[16,5,2,'same',rectify],
[16,5,2,'same',rectify],
[16,5,2,'same',rectify]]
num_classes = 10
num_params = sum(np.prod(ws) for ws in weight_shapes) + num_classes
# 10 classes
# need to be
# specified in
# _get_primary_net
def __init__(self,
lbda=1,
perdatapoint=False,
srng = RandomStreams(seed=427),
prior = log_normal,
coupling=True):
self.coupling = coupling
super(Conv2D_shared_BHN, self).__init__(lbda=lbda,
perdatapoint=perdatapoint,
srng=srng,
prior=prior)
def _get_theano_variables(self):
# redefine a 4-d tensor for convnet
self.input_var = T.tensor4('input_var')
self.target_var = T.matrix('target_var')
self.dataset_size = T.scalar('dataset_size')
self.learning_rate = T.scalar('learning_rate')
def _get_hyper_net(self):
# inition random noise
ep = self.srng.normal(size=(1,
self.num_params),dtype=floatX)
logdets_layers = []
h_net = lasagne.layers.InputLayer([1,self.num_params])
# mean and variation of the initial noise
layer_temp = LinearFlowLayer(h_net)
h_net = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
h_net = SplitLayer(h_net,self.num_params-self.num_classes,1)
h_net1 = IndexLayer(h_net,0,(1,self.num_params-self.num_classes))
h_net2 = IndexLayer(h_net,1)
h_net1 = lasagne.layers.ReshapeLayer(h_net1,
(self.n_kernels,) + \
(np.prod(self.kernel_shape),))
if self.coupling:
# add more to introduce more correlation if needed
layer_temp = CoupledDenseLayer(h_net1,100)
h_net1 = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
h_net1 = PermuteLayer(h_net1,self.num_params)
layer_temp = CoupledDenseLayer(h_net1,100)
h_net1 = IndexLayer(layer_temp,0)
logdets_layers.append(IndexLayer(layer_temp,1))
self.kernel_weights = lasagne.layers.get_output(h_net1,ep)
h_net1 = lasagne.layers.ReshapeLayer(h_net1,
(1, self.n_kernels * \
np.prod(self.kernel_shape) ) )
h_net = lasagne.layers.ConcatLayer([h_net1,h_net2],1)
self.h_net = h_net
self.weights = lasagne.layers.get_output(h_net,ep)
self.logdets = sum([get_output(ld,ep) for ld in logdets_layers])
def _get_primary_net(self):
nc = self.num_classes
t = np.cast['int32'](0)
k = np.cast['int32'](0)
p_net = lasagne.layers.InputLayer([None,1,28,28])
inputs = {p_net:self.input_var}
for ws, args in zip(self.weight_shapes,self.args):
num_param = np.prod(ws)
num_kernel = ws[1]
weight = self.kernel_weights[k:k+num_kernel,:]
weight = weight.dimshuffle(1,0).reshape(self.kernel_shape + \
(num_kernel,))
weight = weight.dimshuffle(0,3,1,2)
num_filters = args[0]
filter_size = args[1]
stride = args[2]
pad = args[3]
nonl = args[4]
p_net = stochasticConv2DLayer([p_net,weight],
num_filters,filter_size,stride,pad,
nonlinearity=nonl)
print p_net.output_shape
t += num_param
k += num_kernel
w_layer = lasagne.layers.InputLayer((None,nc))
weight = self.weights[:,t:t+nc].reshape((self.wd1,nc))
inputs[w_layer] = weight
p_net = stochasticDenseLayer2([p_net,w_layer],nc,
nonlinearity=nonlinearities.softmax)
y = T.clip(get_output(p_net,inputs), 0.001, 0.999) # stability
self.p_net = p_net
self.y = y
def _get_useful_funcs(self):
self.predict_proba = theano.function([self.input_var],self.y)
self.predict = theano.function([self.input_var],self.y.argmax(1))