-
Notifications
You must be signed in to change notification settings - Fork 0
/
nn.py
212 lines (192 loc) · 6.42 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
import numpy as np
import activations as af
import datetime
import sys
import matplotlib.pyplot as plt
import os
def log(str):
tm = datetime.datetime.now().strftime("%I:%M:%S %p")
print("{} -> {}".format(tm,str))
def init_parameters(layer_dims,parameters_file):
if parameters_file == None:
return init_parameters_new(layer_dims)
log("Reusing the parameter from {}".format(parameters_file))
parameters = np.load(parameters_file)
log(type(parameters))
log(parameters[()]['W1'].shape)
return parameters[()]
def init_parameters_new(layer_dims):
parameters = {}
L = len(layer_dims)
for l in range(1, L):
parameters['W' + str(l)] = np.random.randn(layer_dims[l], layer_dims[l - 1]) / np.sqrt(layer_dims[l-1])
parameters['b' + str(l)] = np.zeros((layer_dims[l], 1))
return parameters
def perform_activation(activation,Z):
return getattr(sys.modules["activations"],activation)(Z)
'''
if activation == 'tanh':
return af.tanh(Z)
elif activation == 'sigmoid':
return af.sigmoid(Z)
elif activation == 'relu':
return af.relu(Z)
'''
def perform_activation_backwards(activation,Z,A):
return getattr(sys.modules["activations"],"inverse_"+activation)(Z,A)
'''
s = perform_activation(activation,z)
dZ = dA * s * (1-s)
return dZ
'''
def update_grads(grads,parameters,alpha):
L = len(parameters) // 2
for l in range(1,L+1):
parameters['W' + str(l)] = parameters['W' + str(l)] - alpha * grads['dW' + str(l)]
parameters['b' + str(l)] = parameters['b' + str(l)] - alpha * grads['db' + str(l)]
return parameters #have to check whether this is really reqd
def back_prop(m,A_values,Z_values,Y,activation,parameters):
grads = {}
dZ = A_values[-1] - Y
L = len(A_values)-1
for l in reversed(range(L)):
grads['dW' + str(l + 1)] = (1 / m) * np.dot(dZ,A_values[l].T)
grads['db' + str(l + 1)] = (1 / m) * np.sum(dZ,axis=1,keepdims=True)
if l != 0:
dZ = np.dot(parameters['W' + str(l+1)].T,dZ)
dZ *= perform_activation_backwards(activation[0],Z_values[l-1],A_values[l])
return grads
def forward_prop(X,activation,parameters):
A_values = []
Z_values = []
L = len(parameters) // 2
A = X
A_values.append(A)
for l in range(1,L):
Z = np.dot(parameters['W' + str(l)],A) + parameters['b' + str(l)]
A = perform_activation(activation[0],Z)
A_values.append(A)
Z_values.append(Z)
Z = np.dot(parameters['W' + str(L)],A) + parameters['b' + str(L)]
A = perform_activation(activation[1],Z)
A_values.append(A)
Z_values.append(Z)
return A_values,Z_values
def validate (Y,Y1,m):
succ = 0
for i in range(m):
if(np.sum(Y[:,i] == Y1[:,i]) == 10):
succ+=1
return succ/m
def predict(m,A2):
Y = np.zeros((10, m))
for i in range(m):
max_val = 0
max_val_id = 0
for j in range(10):
if A2[j,i] > max_val :
max_val_id = j
max_val = A2[j,i]
Y[max_val_id,i] = 1
return Y
def get_batch(X,Y,m,X_current_batch,Y_current_batch,batch_size,batch_cursor,epoch):
X_current_batch[:,0:batch_size] = X[:,batch_cursor:batch_cursor+batch_size]
Y_current_batch[:,0:batch_size] = Y[:,batch_cursor:batch_cursor+batch_size]
if batch_cursor + 2*batch_size >= m:
batch_cursor = 0
epoch+=1
else:
batch_cursor += batch_size
return X_current_batch,Y_current_batch,batch_cursor,epoch
def compute_cost(y_hat,Y,m,train_cost,train_accu):
logprobs = np.multiply(np.log(y_hat), Y) + np.multiply((1 - Y), np.log(1 - y_hat))
cost = - np.sum(logprobs) / m
train_cost.append(cost)
Y2 = predict(m,y_hat)
accu = validate(Y,Y2,m)
train_accu.append(accu)
return cost
def compute_dev_set(X,Y,m,activation,parameters,dev_accu):
A_values,Z_values = forward_prop(X,activation,parameters)
Y2 = predict(m,A_values[-1])
accu = validate(Y,Y2,m)
dev_accu.append(accu)
return accu
def model(X,Y,**kwargs):
log("Entered model with {}".format(kwargs))
log("X size : {}, Y size : {}".format(X.shape,Y.shape))
x_n,m = X.shape
y_n = len(Y)
alpha = kwargs.get('alpha',0.01)
iter = kwargs.get('iter',3000)
layer_dims = kwargs.get('hidden_layer_dims',[])
activation = kwargs.get('activation',['tanh','sigmoid'])
batch_size = kwargs.get('batch_size',m)
dev_set_ratio = kwargs.get('dev_set_ratio',0.02)
parameters_file = kwargs.get('parameters_file',None)
layer_dims.insert(0,x_n)
layer_dims.insert(len(layer_dims),y_n)
parameters = init_parameters(layer_dims,parameters_file)
log(len(parameters))
iterations_capture_freq = 50
capture_frequency = 500
accu = 0
train_cost = []
train_accu = []
dev_accu = []
batch_cursor = 0
epoch = 0
X_current_batch = np.zeros([x_n,batch_size])
Y_current_batch = np.zeros([y_n,batch_size])
m_dev = int(m*dev_set_ratio)
m = m - m_dev
X,X_dev = np.split(X,[m],axis=1)
Y,Y_dev = np.split(Y,[m],axis=1)
log("Post splitting of train and dev set, shape of train : {} , dev : {}".format(X.shape,X_dev.shape))
print("Training the model, please wait")
print("00.00% cost: 00.0000 accu: 0.0000",end="")
for i in range(iter):
X_current_batch,Y_current_batch,batch_cursor,epoch = get_batch(X,Y,m,X_current_batch,Y_current_batch,batch_size,batch_cursor,epoch)
A_values,Z_values = forward_prop(X_current_batch,activation,parameters)
if(i%iterations_capture_freq==0):
cost = compute_cost(A_values[-1],Y_current_batch,batch_size,train_cost,train_accu)
if m_dev >0:
accu = compute_dev_set(X_dev,Y_dev,m_dev,activation,parameters,dev_accu)
print("\b"*35,end="")
print("{:05.2f}% cost: {:07.4f} accu: {:06.4f}".format((i/iter*100),cost,accu),end="",flush=True)
#log('dev acc : {}'.format(accu))
grads = back_prop(batch_size,A_values,Z_values,Y_current_batch,activation,parameters)
parameters = update_grads(grads,parameters,alpha)
if i%capture_frequency == 0 and i!=0:
snapshot(train_cost,train_accu,dev_accu,parameters,i)
print("")
if m_dev >0:
accu = compute_dev_set(X_dev,Y_dev,m_dev,activation,parameters,dev_accu)
snapshot(train_cost,train_accu,dev_accu,parameters,i)
log("Model ready with accuracy : {}".format(accu))
return parameters
def snapshot(train_cost,train_accu,dev_accu,parameters,i):
plt.clf()
dir = os.path.abspath("output/snapshots")
if not os.path.exists(dir):
os.makedirs(dir)
np.save(os.path.join(dir, 'parameters'+str(i)),parameters)
#cost graph
plt.subplot(3,1,1)
plt.grid(True)
ay = plt.gca()
ay.set_yscale('log')
plt.plot(train_cost)
plt.title("Cost graph")
#train accu
plt.subplot(3,1,2)
plt.grid(True)
plt.plot(train_accu)
plt.title("Training accuracy")
#dev accu
plt.subplot(3,1,3)
plt.grid(True)
plt.plot(dev_accu)
plt.title("Dev set accuracy")
plt.savefig(dir+"/graph"+str(i)+".png")
plt.close()