-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_nn.py
More file actions
executable file
·280 lines (208 loc) · 8.88 KB
/
Copy pathai_nn.py
File metadata and controls
executable file
·280 lines (208 loc) · 8.88 KB
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
import numpy as np
class ActivationFunction:
def __init__(self, func,dfunc):
self.func = func
self.dfunc = dfunc
sigmoid = ActivationFunction(
lambda x:1 / (1 + np.exp(-x)),
lambda y:y * (1 - y)
)
linear = ActivationFunction(
lambda x:x,
lambda y:1
)
tanh = ActivationFunction(
lambda x:np.tanh(x),
lambda y:1 - (y * y)
)
relu = ActivationFunction(
lambda x:x * (x > 0),
lambda y:1. * (y > 0)
)
class NeuralNetwork:
def __init__(self,i_nodes, h_nodes, o_nodes, open = False):
self.risetime_max = 0
self.risetime_min = 0
self.overshoot_max = 0
self.overshoot_min = 0
self.settling_max = 0
self.settling_min = 0
self.peak_max = 0
self.peak_min = 0
self.steady_max = 0
self.steady_min = 0
if isinstance(i_nodes, NeuralNetwork):
temp = i_nodes
self.input_nodes = temp.input_nodes
self.hidden_nodes = temp.hidden_nodes
self.output_nodes = temp.output_nodes
self.weights_ih = temp.weights_ih.copy()
self.weights_ho = temp.weights_ho.copy()
self.bias_h = temp.bias_h.copy()
self.bias_o = temp.bias_o.copy()
else:
self.input_nodes = i_nodes
self.hidden_nodes = h_nodes
self.output_nodes = o_nodes
if open == False:
random_func = lambda x:x*2-1
self.weights_ih = np.random.rand(self.input_nodes, self.hidden_nodes)
func_ih = np.vectorize(random_func)
self.weights_ih = np.matrix(func_ih(self.weights_ih))
self.weights_ho = np.random.rand(self.hidden_nodes, self.output_nodes)
func_ho = np.vectorize(random_func)
self.weights_ho = np.matrix(func_ho(self.weights_ho))
self.bias_h = np.random.rand(1, self.hidden_nodes)
func_bias_h = np.vectorize(random_func)
self.bias_h = np.matrix(func_bias_h(self.bias_h))
self.bias_o = np.random.rand(1, self.output_nodes)
func_bias_o = np.vectorize(random_func)
self.bias_o = np.matrix(func_bias_o(self.bias_o))
self.setActivation("sigmoid")
self.setLearningRate(0.1)
def setActivation(self, func):
if func == "sigmoid":
self.activation_function = sigmoid
elif func == "tanh":
self.activation_function = tanh
elif func == "relu":
self.activation_function = relu
elif func == "linear":
self.activation_function = linear
self.activation_function_o = linear
def save(self):
np.save("weights_ih", self.weights_ih)
np.save("weights_ho", self.weights_ho)
np.save("bias_h", self.bias_h)
np.save("bias_o", self.bias_o)
np.save("rt", ([self.risetime_min,self.risetime_max]))
np.save("os", ([self.overshoot_min,self.overshoot_max]))
np.save("st", ([self.settling_min,self.settling_max]))
np.save("pk", ([self.peak_min,self.peak_max]))
np.save("se", ([self.steady_min,self.steady_max]))
def normalize(self, x):
temp = []
x1 = []
x2 = []
x3 = []
x4 = []
x5 = []
for i in range(len(x)-1):
flag = False
if i == 0:
temp.append(x[0])
else:
for j in range(len(temp)):
if(x[i][0] == temp[j][0] and x[i][1] == temp[j][1] and x[i][2] == temp[j][2]):
flag = True
if flag == False:
temp.append(x[i])
x1.append(float(x[i][4]))
x2.append(float(x[i][5]))
x3.append(float(x[i][6]))
x4.append(float(x[i][7]))
x5.append(float(x[i][8]))
self.risetime_max = np.amax(x1,axis=0)
self.risetime_min = np.amin(x1,axis=0)
self.overshoot_max = np.amax(x2,axis=0)
self.overshoot_min = np.amin(x2,axis=0)
self.settling_max = np.amax(x3,axis=0)
self.settling_min = np.amin(x3,axis=0)
self.peak_max = np.amax(x4,axis=0)
self.peak_min = np.amin(x4,axis=0)
self.steady_max = np.amax(x5,axis=0)
self.steady_min = np.amin(x5,axis=0)
return temp
def setLearningRate(self, r):
self.learning_rate = r
def funcMap(self, input, func):
temp = input.copy()
val = 0
for i in range (input.shape[0]):
for j in range (input.shape[1]):
val = input[i,j]
temp[i,j] = func(val)
return temp
def predict_from_model(self, input):
self.weights_ih = np.load("weights_ih.npy")
self.weights_ho = np.load("weights_ho.npy")
self.bias_h = np.load("bias_h.npy")
self.bias_o = np.load("bias_o.npy")
rt = np.load("rt.npy")
os = np.load("os.npy")
st = np.load("st.npy")
pk = np.load("pk.npy")
se = np.load("se.npy")
temp = input
temp[0] = np.interp(input[0],rt,[0,1])
temp[1] = np.interp(input[1],os,[0,1])
temp[2] = np.interp(input[2],st,[0,1])
temp[3] = np.interp(input[3],pk,[0,1])
temp[4] = np.interp(input[4],se,[0,1])
input = np.matrix(temp)
hidden = np.dot(input, self.weights_ih)
hidden = np.add(hidden, self.bias_h)
hidden = self.funcMap(hidden, self.activation_function.func)
output = np.dot(hidden, self.weights_ho)
output = np.add(output, self.bias_o)
output = self.funcMap(output, self.activation_function_o.func)
return output
def predict(self, input):
temp = input
temp[0] = np.interp(input[0],[self.risetime_min,self.risetime_max],[0,1])
temp[1] = np.interp(input[1],[self.overshoot_min,self.overshoot_max],[0,1])
temp[2] = np.interp(input[2],[self.settling_min,self.settling_max],[0,1])
temp[3] = np.interp(input[3],[self.peak_min,self.peak_max],[0,1])
temp[4] = np.interp(input[4],[self.steady_min,self.steady_max],[0,1])
input = np.matrix(temp)
hidden = np.dot(input, self.weights_ih)
hidden = np.add(hidden, self.bias_h)
hidden = self.funcMap(hidden, self.activation_function.func)
output = np.dot(hidden, self.weights_ho)
output = np.add(output, self.bias_o)
output = self.funcMap(output, self.activation_function_o.func)
return output
def train(self, input, target):
temp = input
temp[0] = np.interp(input[0],[self.risetime_min,self.risetime_max],[0,1])
temp[1] = np.interp(input[1],[self.overshoot_min,self.overshoot_max],[0,1])
temp[2] = np.interp(input[2],[self.settling_min,self.settling_max],[0,1])
temp[3] = np.interp(input[3],[self.peak_min,self.peak_max],[0,1])
temp[4] = np.interp(input[4],[self.steady_min,self.steady_max],[0,1])
input = np.matrix(temp)
#Generate hidden input calculation, and add with bias
hidden = np.dot(input, self.weights_ih)
hidden = np.add(hidden, self.bias_h)
#Activate hidden node
hidden = self.funcMap(hidden, self.activation_function.func)
#Generate Output of output node, and add with bias
output = np.dot(hidden, self.weights_ho)
output = np.add(output, self.bias_o)
#Activate output node
output = self.funcMap(output, self.activation_function_o.func)
#Calculate error of system prediction
output_error = np.subtract(target, output)
#print output_error[0, 0]
#Calculate the gradien of output
gradient = self.funcMap(output, self.activation_function_o.dfunc)
gradient = np.multiply(gradient, output_error)
gradient = np.multiply(gradient, self.learning_rate)
#Calculate deltas
hidden_t = np.transpose(hidden)
weight_ho_delta = np.dot(hidden_t,gradient)
#Adjust weight by the deltas
self.weights_ho = np.add(self.weights_ho, weight_ho_delta)
#Adjust bias by the gradien
self.bias_o = np.add(self.bias_o, gradient)
#Calculate hidden layer error
who_t = np.transpose(self.weights_ho)
hidden_error = np.dot(output_error, who_t)
#Calculate hidden gradien
hidden_gradien = self.funcMap(hidden, self.activation_function.dfunc)
hidden_gradien = np.multiply(hidden_gradien, hidden_error)
hidden_gradien = np.multiply(hidden_gradien, self.learning_rate)
#calculate input node to hidden node deltas
input_T = np.transpose(input)
weight_ih_delta = np.dot(input_T, hidden_gradien)
self.weights_ih = np.add(self.weights_ih, weight_ih_delta)
self.bias_h = np.add(self.bias_h, hidden_gradien)