-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuralNetwork.py
241 lines (234 loc) · 14.4 KB
/
neuralNetwork.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
import scipy.special
import numpy
#import toolFunctions
class neuralNetwork:
def __init__(self, inputNodes, hiddenNodes, outputNodes,learningRate,activationFunction,randomMode,seedWih,seedWho,noOfHiddenLayer):
self.iNodes = inputNodes
self.oNodes = outputNodes
self.noOfHiddenLayer=noOfHiddenLayer
if(self.noOfHiddenLayer!=1 and self.noOfHiddenLayer!=2 and self.noOfHiddenLayer!=3):
self.noOfHiddenLayer=1;
if(noOfHiddenLayer==1):
self.hNodes = hiddenNodes
elif(noOfHiddenLayer==2):
self.hNodes = hiddenNodes
self.hNodes2 = hiddenNodes
elif(noOfHiddenLayer==3):
self.hNodes = hiddenNodes
self.hNodes2 = hiddenNodes
self.hNodes3 = hiddenNodes
else:
self.hNodes = hiddenNodes
self.lr = learningRate
self.randomMode=randomMode
self.seedWih=seedWih
self.seedWho=seedWho
self.totalEpochs=0
if(randomMode==1):
numpy.random.seed(seedWih)
self.wih = numpy.random.rand(self.hNodes, self.iNodes)-0.5
numpy.random.seed(seedWho)
self.who = numpy.random.rand(self.oNodes, self.hNodes)-0.5
if(noOfHiddenLayer==2):
numpy.random.seed(6)
self.whh = numpy.random.rand(self.hNodes, self.hNodes)-0.5
elif(noOfHiddenLayer==3):
numpy.random.seed(6)
self.whh = numpy.random.rand(self.hNodes, self.hNodes)-0.5
numpy.random.seed(7)
self.whh2 = numpy.random.rand(self.hNodes, self.hNodes)-0.5
else:
numpy.random.seed(seedWih)
self.wih = numpy.random.normal (0.0, pow(self.hNodes,-0.5), (self.hNodes, self.iNodes))
numpy.random.seed(seedWho)
self.who = numpy.random.normal (0.0, pow(self.oNodes,-0.5), (self.oNodes, self.hNodes))
if(noOfHiddenLayer==2):
numpy.random.seed(6)
self.whh = numpy.random.normal (0.0, pow(self.hNodes,-0.5), (self.hNodes, self.hNodes))
elif(noOfHiddenLayer==3):
numpy.random.seed(6)
self.whh = numpy.random.normal (0.0, pow(self.hNodes,-0.5), (self.hNodes, self.hNodes))
numpy.random.seed(7)
self.whh2 = numpy.random.normal (0.0, pow(self.hNodes,-0.5), (self.hNodes, self.hNodes))
switch = {
1:lambda x:1/(1+numpy.exp(-x)),#sigmoid
2:lambda x:2/(1+numpy.exp(-x)),#softsign
3:lambda x:(numpy.fabs(x)+x)/2,#relu
4:lambda x:(1-numpy.exp(-2*x))/(1+numpy.exp(-2*x)),#tanh
5:lambda x:numpy.log(1+numpy.exp(x))#softplus
}
self.activation_function = switch[activationFunction]
self.activationFunctionIndex=activationFunction
#f'(x)
#1 -numpy.exp(-x)/(1+numpy.exp(-x))^2
#2 -2*numpy.exp(-x)/(1+numpy.exp(-x))^2
#3 1/0
#4 -4*numpy.exp(-2*x)/(1+numpy.exp(-2*x))^2
#5
#print(self.wih)
pass
def train(self,inputs_list, target_list,activateFunctionSelect):
inputs = numpy.array(inputs_list, ndmin=2).T
targets = numpy.array(target_list, ndmin=2).T
#print(inputs)
#print(targets)
if(self.noOfHiddenLayer==1):
hidden_inputs = numpy.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = numpy.dot(self.who,hidden_outputs)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors = numpy.dot(self.who.T,output_errors)
# all these Derivative results have been deleted the constant !in order to add... and you need to pay attention to them
if(activateFunctionSelect==1):#sigmoid ok
fx1=numpy.exp(-final_inputs)/((1+numpy.exp(-final_inputs))*(1+numpy.exp(-final_inputs)))
fx2=numpy.exp(-hidden_inputs)/((1+numpy.exp(-hidden_inputs))*(1+numpy.exp(-hidden_inputs)))
elif(activateFunctionSelect==2):#softsign ok
fx1=numpy.exp(-final_inputs)/((1+numpy.exp(-final_inputs))*(1+numpy.exp(-final_inputs)))
fx2=numpy.exp(-hidden_inputs)/((1+numpy.exp(-hidden_inputs))*(1+numpy.exp(-hidden_inputs)))
elif(activateFunctionSelect==3):#relu ok
final_inputs[final_inputs>0]=1
hidden_inputs[hidden_inputs>0]=1
final_inputs[final_inputs<0]=0
hidden_inputs[hidden_inputs<0]=0
fx1=final_inputs;
fx2=hidden_inputs
elif(activateFunctionSelect==4):#tanh maybe we can do
fx1=numpy.exp(-2*final_inputs)/((1+numpy.exp(-2*final_inputs))*(1+numpy.exp(-2*final_inputs)))
fx2=numpy.exp(-2*hidden_inputs)/((1+numpy.exp(-2*hidden_inputs))*(1+numpy.exp(-2*hidden_inputs)))
elif(activateFunctionSelect==5):#softplus ok
fx1=numpy.exp(final_inputs)/(1+numpy.exp(final_inputs))
fx2=numpy.exp(hidden_inputs)/(1+numpy.exp(hidden_inputs))
pass
self.who += self.lr * numpy.dot((output_errors * fx1),numpy.transpose(hidden_outputs))
self.wih += self.lr * numpy.dot((hidden_errors * fx2),numpy.transpose(inputs))
elif(self.noOfHiddenLayer==2):
hidden_inputs = numpy.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
hidden_inputs2 = numpy.dot(self.whh,hidden_outputs)
hidden_outputs2 = self.activation_function(hidden_inputs2)
final_inputs = numpy.dot(self.who,hidden_outputs2)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors2 = numpy.dot(self.who.T,output_errors)
hidden_errors = numpy.dot(self.whh.T,hidden_errors2)
# all these Derivative results have been deleted the constant !in order to add... and you need to pay attention to them
if(activateFunctionSelect==1):#sigmoid ok
fx1=numpy.exp(-final_inputs)/((1+numpy.exp(-final_inputs))*(1+numpy.exp(-final_inputs)))
fx2=numpy.exp(-hidden_inputs2)/((1+numpy.exp(-hidden_inputs2))*(1+numpy.exp(-hidden_inputs2)))
fx3=numpy.exp(-hidden_inputs)/((1+numpy.exp(-hidden_inputs))*(1+numpy.exp(-hidden_inputs)))
elif(activateFunctionSelect==2):#softsign ok
fx1=numpy.exp(-final_inputs)/((1+numpy.exp(-final_inputs))*(1+numpy.exp(-final_inputs)))
fx2=numpy.exp(-hidden_inputs2)/((1+numpy.exp(-hidden_inputs2))*(1+numpy.exp(-hidden_inputs2)))
fx3=numpy.exp(-hidden_inputs)/((1+numpy.exp(-hidden_inputs))*(1+numpy.exp(-hidden_inputs)))
elif(activateFunctionSelect==3):#relu ok
final_inputs[final_inputs>0]=1
hidden_inputs[hidden_inputs>0]=1
hidden_inputs2[hidden_inputs>0]=1
final_inputs[final_inputs<0]=0
hidden_inputs[hidden_inputs<0]=0
hidden_inputs2[hidden_inputs<0]=0
fx1=final_inputs;fx2=hidden_inputs2;fx3=hidden_inputs
elif(activateFunctionSelect==4):#tanh maybe we can do
fx1=numpy.exp(-2*final_inputs)/((1+numpy.exp(-2*final_inputs))*(1+numpy.exp(-2*final_inputs)))
fx2=numpy.exp(-2*hidden_inputs2)/((1+numpy.exp(-2*hidden_inputs2))*(1+numpy.exp(-2*hidden_inputs2)))
fx3=numpy.exp(-2*hidden_inputs)/((1+numpy.exp(-2*hidden_inputs))*(1+numpy.exp(-2*hidden_inputs)))
elif(activateFunctionSelect==5):#softplus ok
fx1=numpy.exp(final_inputs)/(1+numpy.exp(final_inputs))
fx2=numpy.exp(hidden_inputs2)/(1+numpy.exp(hidden_inputs2))
fx3=numpy.exp(hidden_inputs)/(1+numpy.exp(hidden_inputs))
pass
self.who += self.lr * numpy.dot((output_errors * fx1),numpy.transpose(hidden_outputs2))
self.whh += self.lr * numpy.dot((hidden_errors2 * fx2),numpy.transpose(hidden_outputs))
self.wih += self.lr * numpy.dot((hidden_errors * fx3),numpy.transpose(inputs))
elif(self.noOfHiddenLayer==3):
hidden_inputs = numpy.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
hidden_inputs2 = numpy.dot(self.whh,hidden_outputs)
hidden_outputs2 = self.activation_function(hidden_inputs2)
hidden_inputs3 = numpy.dot(self.whh,hidden_outputs2)
hidden_outputs3 = self.activation_function(hidden_inputs3)
final_inputs = numpy.dot(self.who,hidden_outputs3)
final_outputs = self.activation_function(final_inputs)
output_errors = targets - final_outputs
hidden_errors3 = numpy.dot(self.who.T,output_errors)
hidden_errors2 = numpy.dot(self.whh2.T,hidden_errors3)
hidden_errors = numpy.dot(self.whh.T,hidden_errors2)
# all these Derivative results have been deleted the constant !in order to add... and you need to pay attention to them
if(activateFunctionSelect==1):#sigmoid ok
fx1=numpy.exp(-final_inputs)/((1+numpy.exp(-final_inputs))*(1+numpy.exp(-final_inputs)))
fx2=numpy.exp(-hidden_inputs2)/((1+numpy.exp(-hidden_inputs2))*(1+numpy.exp(-hidden_inputs2)))
fx3=numpy.exp(-hidden_inputs3)/((1+numpy.exp(-hidden_inputs3))*(1+numpy.exp(-hidden_inputs3)))
fx4=numpy.exp(-hidden_inputs)/((1+numpy.exp(-hidden_inputs))*(1+numpy.exp(-hidden_inputs)))
elif(activateFunctionSelect==2):#softsign ok
fx1=numpy.exp(-final_inputs)/((1+numpy.exp(-final_inputs))*(1+numpy.exp(-final_inputs)))
fx2=numpy.exp(-hidden_inputs2)/((1+numpy.exp(-hidden_inputs2))*(1+numpy.exp(-hidden_inputs2)))
fx3=numpy.exp(-hidden_inputs3)/((1+numpy.exp(-hidden_inputs3))*(1+numpy.exp(-hidden_inputs3)))
fx4=numpy.exp(-hidden_inputs)/((1+numpy.exp(-hidden_inputs))*(1+numpy.exp(-hidden_inputs)))
elif(activateFunctionSelect==3):#relu ok
final_inputs[final_inputs>0]=1
hidden_inputs[hidden_inputs>0]=1
hidden_inputs2[hidden_inputs>0]=1
hidden_inputs3[hidden_inputs>0]=1
final_inputs[final_inputs<0]=0
hidden_inputs[hidden_inputs<0]=0
hidden_inputs2[hidden_inputs<0]=0
hidden_inputs3[hidden_inputs<0]=0
fx1=final_inputs;fx2=hidden_inputs2;fx3=hidden_inputs3;fx4=hidden_inputs
elif(activateFunctionSelect==4):#tanh maybe we can do
fx1=numpy.exp(-2*final_inputs)/((1+numpy.exp(-2*final_inputs))*(1+numpy.exp(-2*final_inputs)))
fx2=numpy.exp(-2*hidden_inputs2)/((1+numpy.exp(-2*hidden_inputs2))*(1+numpy.exp(-2*hidden_inputs2)))
fx3=numpy.exp(-2*hidden_inputs3)/((1+numpy.exp(-2*hidden_inputs3))*(1+numpy.exp(-2*hidden_inputs3)))
fx4=numpy.exp(-2*hidden_inputs)/((1+numpy.exp(-2*hidden_inputs))*(1+numpy.exp(-2*hidden_inputs)))
elif(activateFunctionSelect==5):#softplus ok
fx1=numpy.exp(final_inputs)/(1+numpy.exp(final_inputs))
fx2=numpy.exp(hidden_inputs2)/(1+numpy.exp(hidden_inputs2))
fx3=numpy.exp(hidden_inputs3)/(1+numpy.exp(hidden_inputs3))
fx4=numpy.exp(hidden_inputs)/(1+numpy.exp(hidden_inputs))
pass
self.who += self.lr * numpy.dot((output_errors * fx1),numpy.transpose(hidden_outputs3))
self.whh2 += self.lr * numpy.dot((hidden_errors3 * fx2),numpy.transpose(hidden_outputs2))
self.whh += self.lr * numpy.dot((hidden_errors2 * fx3),numpy.transpose(hidden_outputs))
self.wih += self.lr * numpy.dot((hidden_errors * fx4),numpy.transpose(inputs))
pass
def query(self, inputs_list):
inputs = numpy.array(inputs_list, ndmin=2).T
if(self.noOfHiddenLayer==1):
hidden_inputs = numpy.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
final_inputs = numpy.dot(self.who,hidden_outputs)
final_outputs = self.activation_function(final_inputs)
elif(self.noOfHiddenLayer==2):
hidden_inputs = numpy.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
hidden_inputs2 = numpy.dot(self.whh,hidden_outputs)
hidden_outputs2 = self.activation_function(hidden_inputs2)
final_inputs = numpy.dot(self.who,hidden_outputs2)
final_outputs = self.activation_function(final_inputs)
elif(self.noOfHiddenLayer==3):
hidden_inputs = numpy.dot(self.wih,inputs)
hidden_outputs = self.activation_function(hidden_inputs)
hidden_inputs2 = numpy.dot(self.whh,hidden_outputs)
hidden_outputs2 = self.activation_function(hidden_inputs2)
hidden_inputs3 = numpy.dot(self.whh,hidden_outputs2)
hidden_outputs3 = self.activation_function(hidden_inputs3)
final_inputs = numpy.dot(self.who,hidden_outputs3)
final_outputs = self.activation_function(final_inputs)
#hidden_outputs=numpy.insert(hidden_outputs,0,target)#need to modify query function by add target parameter
#hidden_outputs=hidden_outputs.tolist()
#toolFunctions.makeMNIST(hidden_outputs,'dstNeedToSAVE.csv')
return final_outputs
pass
def checkParameter(self):
print("self.iNodes",self.iNodes)
print("self.oNodes",self.oNodes)
print("hiddenNodes",self.hNodes)
print("self.lr",self.lr)
print("self random mode:",self.randomMode)
print("self random seed Wih:",self.seedWih)
print("self random seed Who:",self.seedWho)
print("self activationFunctionIndex:",self.activationFunctionIndex)
print("no. of layer:",self.noOfHiddenLayer)
#print("self.wih",self.wih.shape,' ',self.wih)
#print("self.who",self.who.shape,' ',self.who)
pass