-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainer.py
More file actions
53 lines (38 loc) · 1.23 KB
/
trainer.py
File metadata and controls
53 lines (38 loc) · 1.23 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
from keras.api.datasets import mnist
import numpy as np
import pickle
from matplotlib import pyplot
from net import network
# loading the dataset
(train_X, train_y), (test_X, test_y) = mnist.load_data()
sampleSize = 100
learnRate = 5
trainingLength = len(train_X)
net = network(28 * 28, 3, 16, 10)
wbVec = np.load("mnist_16x16x16_model.npy")
net.initWeightsBiases(wbVec)
nablaLength = 0
for layer in range(0, net.numH + 1):
weightShape = np.shape(net.weights[layer])
nablaLength += weightShape[0] * weightShape[1] + len(net.biases[layer])
leftInd = 0
rightInd = 0
while leftInd < trainingLength:
rightInd = leftInd + sampleSize
if rightInd > trainingLength:
break
nabla = np.zeros(nablaLength)
costSum = 0
for i in range(leftInd, rightInd):
imageVec = np.array(train_X[i]).flatten()
imageVec = np.divide(imageVec, 255)
nab, cost = net.getNabla(imageVec, train_y[i])
nabla = np.add(nabla, nab)
costSum += cost
nabla = np.divide(nabla, (rightInd - leftInd))
costSum = costSum / (rightInd - leftInd)
net.learn(nabla, learnRate)
print(rightInd, ": ", costSum)
costSum = 0
leftInd = rightInd
np.save("mnist_16x16x16_model", net.getNetWeightBiasVec())