@@ -71,7 +71,10 @@ def calcMaxIndex(self, li):
7171
7272
7373def calcOutput (self , x ):
74+ #print("Here")
75+ #print(x)
7476x = int (x )
77+ #print("x -> ",x)
7578self .output = {0 :'A' , 1 :'B' , 2 :'C' , 3 :'D' , 4 :'E' , 5 :'F' , 6 :'G' , 7 :'H' , 8 :'I' , 9 :'J' }[x ]
7679return self .output
7780
@@ -87,6 +90,10 @@ def trainNetwork(self):
8790def getOutput (self ):
8891return self .output
8992
93+ # def getOutForInput(self , x):
94+ # if self.input_layer_outputs[x] == 'A':
95+ # return
96+
9097
9198def sgdTrain (self ):
9299print ("In training" )
@@ -107,30 +114,54 @@ def sgdTrain(self):
107114#print("len of nn_inputs: " , len(self.nn_inputs))
108115while (imageInd < len (self .nn_inputs )):
109116#print("And Now in Second while:)")
110- imageInd = 3 * coef + remainder
111- #print("this is imageInd: ",imageInd)
117+ #print("this is imageInd: " , imageInd)
112118#print("imageInd: ",self.nn_inputs[imageInd][1])
119+ #print("sgd size input : " , len(self.inlay.setNewInput(self.nn_inputs[imageInd][1])))
113120self .input_layer_outputs = self .inlay .setNewInput (self .nn_inputs [imageInd ][1 ])
121+ #print("sgd size input out : " , len(self.input_layer_outputs))
114122#print ("this is the len of input_layer_outputs -> ",len(self.input_layer_outputs))
115123#print ("out input layer: ",self.input_layer_outputs)
116124#print("hello sabri:)")
117125self .hidden_layer_outputs = self .hidlay .setNewInput (self .input_layer_outputs )
118126#print ("out hidden layer: " , self.hidden_layer_outputs)
119127self .output_layer_output = self .outlay .setNewInput (self .hidden_layer_outputs )
120- print ("Expected : {}" .format (self .nn_inputs [imageInd ][0 ]))
121- print ("Got : " + self .calcOutput (self .calcMaxIndex (self .output_layer_output )))
128+ # print("Expected : {}".format(self.nn_inputs[imageInd][0]))
129+ # print("Got : "+self.calcOutput(self.calcMaxIndex(self.output_layer_output)))
122130l2_error = self .sgdCalcError (self .nn_inputs [imageInd ][0 ], self .calcOutput (self .calcMaxIndex (self .output_layer_output )))
123131#print("output layer output -> " , self.calcOutput(self.calcMaxIndex(self.output_layer_output)))
124132l2_delta = l2_error * sigmoidDeriv (ord (self .calcOutput (self .calcMaxIndex (self .output_layer_output ))))
125- syn1 = self .outlay .getWeights ()
133+ #print("max index out layer: " , self.calcMaxIndex(self.output_layer_output))
134+ #print("sgdtrain out layer: " , self.calcOutput(self.calcMaxIndex(self.output_layer_output)))
135+ #TODO: Test
136+ syn2 = self .outlay .getWeights ()
137+ syn1 = self .hidlay .getWeights ()
138+ syn0 = self .inlay .getWeights ()
139+ #print(syn0)
126140# print("syn1.T is: " , syn1.T)
127- l1_error = np .asarray (l2_delta ).dot (syn1 .T )
128-
141+ l2 = ord (self .calcOutput (self .calcMaxIndex (self .output_layer_output )))
142+ l1 = ord (self .calcOutput (self .calcMaxIndex (self .hidden_layer_outputs )))
143+ #l0 = ord(self.calcOutput(self.calcMaxIndex(self.input_layer_outputs)))
144+ #print("sgdtrain hidden layer" , self.calcOutput(self.calcMaxIndex(self.hidden_layer_outputs)))
145+ #print("max index input layer: " , self.calcMaxIndex(self.input_layer_outputs))
146+ #m = self.calcMaxIndex(self.input_layer_outputs)
147+ #print("sgdtrain input layer" , self.input_layer_outputs[self.calcMaxIndex(self.input_layer_outputs)])
148+ #print("calc output input layer: " , self.calcOutput(m))
149+ l1_error = np .asarray (l2_delta ).dot (syn2 .T )
150+ l1_delta = l1_error * sigmoidDeriv (l1 )
151+ # print(l1.T)
152+ syn2 += np .asarray (l1 ).T .dot (np .asarray (l2_delta ))
153+ #print("syn2: " , syn2)
154+ #syn1 += l0.T.dot(np.asarray(l1_delta))
155+
129156# print(syn1)
157+ # print(syn2)
130158# print(l2_error)
131159# print(l2_delta)
160+ # print(l1_error)
161+ # print(l1_delta)
132162
133163coef += 1
164+ imageInd = 3 * coef + remainder
134165
135166
136167
@@ -147,7 +178,9 @@ def sgdCreateNetworkAndTrain(self):
147178#print("this is inlay input in sgdCreateNetworkAndTrain",inlay_inputs)
148179self .inlay = InputLayer (inlay_inputs , 2 )
149180#print("self inlay in sgdCreateNetworkAndTrain" , self.inlay)
181+ #print("size inlay_inputs: ",len(self.inlay.getOutput()))
150182self .input_layer_outputs = self .inlay .getOutput ()
183+ #print("size input_layer_outputs: ",len(self.input_layer_outputs))
151184#print("input layer out in sgdCreateNetworkAndTrain: " , self.input_layer_outputs)
152185self .hidlay = HiddenLayer (HIDDEN_LAYER , self .input_layer_outputs )
153186#print("hidden layer out in sgdCreateNetworkAndTrain: " , self.hidlay)
0 commit comments