-
Notifications
You must be signed in to change notification settings - Fork 108
/
binary_connect.py
279 lines (196 loc) · 9.56 KB
/
binary_connect.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
# Copyright 2015 Matthieu Courbariaux
# This file is part of BinaryConnect.
# BinaryConnect is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# BinaryConnect is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with BinaryConnect. If not, see <http://www.gnu.org/licenses/>.
import time
from collections import OrderedDict
import numpy as np
# specifying the gpu to use
# import theano.sandbox.cuda
# theano.sandbox.cuda.use('gpu1')
import theano
import theano.tensor as T
import lasagne
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
def hard_sigmoid(x):
return T.clip((x+1.)/2.,0,1)
# The binarization function
def binarization(W,H,binary=True,deterministic=False,stochastic=False,srng=None):
# (deterministic == True) <-> test-time <-> inference-time
if not binary or (deterministic and stochastic):
# print("not binary")
Wb = W
else:
# [-1,1] -> [0,1]
Wb = hard_sigmoid(W/H)
# Stochastic BinaryConnect
if stochastic:
# print("stoch")
Wb = T.cast(srng.binomial(n=1, p=Wb, size=T.shape(Wb)), theano.config.floatX)
# Deterministic BinaryConnect (round to nearest)
else:
# print("det")
Wb = T.round(Wb)
# 0 or 1 -> -1 or 1
Wb = T.cast(T.switch(Wb,H,-H), theano.config.floatX)
return Wb
# This class extends the Lasagne DenseLayer to support BinaryConnect
class DenseLayer(lasagne.layers.DenseLayer):
def __init__(self, incoming, num_units,
binary = True, stochastic = True, H=1.,W_LR_scale="Glorot", **kwargs):
self.binary = binary
self.stochastic = stochastic
self.H = H
if H == "Glorot":
num_inputs = int(np.prod(incoming.output_shape[1:]))
self.H = np.float32(np.sqrt(1.5/ (num_inputs + num_units)))
# print("H = "+str(self.H))
self.W_LR_scale = W_LR_scale
if W_LR_scale == "Glorot":
num_inputs = int(np.prod(incoming.output_shape[1:]))
self.W_LR_scale = np.float32(1./np.sqrt(1.5/ (num_inputs + num_units)))
self._srng = RandomStreams(lasagne.random.get_rng().randint(1, 2147462579))
if self.binary:
super(DenseLayer, self).__init__(incoming, num_units, W=lasagne.init.Uniform((-self.H,self.H)), **kwargs)
# add the binary tag to weights
self.params[self.W]=set(['binary'])
else:
super(DenseLayer, self).__init__(incoming, num_units, **kwargs)
def get_output_for(self, input, deterministic=False, **kwargs):
self.Wb = binarization(self.W,self.H,self.binary,deterministic,self.stochastic,self._srng)
Wr = self.W
self.W = self.Wb
rvalue = super(DenseLayer, self).get_output_for(input, **kwargs)
self.W = Wr
return rvalue
# This class extends the Lasagne Conv2DLayer to support BinaryConnect
class Conv2DLayer(lasagne.layers.Conv2DLayer):
def __init__(self, incoming, num_filters, filter_size,
binary = True, stochastic = True, H=1.,W_LR_scale="Glorot", **kwargs):
self.binary = binary
self.stochastic = stochastic
self.H = H
if H == "Glorot":
num_inputs = int(np.prod(filter_size)*incoming.output_shape[1])
num_units = int(np.prod(filter_size)*num_filters) # theoretically, I should divide num_units by the pool_shape
self.H = np.float32(np.sqrt(1.5 / (num_inputs + num_units)))
# print("H = "+str(self.H))
self.W_LR_scale = W_LR_scale
if W_LR_scale == "Glorot":
num_inputs = int(np.prod(filter_size)*incoming.output_shape[1])
num_units = int(np.prod(filter_size)*num_filters) # theoretically, I should divide num_units by the pool_shape
self.W_LR_scale = np.float32(1./np.sqrt(1.5 / (num_inputs + num_units)))
# print("W_LR_scale = "+str(self.W_LR_scale))
self._srng = RandomStreams(lasagne.random.get_rng().randint(1, 2147462579))
if self.binary:
super(Conv2DLayer, self).__init__(incoming, num_filters, filter_size, W=lasagne.init.Uniform((-self.H,self.H)), **kwargs)
# add the binary tag to weights
self.params[self.W]=set(['binary'])
else:
super(Conv2DLayer, self).__init__(incoming, num_filters, filter_size, **kwargs)
def convolve(self, input, deterministic=False, **kwargs):
self.Wb = binarization(self.W,self.H,self.binary,deterministic,self.stochastic,self._srng)
Wr = self.W
self.W = self.Wb
rvalue = super(Conv2DLayer, self).convolve(input, **kwargs)
self.W = Wr
return rvalue
# This function computes the gradient of the binary weights
def compute_grads(loss,network):
layers = lasagne.layers.get_all_layers(network)
grads = []
for layer in layers:
params = layer.get_params(binary=True)
if params:
# print(params[0].name)
grads.append(theano.grad(loss, wrt=layer.Wb))
return grads
# This functions clips the weights after the parameter update
def clipping_scaling(updates,network):
layers = lasagne.layers.get_all_layers(network)
updates = OrderedDict(updates)
for layer in layers:
params = layer.get_params(binary=True)
for param in params:
print("W_LR_scale = "+str(layer.W_LR_scale))
print("H = "+str(layer.H))
updates[param] = param + layer.W_LR_scale*(updates[param] - param)
updates[param] = T.clip(updates[param], -layer.H,layer.H)
return updates
# Given a dataset and a model, this function trains the model on the dataset for several epochs
# (There is no default train function in Lasagne yet)
def train(train_fn,val_fn,
batch_size,
LR_start,LR_decay,
num_epochs,
X_train,y_train,
X_val,y_val,
X_test,y_test):
# A function which shuffles a dataset
def shuffle(X,y):
shuffled_range = range(len(X))
np.random.shuffle(shuffled_range)
# print(shuffled_range[0:10])
new_X = np.copy(X)
new_y = np.copy(y)
for i in range(len(X)):
new_X[i] = X[shuffled_range[i]]
new_y[i] = y[shuffled_range[i]]
return new_X,new_y
# This function trains the model a full epoch (on the whole dataset)
def train_epoch(X,y,LR):
loss = 0
batches = len(X)/batch_size
for i in range(batches):
loss += train_fn(X[i*batch_size:(i+1)*batch_size],y[i*batch_size:(i+1)*batch_size],LR)
loss/=batches
return loss
# This function tests the model a full epoch (on the whole dataset)
def val_epoch(X,y):
err = 0
loss = 0
batches = len(X)/batch_size
for i in range(batches):
new_loss, new_err = val_fn(X[i*batch_size:(i+1)*batch_size], y[i*batch_size:(i+1)*batch_size])
err += new_err
loss += new_loss
err = err / batches * 100
loss /= batches
return err, loss
# shuffle the train set
X_train,y_train = shuffle(X_train,y_train)
best_val_err = 100
best_epoch = 1
LR = LR_start
# We iterate over epochs:
for epoch in range(num_epochs):
start_time = time.time()
train_loss = train_epoch(X_train,y_train,LR)
X_train,y_train = shuffle(X_train,y_train)
val_err, val_loss = val_epoch(X_val,y_val)
# test if validation error went down
if val_err <= best_val_err:
best_val_err = val_err
best_epoch = epoch+1
test_err, test_loss = val_epoch(X_test,y_test)
epoch_duration = time.time() - start_time
# Then we print the results for this epoch:
print("Epoch "+str(epoch + 1)+" of "+str(num_epochs)+" took "+str(epoch_duration)+"s")
print(" LR: "+str(LR))
print(" training loss: "+str(train_loss))
print(" validation loss: "+str(val_loss))
print(" validation error rate: "+str(val_err)+"%")
print(" best epoch: "+str(best_epoch))
print(" best validation error rate: "+str(best_val_err)+"%")
print(" test loss: "+str(test_loss))
print(" test error rate: "+str(test_err)+"%")
# decay the LR
LR *= LR_decay