Skip to content

Commit 80e7493

Browse files
committed
merge new stuff
2 parents 836c7ad + 8f3d497 commit 80e7493

File tree

5 files changed

+106
-9
lines changed

5 files changed

+106
-9
lines changed

hidden_layer.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,30 @@ def calcOutputs(self):
2626
for n in self.neurons:
2727
self.outputValues.append(n.calc_output())
2828

29+
#
30+
# set Weight Updated
31+
#
32+
# def setWeightsUpdated(self , updatedWeights):
33+
# #TODO: testing...
34+
# x = len(self.getWeights())
35+
# updatedWeightsTemp = updatedWeights[:x]
36+
# # if updatedWeights is numpy delete is false
37+
# del updatedWeights[:x]
38+
# for n in self.neurons:
39+
# n.updatedWeights(updatedWeightsTemp)
40+
#
41+
#
42+
#
43+
2944
def getOutput(self):
3045
return self.outputValues
3146

47+
def getWeights(self):
48+
li = []
49+
for n in self.neurons:
50+
li.extend(n.getWeights())
51+
return np.asarray(li)
52+
3253

3354
def setNewInput(self, newInput):
3455
#print("len of newInput in hiddenLayer",len(newInput))

input_layer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ def calcOutputs(self):
4242
def getOutput(self):
4343
return self.outputValues
4444

45+
#
46+
# set weight updated
47+
#
48+
# def setWeightsUpdated(self , updatedWeights):
49+
# #TODO: testing...
50+
# x = len(self.getWeights())
51+
# updatedWeightsTemp = updatedWeights[:x]
52+
# # if updatedWeights is numpy delete is false
53+
# updatedWeights = np.array([:x])
54+
# for n in self.neurons:
55+
# n.updatedWeights(updatedWeightsTemp)
56+
#
57+
#
58+
#
59+
60+
def getWeights(self):
61+
li = []
62+
for n in self.neurons:
63+
li.extend(n.getWeights())
64+
return np.asarray(li)
65+
4566
def setNewInput(self, newInput):
4667
#print ("newInput in input layer: " , newInput)
4768
self.input_values = newInput
@@ -61,5 +82,6 @@ def setNewInput(self, newInput):
6182
n.setNewInput(inputval)
6283
#print("inputval in input layer: " , inputval)
6384
self.calcOutputs()
85+
#print("size of output of set new out in input layer: " , len(self.getOutput()))
6486
return self.getOutput()
6587

neural_network.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ def calcMaxIndex(self, li):
7171

7272

7373
def calcOutput(self, x):
74+
#print("Here")
75+
#print(x)
7476
x = int(x)
77+
#print("x -> ",x)
7578
self.output = {0:'A', 1:'B', 2:'C', 3:'D', 4:'E', 5:'F', 6:'G', 7:'H', 8:'I', 9:'J'}[x]
7679
return self.output
7780

@@ -87,6 +90,10 @@ def trainNetwork(self):
8790
def getOutput(self):
8891
return self.output
8992

93+
# def getOutForInput(self , x):
94+
# if self.input_layer_outputs[x] == 'A':
95+
# return
96+
9097

9198
def sgdTrain(self):
9299
print("In training")
@@ -107,30 +114,54 @@ def sgdTrain(self):
107114
#print("len of nn_inputs: " , len(self.nn_inputs))
108115
while(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])))
113120
self.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:)")
117125
self.hidden_layer_outputs = self.hidlay.setNewInput(self.input_layer_outputs)
118126
#print ("out hidden layer: " , self.hidden_layer_outputs)
119127
self.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)))
122130
l2_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)))
124132
l2_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

133163
coef +=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)
148179
self.inlay = InputLayer(inlay_inputs, 2)
149180
#print("self inlay in sgdCreateNetworkAndTrain" , self.inlay)
181+
#print("size inlay_inputs: ",len(self.inlay.getOutput()))
150182
self.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)
152185
self.hidlay = HiddenLayer(HIDDEN_LAYER, self.input_layer_outputs)
153186
#print("hidden layer out in sgdCreateNetworkAndTrain: " , self.hidlay)

neuron.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ def calc_output(self):
2727
self.calc_activation()
2828
return self.output
2929

30+
def updateWeights(self, updatedWeights):
31+
#TODO: delete one elements
32+
self.weights = np.array([])
33+
np.copyto(self.weights , updatedWeights)
34+
print("weightsupdated" , self.weights)
35+
3036
def setNewInput(self, newInput):
3137
cnt = 0
3238
#print ("newInput in Neuron: " ,newInput)
@@ -45,6 +51,8 @@ def setNewInput(self, newInput):
4551
#for i in range (0,newInput.length):
4652
#cnt +=1
4753
#print(cnt)
54+
self.pixel_values = np.array([])
55+
#print("pixel" , self.pixel_values)
4856
self.pixel_values = np.asarray(newInput)
4957
#print(len(newInput))
5058
#print(self.pixel_values.size)

output_layer.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,28 @@ def calcOutputs(self):
2424
for n in self.neurons:
2525
self.outputValues.append(n.calc_output())
2626

27+
#
28+
# set Weight updated
29+
#
30+
# def setWeightsUpdated(self , updatedWeights):
31+
# #TODO: testing...
32+
# x = len(self.getWeights())
33+
# updatedWeightsTemp = updatedWeights[:x]
34+
# # if updatedWeights is numpy delete is false
35+
# del updatedWeights[:x]
36+
# for n in self.neurons:
37+
# n.updatedWeights(updatedWeightsTemp)
38+
#
39+
#
40+
#
41+
2742
def getOutput(self):
2843
return self.outputValues
2944

3045
def setNewInput(self,newInput):
3146
self.input_values = newInput
3247
for n in self.neurons:
3348
n.setNewInput(self.input_values)
34-
35-
3649
self.calcOutputs()
3750
return self.getOutput()
3851

0 commit comments

Comments
 (0)