-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.py
More file actions
270 lines (209 loc) · 7.69 KB
/
net.py
File metadata and controls
270 lines (209 loc) · 7.69 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
import numpy as np
import random
import copy
class network:
inputSize = 28 * 28
numH = 2
hSize = 16
outputSize = 10
weights = list()
biases = list()
def __init__(self, inputSize, numHidden, hiddenSize, outputSize):
self.inputSize = inputSize
self.numH = numHidden
self.hSize = hiddenSize
self.outputSize = outputSize
self.weights = self.getInitWeights()
self.biases = self.getInitBiases()
def getInitWeights(self):
weightList = list()
for layer in range(0, self.numH + 1):
if layer == 0:
rowSize = self.inputSize
else:
rowSize = self.hSize
if layer == self.numH:
colSize = self.outputSize
else:
colSize = self.hSize
weightMat = np.zeros((colSize, rowSize))
for row in range(0, colSize):
for col in range(0, rowSize):
weightMat[row][col] = random.uniform(-0.5, 0.5)
weightList.append(weightMat)
return weightList
def getInitBiases(self):
biasList = list()
for layer in range(0, self.numH + 1):
if layer == self.numH:
size = self.outputSize
else:
size = self.hSize
biasVec = np.zeros(size)
for row in range(0, size):
biasVec[row] = random.uniform(-0.5, 0.5)
biasList.append(biasVec)
return biasList
def forwardProp(self, input):
output = input
activations = list()
z = list()
activations.append(np.copy(output))
for layer in range(0, self.numH):
output = np.matmul(self.weights[layer], output)
output = np.add(output, self.biases[layer])
z.append(np.copy(output))
output = np.vectorize(network.Sigmoid)(output)
activations.append(np.copy(output))
output = np.matmul(self.weights[self.numH], output)
output = np.add(output, self.biases[self.numH])
z.append(np.copy(output))
output = network.Softmax(output)
activations.append(np.copy(output))
return activations, z
def cost(output, answer):
sum = 0
for i in range(0, len(output)):
sum += np.power((output[i] - (1 if i == answer else 0)), 2)
return sum
def getNabla(self, input, answer):
activations, z = self.forwardProp(input)
weightsList = copy.deepcopy(self.weights)
biasesList = copy.deepcopy(self.biases)
dcdaPrev = list()
# output layer
for row in range(0, self.outputSize):
dcda = 2 * (activations[-1][row] - (1 if row == answer else 0))
dadz = network.dSoftmax(activations[-1][row])
for col in range(0, self.hSize):
dzdw = activations[-2][col]
weightsList[-1][row][col] = dzdw * dadz * dcda
biasesList[-1][row] = dadz * dcda
for k in range(0, self.hSize):
sum = 0
for j in range(0, self.outputSize):
dcda = 2 * (activations[-1][j] - (1 if j == answer else 0))
dadz = network.dSoftmax(activations[-1][j])
dcdak = self.weights[-1][j][k]
sum = sum + dcdak * dadz * dcda
dcdaPrev.append(sum)
sum = 0
# hidden layers
for layer in range(-2, -(self.numH + 1), -1):
for row in range(0, self.hSize):
dcda = dcdaPrev[row]
dadz = network.dSigmoid(z[layer][row])
for col in range(0, self.hSize):
dzdw = activations[layer - 1][col]
weightsList[layer][row][col] = dzdw * dadz * dcda
biasesList[layer][row] = dadz * dcda
dcdaPrevNew = list()
for k in range(0, self.hSize):
sum = 0
for j in range(0, self.hSize):
dcda = dcdaPrev[j]
dadz = network.dSigmoid(activations[layer][j])
dcdak = self.weights[layer][j][k]
sum = sum + dcdak * dadz * dcda
dcdaPrevNew.append(sum)
sum = 0
dcdaPrev = dcdaPrevNew
# first hidden
for row in range(0, self.hSize):
dcda = dcdaPrev[row]
dadz = network.dSigmoid(z[0][row])
for col in range(0, self.inputSize):
dzdw = input[col]
weightsList[0][row][col] = dzdw * dadz * dcda
biasesList[0][row] = dadz * dcda
return self.getWeightBiasVec(weightsList, biasesList), network.cost(
activations[-1], answer
)
def learn(self, nabla, learnRate):
wbVec = self.getNetWeightBiasVec()
wbVec = np.add(np.multiply(nabla, -learnRate), wbVec)
self.initWeightsBiases(wbVec)
return
def ReLU(i):
return np.maximum(0, i)
def Sigmoid(n):
return 1 / (1 + np.exp(-n))
def dSigmoid(n):
return np.exp(-n) / np.power((1 + np.exp(-n)), 2)
def Softmax(vector):
max = np.max(vector)
adjustedVec = np.subtract(vector, max)
sum = np.sum(np.exp(adjustedVec))
return np.exp(adjustedVec) / sum
def dSoftmax(n):
return n * (1 - n)
def getyVec(n):
arr = np.zeros(10)
arr[n - 1] = 1
return arr
# **************** Util *******************
def getNetWeightBiasVec(self):
vec = list()
# weights
for wMat in self.weights:
for wRow in wMat:
for w in wRow:
vec.append(w)
for bVec in self.biases:
for b in bVec:
vec.append(b)
return np.array(vec)
def getWeightBiasVec(self, weights, biases):
vec = list()
# weights
for wMat in weights:
for wRow in wMat:
for w in wRow:
vec.append(w)
for bVec in biases:
for b in bVec:
vec.append(b)
return np.array(vec)
def initWeightsBiases(self, wbVec):
ind = 0
# read input weights
for row in range(0, self.hSize):
for col in range(0, self.inputSize):
self.weights[0][row][col] = wbVec[ind]
ind += 1
# read hidden layer weights
for hLayer in range(1, self.numH):
for row in range(0, self.hSize):
for col in range(0, self.hSize):
self.weights[hLayer][row][col] = wbVec[ind]
ind += 1
# read pre output layer weights
for row in range(0, self.outputSize):
for col in range(0, self.hSize):
self.weights[self.numH][row][col] = wbVec[ind]
ind += 1
# read hidden layer biases
for layer in range(0, self.numH):
for row in range(0, self.hSize):
self.biases[layer][row] = wbVec[ind]
ind += 1
# read output layer biases
for row in range(0, self.outputSize):
self.biases[self.numH][row] = wbVec[ind]
ind += 1
net = network(1, 2, 1, 1)
print(net.getNetWeightBiasVec())
net.getNabla(np.array([0]), 0)
print(net.getNetWeightBiasVec())
# for i in range(0, 5):
# net.learn(net.getNabla(np.array([0.5]), 1), 0)
# print(net.weights)
# print(net.inputSize, net.numH, net.hSize, net.outputSize)
# print(net.weights, "\n")
# print(net.biases, "\n")
# np.save("mnist_16x16_model", net.getWeightBiasVec())
# wbVec = np.load('mnist_16x16_model.npy')
# print(wbVec,"\n")
# net.initWeightsBiases(wbVec)
# print(net.weights, "\n")
# print(net.biases, "\n")