-
Notifications
You must be signed in to change notification settings - Fork 0
/
BayesByBackprop.py
297 lines (224 loc) · 8.14 KB
/
BayesByBackprop.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
import logging
import collections
import mxnet as mx
import numpy as np
from mxnet import nd, autograd
log = logging.getLogger(__name__)
class BayesByBackprop:
def __init__(self, seed, num_hidden_layers, num_hidden_units, batch_size, epochs, learning_rate, sigma_p):
log.debug("BayesByBackprop instance created with seed = {}".format(seed))
self.config = {
"num_hidden_layers": num_hidden_layers,
"num_hidden_units": num_hidden_units,
"batch_size": batch_size,
"epochs": epochs,
"learning_rate": learning_rate,
"sigma_p": sigma_p,
}
self.ctx = mx.cpu()
mx.random.seed(seed)
np.random.seed(seed)
self.num_inputs = None
self.num_outputs = None
self.layer_param_shapes = []
self.mus = []
self.rhos = []
#######################################
### Model definition ###
#######################################
def define_model(self, num_inputs, num_outputs):
"""
Define BBB model with a single input X and its label y
Parameters
----------
num_inputs : int
Number of nodes in input layer
num_outputs : int
Number of nodes in output layer
"""
self.num_inputs = num_inputs
self.num_outputs = num_outputs
num_hidden = self.config['num_hidden_units']
num_layers = self.config['num_hidden_layers']
for i in range(num_layers + 1):
if i == 0:
W_shape = (num_inputs, num_hidden)
b_shape = (num_hidden,)
elif i == num_layers:
W_shape = (num_hidden, num_outputs)
b_shape = (num_outputs,)
else:
W_shape = (num_hidden, num_hidden)
b_shape = (num_hidden, )
self.layer_param_shapes.extend([W_shape, b_shape])
def net(X, layer_params):
"""
Given weights and input, compute output from NN
Parameters
----------
X : mxnet.ndarray
An input vector
layer_params : python list of mxnet.ndarray
Weights for each layer
"""
def relu(X):
"""
Activation function
Parameters
----------
X : mxnet.ndarray
An input vector
"""
return nd.maximum(X, nd.zeros_like(X))
layer_input = X
for i in range(len(layer_params) // 2 - 2):
h_linear = nd.dot(layer_input, layer_params[2*i]) + layer_params[2*i+1]
layer_input = relu(h_linear)
output = nd.dot(layer_input, layer_params[-2]) + layer_params[-1] # Last layer without ReLU
return output
def predict(self, X):
"""
Given input, predict output using this model (mus)
Parameters
----------
X : mxnet.ndarray
An input vector
"""
X = X.as_in_context(self.ctx).reshape((-1, self.num_inputs))
output = BayesByBackprop.net(X, self.mus)
predictions = nd.argmax(output, axis=1)
return predictions
#######################################
### Cost Function ###
#######################################
def combined_loss(self, num_batches, params, sigmas, output, label_one_hot):
"""
Total cost across batches
"""
def log_softmax_likelihood(yhat_linear, y):
"""
Likelihood of output yhat_linear, given the label y
yhat_linear, y: ndarray
"""
return nd.nansum(y * nd.log_softmax(yhat_linear), axis=0, exclude=True)
def log_gaussian(x, mu, sigma):
"""
Log of Gaussian function log N(x|mu,sigma)
x, mu, sigma: ndarray
"""
# x, mu, sigma are of size #node_i*#node_i+1 for i-th layer
return -0.5*np.log(2.0*np.pi) - nd.log(sigma) - 0.5 * (x-mu)**2 / (sigma ** 2)
def gaussian_prior(x, sigma_prior):
"""
Gaussian Prior
x: ndarray (weights of one layer)
"""
return nd.sum(log_gaussian(x, 0., sigma_prior))
sigma_p = nd.array([self.config['sigma_p']], ctx=self.ctx)
log_likelihood_sum = nd.sum(log_softmax_likelihood(output, label_one_hot))
log_prior_sum = sum([nd.sum(gaussian_prior(param, sigma_p)) for param in params])
log_var_posterior_sum = sum([nd.sum(log_gaussian(params[i], self.mus[i], sigmas[i])) for i in range(len(params))])
return 1.0 / num_batches * (log_var_posterior_sum - log_prior_sum) - log_likelihood_sum
######################################
### Evaluation ###
######################################
def evaluate_accuracy(self, data_iterator, net, layer_params):
numerator = 0.
denominator = 0.
for i, (data, label) in enumerate(data_iterator):
if i == 1000:
break
data = data.as_in_context(self.ctx).reshape((-1, 784))
label = label.as_in_context(self.ctx)
predictions = self.predict(data)
numerator += nd.sum(predictions == label)
denominator += data.shape[0]
return (numerator / denominator).asscalar()
######################################
### Training ###
######################################
def train(self, train_dataset, test_dataset):
"""
train_dataset: mxnet.gluon.data.dataset
e.g. mx.gluon.data.vision.MNIST(train=True, transform=transform)
test_dataset: mxnet.gluon.data.dataset
e.g. mx.gluon.data.vision.MNIST(train=False, transform=transform)
"""
batch_size = self.config['batch_size']
train_data = mx.gluon.data.DataLoader(train_dataset, batch_size, shuffle=True)
test_data = mx.gluon.data.DataLoader(test_dataset, batch_size, shuffle=False)
num_train = sum([batch_size for i in train_data])
num_batches = num_train / batch_size
# 1. Init params
weight_scale = .1
rho_offset = -3
mus = []
rhos = []
for shape in self.layer_param_shapes:
mu = nd.random_normal(shape=shape, ctx=self.ctx, scale=weight_scale)
rho = rho_offset + nd.zeros(shape=shape, ctx=self.ctx)
self.mus.append(mu)
self.rhos.append(rho)
variational_params = self.mus + self.rhos
for param in variational_params:
param.attach_grad()
# 2. Functions for main training loop
def sample_epsilons(param_shapes):
epsilons = [nd.random_normal(shape=shape, loc=0., scale=1.0, ctx=self.ctx) for shape in param_shapes]
return epsilons
def transform_rhos(rhos):
""" Apply softmax on rhos to get sigmas """
return [nd.log(1. + nd.exp(rho)) for rho in rhos]
def transform_gaussian_samples(mus, sigmas, epsilons):
""" w = mu + sigma o epsilons"""
samples = []
for j in range(len(mus)):
samples.append(mus[j] + sigmas[j] * epsilons[j])
return samples
def SGD(params, lr):
"""
Stochastic Gradient Descent
"""
for param in params:
param[:] = param - lr * param.grad
# 3. Complete training loop
epochs = self.config['epochs']
learning_rate = self.config['learning_rate']
smoothing_constant = .01
train_acc = []
test_acc = []
for e in range(epochs):
for i, (data, label) in enumerate(train_data):
if i == 1000:
break
data = data.as_in_context(self.ctx).reshape((-1, self.num_inputs))
label = label.as_in_context(self.ctx)
label_one_hot = nd.one_hot(label, 10)
with autograd.record():
# sample epsilons from standard normal
epsilons = sample_epsilons(self.layer_param_shapes)
# compute softplus for variance
sigmas = transform_rhos(self.rhos)
# obtain a sample from q(w|theta) by transforming the epsilons
# layer_params = transform_gaussian_samples(mus, sigmas, epsilons)
layer_params = transform_gaussian_samples(self.mus, sigmas, epsilons)
# forward-propagate the batch
output = BayesByBackprop.net(data, layer_params)
# calculate the loss
loss = self.combined_loss(num_batches, layer_params, sigmas, output, label_one_hot)
# backpropagate for gradient calculation
loss.backward()
# apply stochastic gradient descent to variational parameters
SGD(variational_params, learning_rate)
# calculate moving loss for monitoring convergence
curr_loss = nd.mean(loss).asscalar()
moving_loss = (curr_loss if ((i == 0) and (e == 0))
else (1 - smoothing_constant) * moving_loss + (smoothing_constant) * curr_loss)
test_accuracy = self.evaluate_accuracy(test_data, BayesByBackprop.net, self.mus)
train_accuracy = self.evaluate_accuracy(train_data, BayesByBackprop.net, self.mus)
train_acc.append(np.asscalar(train_accuracy))
test_acc.append(np.asscalar(test_accuracy))
print("Epoch %s. Loss: %s, Train_acc %s, Test_acc %s" %
(e, moving_loss, train_accuracy, test_accuracy))
log.info("Epoch %s. Loss: %s, Train_acc %s, Test_acc %s" %
(e, moving_loss, train_accuracy, test_accuracy))