-
Notifications
You must be signed in to change notification settings - Fork 1
/
nn.py
356 lines (264 loc) · 8.79 KB
/
nn.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
import sys
import os
import numpy as np
from dataloader import *
class NN(object):
def __init__(self,hidden_dims=(1024,2048),n_hidden=2,mode='train',dpath=None,model_path=None):
if dpath is not None:
try:
self.mnist_data = load_data(dpath,mode=mode)
self.n_samples,self.ip_dim = self.mnist_data[:,1:].shape
except:
raise Exception('Not able to parse MNIST Data')
self.ncls = 10
self.mode = mode
self.batch_size = 128
self.counter = 0
self.nepochs = 10
self.maxiters = 1000000
self.lr = 1e-3
self.eps = 1e-8
self.save_model_path = model_path
self.n_hidden = n_hidden
self.weights={}
self.biases={}
self.batch_norm_params={}
self.bn_cache={}
self.save_iter = 1
if mode == 'train':
print "Initializing Weights......"
self.init_weights(n_hidden,hidden_dims)
else:
print "Loading Trained Weights......"
self.load_weight(model_path)
def load_weight(self,path):
'''
Loads the pretrained model
path = Path of the model
'''
if path is None:
raise Exception('Model Load Path Invalid')
try:
self.weights = np.load(os.path.join(path,'weights.npy')).item()
except:
raise Exception('Not able to load Network weights')
try:
self.biases = np.load(os.path.join(path,'biases.npy')).item()
except:
raise Exception('Not able to load Network biases')
try:
self.batch_norm_params = np.load(os.path.join(path,'bn_params.npy')).item()
except:
raise Exception('Not able to load Network weights')
def init_weights(self,n_hidden,dims):
'''
Initializes the weights of the network
n_hidden = Number of hidden layers
dims = Number of Hidden Dimensions
'''
self.weights[0]=np.random.randn(self.ip_dim,dims[0]) * (1.0/np.sqrt(self.ip_dim))
self.biases[0] = np.zeros((dims[0],))
for i in range(1,n_hidden):
self.weights[i] = np.random.randn(dims[i-1],dims[i])*(1.0/np.sqrt(dims[i-1]))
self.biases[i] = np.zeros((dims[i],))
self.weights[-1] = np.random.randn(dims[n_hidden-1],self.ncls)*(1.0/np.sqrt(dims[n_hidden-1]))
self.biases[-1] = np.zeros((self.ncls,))
for i in range(n_hidden):
self.batch_norm_params[i] = {'gamma':np.ones((dims[i],)),\
'beta':np.zeros((dims[i],)), \
'running_mean':np.zeros((dims[i],)),\
'running_var':np.zeros((dims[i],)),}
self.bn_cache[i]=[]
def get_data(self):
'''
Iterate over the training data for batch generation
'''
if self.counter > self.n_samples:
self.counter = 0
if(self.counter == 0):
np.random.shuffle(self.mnist_data)
return self.mnist_data[self.counter:self.counter+self.batch_size,:]
def forward(self,ip,labels=None):
'''
Forward Pass
ip = Input image tensor
labels = If None, it will not compute loss
'''
h1 = ip.dot(self.weights[0]) + self.biases[0]
h1_bn = self.batchnorm_forward(h1,0,mode=self.mode)
h1_relu = self.relu(h1_bn.copy())
h2 = h1_relu.dot(self.weights[1]) + self.biases[1]
h2_bn = self.batchnorm_forward(h2,1,mode=self.mode)
h2_relu = self.relu(h2_bn.copy())
logits = h2_relu.dot(self.weights[-1]) + self.biases[-1]
probs = self.softmax(logits)
if labels is not None:
loss = self.loss(labels,probs)
else:
loss = None
return (h1,h1_bn,h1_relu,h2,h2_bn,h2_relu,logits,probs,loss)
def loss(self,y,pred):
'''
Cross Entropy Loss
'''
return -np.sum(y*np.log(pred))/(y.shape[0]+self.eps)
def softmax(self,ip):
'''
Computes Softmax of the Logits
'''
centered_ip = ip - np.max(ip,axis=1).reshape(-1,1)
probs = np.exp(centered_ip)
return probs/np.sum(probs,axis=1).reshape(-1,1).astype(np.float32)
def relu(self,ip):
'''
RelU Activation
'''
x = ip
x[np.where(x < 0)] = 0
return x
def batchnorm_forward(self,ip,index,mode='train'):
'''
Perform batch normalization over ip (Nx D)
'''
gamma = self.batch_norm_params[index]['gamma']
beta = self.batch_norm_params[index]['beta']
running_mean = self.batch_norm_params[index]['running_mean']
running_var = self.batch_norm_params[index]['running_var']
N,D = ip.shape
if mode == 'train':
x = ip
mu = np.mean(x,axis=0) # D,
xmu = x - mu
delta = (x-mu)**2
var = (np.mean(delta,axis=0))
sqrtvar = np.sqrt(var+self.eps)
invvar = 1/sqrtvar
x_hat = xmu*invvar
x_hat_gamma = x_hat*gamma
out = x_hat_gamma + beta
running_mean = 0.9*running_mean + 0.1*mu
running_var = 0.9*running_var + 0.1*var
self.batch_norm_params[index]['running_mean'] = running_mean
self.batch_norm_params[index]['running_var'] = running_var
self.bn_cache[index] = [x,mu,xmu,delta,var,sqrtvar,invvar,x_hat,x_hat_gamma]
else:
x = ip
xmu = x - running_mean
x_hat = xmu/np.sqrt(running_var+self.eps)
out = x_hat*gamma + beta
return out
def batchnorm_backward(self,din,index):
x,mu,xmu,delta,var,sqrtvar,invvar,x_hat,x_hat_gamma = self.bn_cache[index]
gamma = self.batch_norm_params[index]['gamma']
beta = self.batch_norm_params[index]['beta']
N,D = din.shape
dbeta = np.sum(din,axis=0)
dgamma = np.sum(din*x_hat,axis=0)
dx_1 = gamma*invvar*din # del
dx_2 = -gamma*invvar*np.sum(din,axis=0)/N
dx_3 = -gamma*(xmu)*(var+self.eps)**(-1.5)*(np.sum(din*(xmu),axis=0))/float(N)
dx = dx_1+dx_2+dx_3
return dx,dbeta,dgamma
def backward(self,cache,ip,labels):
'''
Backward Pass
'''
h1,h1_bn,h1_relu, h2,h2_bn,h2_relu, logits, probs,__ = cache
dy = (labels - probs)
dW2 = h2_relu.T.dot(dy)
db2 = np.sum(dy,axis=0)
dh2 = dy.dot(self.weights[-1].T)
dh2[np.where(h2 == 0)] = 0
dh2, dbeta2,dgamma2 = self.batchnorm_backward(dh2,1)
dW1 = h1_relu.T.dot(dh2)
db1 = np.sum(dh2,axis=0)
dh1 = dh2.dot(self.weights[1].T)
dh1[np.where(h1 == 0)] = 0
dh1, dbeta1,dgamma1 = self.batchnorm_backward(dh1,0)
dW0 = ip.T.dot(dh1)
db0 = np.sum(dh1,axis=0)
return (dW0,db0,dW1,db1,dW2,db2,dbeta2,dgamma2,dbeta1,dgamma1)
def update(self,grads):
'''
Weight Update
'''
dW0,db0,dW1,db1,dW2,db2,dbeta2,dgamma2,dbeta1,dgamma1 = grads
self.weights[0] += self.lr*(dW0)
self.biases[0] += self.lr*(db0)
self.weights[1] += self.lr*(dW1)
self.biases[1] += self.lr*(db1)
self.weights[-1] += self.lr*(dW2)
self.biases[-1] += self.lr*(db2)
self.batch_norm_params[0]['gamma'] += self.lr*dgamma1
self.batch_norm_params[1]['gamma'] += self.lr*dgamma2
self.batch_norm_params[0]['beta'] += self.lr*dbeta1
self.batch_norm_params[1]['beta'] += self.lr*dbeta2
def accuracy(self,preds,labels):
'''
Compute Accuracy
'''
p_idx = np.argmax(preds,axis=1)
l_idx = np.argmax(labels,axis=1)
pos = len(np.where(p_idx-l_idx == 0)[0])
return pos/(float(preds.shape[0])+self.eps)
def train(self):
'''
Start Training the network
'''
it_per_epoch = self.n_samples/self.batch_size + 1
it_num = 0
print "Starting Training.............."
for i in range(self.nepochs):
for j in range(it_per_epoch):
batch_data = self.get_data()
self.counter += self.batch_size
batch_imgs = batch_data[:,1:].astype(np.float32)/255.0 - 0.5
batch_labels = np.zeros((batch_imgs.shape[0],self.ncls))
batch_labels[np.arange(batch_labels.shape[0]),batch_data[:,0].astype(np.uint8).tolist()] = 1
cache = self.forward(batch_imgs,batch_labels)
grads = self.backward(cache,batch_imgs,batch_labels)
self.update(grads)
if not it_num%self.save_iter:
new_acc = self.accuracy(cache[-2],batch_labels)
new_loss = cache[-1]
print "Training Iteration===>%d, Loss ====>%.4f, Acc ====>%.4f"%(it_num,new_loss,new_acc)
it_num +=1
if it_num > self.maxiters:
break
if it_num > self.maxiters:
break
print "Done Training!"
self.save_model()
print "Model Saved!"
def test(self):
it_per_epoch = self.n_samples/self.batch_size + 1
it_num = 0
for j in range(it_per_epoch):
batch_data = self.get_data()
self.counter += self.batch_size
batch_imgs = batch_data[:,1:].astype(np.float32)/255.0 - 0.5
batch_labels = np.zeros((batch_imgs.shape[0],self.ncls))
batch_labels[np.arange(batch_labels.shape[0]),batch_data[:,0].astype(np.uint8).tolist()] = 1
cache = self.forward(batch_imgs,batch_labels)
print "Testing Iteration===>%d, Acc ====>%.4f"%(it_num,self.accuracy(cache[-2],batch_labels))
it_num +=1
def save_model(self):
'''
Save the trained mode at given model path
'''
if not os.path.exists(self.save_model_path):
os.makedirs(self.save_model_path)
else:
map(lambda x: os.unlink(os.path.join(self.save_model_path,x)), os.listdir(self.save_model_path))
np.save(os.path.join(self.save_model_path,'bn_params.npy'),self.batch_norm_params)
np.save(os.path.join(self.save_model_path,'weights.npy'),self.weights)
np.save(os.path.join(self.save_model_path,'biases.npy'),self.biases)
if __name__ == '__main__':
mode = sys.argv[1]
dpath = sys.argv[2]
model_path = sys.argv[3]
obj = NN(mode=mode,dpath=dpath,model_path=model_path)
if mode == 'train':
obj.train()
else:
obj.test()