-
Notifications
You must be signed in to change notification settings - Fork 5
/
nn_class.py
278 lines (215 loc) · 10.1 KB
/
nn_class.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
import numpy as np
import matplotlib.pyplot as plt
import math
class nn():
def __init__(self):
self.hidden_layers = None
self.input_nodes = None
self.output_nodes = None
self.avg_no = None
self.weights_list = []
self.bias_list = []
self.grad = None
self.lr = 0.
self.C = 0.
def set_params(self,hl,input_nodes, output, lr = 0.05,C = 0.1, avg_no = 40):
self.hidden_layers = hl
self.input_nodes = input_nodes
self.output_nodes = output
self.lr = lr
self.C = C
self.avg_no = avg_no
self.grad = np.zeros(self.avg_no)
self.set_weights_bias()
def set_weights_bias(self):
for i in range(len(self.hidden_layers)+1):
print(i)
if i == 0:
self.weights_list.append(np.matrix(self.weights(size = (self.input_nodes,self.hidden_layers[i]))))
self.bias_list.append(np.matrix(self.biases(size = (1,self.hidden_layers[i])))) #1x40
elif i == len(self.hidden_layers):
self.weights_list.append(np.matrix(self.weights(size = (self.hidden_layers[i-1],self.output_nodes)))) #20x1
self.bias_list.append(np.matrix(self.biases(size = (1,self.output_nodes))))
else:
self.weights_list.append(np.matrix(self.weights(size = (self.hidden_layers[i-1],self.hidden_layers[i])))) #40x20
self.bias_list.append(np.matrix(self.biases(size = (1,self.hidden_layers[i])))) #1x20
def forward_pass(self,data):
logits = []
activated_layers = []
for i in range(len(self.weights_list)):
if i == 0:
logits.append(np.add(np.matmul(data,self.weights_list[i]),self.bias_list[i]))
activated_layers.append(self.tanh(logits[i]))
elif i == (len(self.weights_list) - 1):
logits.append(np.add(np.matmul(activated_layers[i-1],self.weights_list[i]),self.bias_list[i]))
activated_layers.append(self.linear(logits[i]))
else:
logits.append(np.add(np.matmul(activated_layers[i-1],self.weights_list[i]),self.bias_list[i]))
activated_layers.append(self.tanh(logits[i]))
return activated_layers
def train(self, data, actual_y, total_epoch = 5000, threshold = 0.000001):
activated_layer = None
data_points = len(actual_y)
loss_graph = []
error = 1.
current_epoch = 0
dweights_moment, dbias_moment = [0.]*len(self.weights_list), [0.]*len(self.weights_list)
#threshold = 0.0000001
print(range(len(self.weights_list)-1,-1,-1))
while error > threshold and current_epoch<total_epoch:
current_epoch +=1
activated_layer = self.forward_pass(data)
dweights, dbias = [], []
for i in xrange(len(self.weights_list)-1,-1,-1):
if i == len(self.weights_list)-1:
base_weights = np.multiply(self.loss_func_mse_der(actual_y,activated_layer[-1])/data_points,self.der_linear(activated_layer[i]))
base_bias = np.multiply(self.loss_func_mse_der(actual_y,activated_layer[-1])/data_points,self.der_linear(activated_layer[i]))
dweights.append(np.dot(activated_layer[i-1].T,base_weights) + (self.C/data_points)*self.weights_list[i])
dbias.append(base_bias.sum(axis = 0))
if current_epoch == 1:
dweights_moment[len(self.weights_list) - i - 1] = self.ewa([], 0.9, dweights[len(self.weights_list) - i - 1])
dbias_moment[len(self.weights_list) - i - 1] = self.ewa([], 0.9, dbias[len(self.weights_list) - i - 1])
else:
dweights_moment[len(self.weights_list) - i - 1] = self.ewa(dweights_moment[len(self.weights_list) - i - 1], 0.9, dweights[len(self.weights_list) - i - 1])
dbias_moment[len(self.weights_list) - i - 1] = self.ewa(dbias_moment[len(self.weights_list) - i - 1], 0.9, dbias[len(self.weights_list) - i - 1])
elif i == 0:
base_weights = np.multiply(np.dot(base_weights,self.weights_list[i+1].T),self.der_tanh(activated_layer[i]))
dweights.append(np.dot(data.T,base_weights) + (self.C/data_points)*self.weights_list[i])
base_bias = np.multiply(np.dot(base_bias,self.weights_list[i+1].T),self.der_tanh(activated_layer[i]))
dbias.append(base_bias.sum(axis = 0))
if current_epoch == 1:
dweights_moment[len(self.weights_list) - i - 1] = self.ewa([], 0.9, dweights[len(self.weights_list) - i - 1])
dbias_moment[len(self.weights_list) - i - 1] = self.ewa([], 0.9, dbias[len(self.weights_list) - i - 1])
else:
dweights_moment[len(self.weights_list) - i - 1] = self.ewa(dweights_moment[len(self.weights_list) - i - 1], 0.9, dweights[len(self.weights_list) - i - 1])
dbias_moment[len(self.weights_list) - i - 1] = self.ewa(dbias_moment[len(self.weights_list) - i - 1], 0.9, dbias[len(self.weights_list) - i - 1])
elif (i>0 and i <len(self.weights_list)-1):
base_weights = np.multiply((np.dot(base_weights,self.weights_list[i+1].T)),self.der_tanh(activated_layer[i]))
dweights.append(np.dot(activated_layer[i-1].T,base_weights) + (self.C/data_points)*self.weights_list[i])
base_bias = np.multiply((np.dot(base_bias,self.weights_list[i+1].T)),self.der_tanh(activated_layer[i]))
dbias.append(base_bias.sum(axis = 0))
if current_epoch == 1:
dweights_moment[len(self.weights_list) - i - 1] = self.ewa([], 0.9, dweights[len(self.weights_list) - i - 1])
dbias_moment[len(self.weights_list) - i - 1] = self.ewa([], 0.9, dbias[len(self.weights_list) - i - 1])
else:
dweights_moment[len(self.weights_list) - i - 1] = self.ewa(dweights_moment[len(self.weights_list) - i - 1], 0.9, dweights[len(self.weights_list) - i - 1])
dbias_moment[len(self.weights_list) - i - 1] = self.ewa(dbias_moment[len(self.weights_list) - i - 1], 0.9, dbias[len(self.weights_list) - i - 1])
for i in range(len(self.weights_list)-1,-1,-1):
self.weights_list[i] = self.weights_list[i] - self.lr*dweights_moment[len(self.weights_list) - i - 1]
self.bias_list[i] = self.bias_list[i] - self.lr*dbias_moment[len(self.weights_list) - i - 1]
error = self.loss_func_mse(actual_y,activated_layer[-1])/data_points
if current_epoch%10000 == 0:
loss_graph.append(error)
print('Error at {}:{}'.format(current_epoch,error))
return loss_graph, activated_layer[-1]
@staticmethod
def ewa(moving_avg, beta, current_weights):
if len(moving_avg) == 0:
return (1-beta)*current_weights
else:
momentum = beta*(moving_avg) + (1 - beta)*current_weights
return momentum
def show_weights_bias(self):
print('Weights: {}'.format(self.weights_list))
print('Bias: {}'.format(self.bias_list))
def predict(self,test_data):
activated_layer = self.forward_pass(test_data)
return activated_layer[-1]
@staticmethod
def weights(mean = 0.0, std = 1.0 ,size=(0,0)):
return np.random.normal(mean,std,size)
@staticmethod
def biases(mean = 0.0, std = 0.0 ,size=(0,0)):
return np.random.normal(mean,std,size)
@staticmethod
def loss_func_mse(actual_y,predicted_y):
return np.matrix.sum(np.power((predicted_y - actual_y),2))
@staticmethod
def loss_func_mse_der(actual_y,predicted_y):
return 2*(predicted_y-actual_y)
@staticmethod
def tanh(x):
return (2/(1+np.exp(-2*x))) - 1
@staticmethod
def der_tanh(x):
return 1 - np.power(x,2)
@staticmethod
def der_sigmoid(x):
return np.multiply(x,(1.0-x))
@staticmethod
def sigmoid(x):
return 1.0/(1.0+ np.exp(-x))
@staticmethod
def linear(x):
return x
@staticmethod
def der_linear(x):
return np.ones_like(x)
def max_min_transform(arr):
a = (arr.max(axis = 0)-arr.min(axis = 0))
b = arr.min(axis = 0)
arr = (arr - b)/a
print(arr.shape, a.shape, b.shape)
return arr,a,b
def standardization(data, mean_data, std_data):
data = (data - mean_data)/std_data
#print(mean_data,std_data)
return data
if __name__ == '__main__':
theta1 =np.linspace(-np.pi/4.,np.pi/4.,250)
theta2 = np.linspace(-np.pi/4.,np.pi/4.,250)
np.random.seed(10)
np.random.shuffle(theta1)
np.random.shuffle(theta2)
xp = np.cos(theta1) + np.cos(theta1 + theta2)
yp = np.sin(theta1) + np.sin(theta1 + theta2)
#xp = np.cos(theta1)
#yp = np.sin(theta1)
dataset = np.array([xp, yp, theta1,theta2]).T
np.random.shuffle(dataset)
dataset = np.matrix(dataset)
actual_y = dataset[:,2:]
print(actual_y.shape)
#actual_y,a,b = max_min_transform(actual_y)
data = dataset[:,:2]
data_std = standardization(data, data.mean(axis = 0), data.std(axis =0))
my_nn = nn()
my_nn.set_params([3,5,4],2,2,lr = 0.05,C = 0.1, avg_no = 40)
loss_graph, train_predict = my_nn.train(data_std,actual_y,total_epoch = 60000, threshold = 0.00000000001)
plt.plot(range(len(loss_graph)), loss_graph)
plt.show()
##### Testing New Data #########
# Case 1:
'''
test_theta1 = np.linspace(0,np.pi/2.,22)
test_theta2 = np.linspace(0,np.pi/4.,22)
test_xp = np.cos(test_theta1) + np.cos(test_theta1 + test_theta2)
test_yp = np.sin(test_theta1) + np.sin(test_theta1 + test_theta2)
'''
# Case 2:
test_theta1 = np.linspace(0,np.pi/4.,5)
test_theta2 = np.linspace(-np.pi/4.,0,5)
np.random.shuffle(test_theta2)
np.random.shuffle(test_theta1)
test_xp = np.cos(test_theta1) + np.cos(test_theta1 + test_theta2)
test_yp = np.sin(test_theta1) + np.sin(test_theta1 + test_theta2)
test_dataset = np.array([test_xp, test_yp]).T
test_dataset = np.matrix(test_dataset)
#test_y = (test_dataset[:,2:] - b)/a
test_data = standardization(test_dataset[:,:2], data.mean(axis = 0), data.std(axis =0))
test_predict = my_nn.predict(test_data)
theta_predict = test_predict
# Get the values of theta 1 and theta 2
#theta_predict = np.multiply(test_predict,a) + b
predicted_x = np.cos(theta_predict[:,0]) + np.cos(theta_predict[:,0] + theta_predict[:,1])
predicted_y = np.sin(theta_predict[:,0]) + np.sin(theta_predict[:,0] + theta_predict[:,1])
loss = math.sqrt(np.sum(np.power((test_xp - predicted_x),2) + np.power((test_yp - predicted_y),2))/len(test_yp))
print('RMS Error: {}'.format(loss))
########### Plotting ################
plt.scatter([xp],[yp], c='y', label = 'Trained_labels')
plt.scatter([test_xp], [test_yp], c = 'b', label = 'True Values')
plt.scatter([predicted_x], [predicted_y], c='g', label = 'Predicted Values')
plt.xlim([-2.5,2.5])
plt.ylim([-2.5,2.5])
plt.legend(loc = 'best')
plt.show()