-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_nn.py
172 lines (157 loc) · 5.79 KB
/
mnist_nn.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
import sys; args = sys.argv[1:]
import math
import random
import re
import pandas as pd
import cv2
import numpy as np
import matplotlib.pyplot as plt
def function(x): return 1/(1+(math.e**(-x)))
def derivative(x): return (1-x)*x
def dot(x,y): return sum([x[i]*y[i] for i in range(len(x))])
def backProp(x,ogweights,outputs):
alpha = 0.1
error = [[0 for a in n] for n in x]
weights = [[weight for weight in og] for og in ogweights]
outputLayer = [output - x[-2][a]*weights[-1][a] for a,output in enumerate(outputs)] #output error t-y
lastError = [outputLayer[i]*ogweights[-1][i]*derivative(x[-2][i]) for i in range(len(outputs))] #(output error)*finalweight*derivative(xfinal)
lastGrad = [outputLayer[i]*(x[-2][i]) for i in range(len(outputs))] #(output error)*finalweight*xfinal
for i in range(len(weights[-1])):
weights[-1][i] += alpha*lastGrad[i] #update the weight
error[-1][i] = outputLayer[i] #update the output layer
error[-2][i] = lastError[i] #update the last layer
for layer in range(len(x)-3,-1,-1): # A1 B1 C1 A2 B2 C2
for i in range(len(x[layer])): #leftmost index
Ei = 0
for j in range(len(x[layer+1])):
neggradient = x[layer][i]*error[layer+1][j]
Ei +=weights[layer][j*len(x[layer])+i]*error[layer+1][j]
weights[layer][j*len(x[layer])+i] += alpha * neggradient #update the weights (weight+alpha*gradient)
error[layer][i] = Ei*derivative(x[layer][i])
return weights
def ff(NN,weights):
for layer in range(0,len(NN)-1):
if layer == len(NN)-2:
for i in range(len(NN[layer+1])):
NN[layer+1][i] = weights[layer][i]*NN[layer][i]
else:
for i in range(len(NN[layer+1])):
NN[layer+1][i] = function(dot(NN[layer],weights[layer][i*(len(NN[layer])):i*(len(NN[layer]))+len(NN[layer])]))
return NN
def makeNN(inputs,weights,outputs):
NN = [[] for i in range(len(weights))]
NN[0] = [input for input in inputs]
NN[1] = [0 for i in range(32)]
NN[2] = [0 for i in range(len(outputs))]
NN.append([0 for i in range(len(outputs))])
return NN
def grfParse():
import csv
import random
import numpy as np
inputs = []
final = [[None, None] for _ in range(60000)] #inputs, outputs, weights
with open('mnist_train.csv', 'r') as csvfile:
csv_reader = csv.reader(csvfile)
first = True
i = 0
for row in csv_reader:
if first: #skip the header
first = False
continue
inputs = [int(a)/255.0 for a in row[1:]]
inputs.append(1) #add the bias
final[i][0] = inputs # inputs
outputs = [1 if j == (int)(row[0]) else 0 for j in range(10)]
final[i][1] = outputs # outputs
i += 1
weights = [
[random.uniform(-0.5,0.5) for a in range(785*32)],
[random.uniform(-0.5,0.5) for a in range(10*32)],
[random.uniform(-0.5,0.5) for a in range(len(outputs))]
]
return final,weights
def validationParse():
import csv
import random
import numpy as np
inputs = []
final = [[None, None] for _ in range(10000)] #inputs, outputs, weights
with open('mnist_test.csv', 'r') as csvfile:
csv_reader = csv.reader(csvfile)
first = True
i = 0
for row in csv_reader:
if first: #skip the header
first = False
continue
inputs = [int(a)/255.0 for a in row[1:]]
inputs.append(1) #add the bias
final[i][0] = inputs # inputs
outputs = [1 if j == (int)(row[0]) else 0 for j in range(10)]
final[i][1] = outputs # outputs
i += 1
with open("output.txt", "r") as file:
line = file.readline()
weights = [[] for a in range(3)]
count = 0
while line:
print(count)
line = line.strip()
temp_weights = line.split(" ")
weights[count] = ([float(a) for a in temp_weights])
line = file.readline()
count+=1
return final,weights
def main():
global alpha
alpha = 0.2
NNlist,weights = grfParse()
accuracy_history = []
epochs = 8
for i in range(epochs):
error_count = 0
for a,nn in enumerate(NNlist):
inputs = nn[0]
outputs = nn[1]
NN = makeNN(inputs, weights, outputs)
NN = ff(NN, weights)
outputValue = max(NN[-1])
if(outputs.index(1) != NN[-1].index(outputValue)):
error_count+=1
weights = backProp(NN, weights, outputs)
accuracy = (len(NNlist)-error_count)/len(NNlist)
accuracy_history.append(accuracy)
temp = "Layer counts "
for a in NN:
temp+=str(len(a))+" "
print(temp)
with open('output.txt', 'w') as file:
for a in weights:
tempstr = ""
for x in a:
tempstr += str(x)+" "
file.write(tempstr+"\n")
print("Training accuracy over epochs")
print(accuracy_history)
NNlist,weights = validationParse()
error_count = 0
for a,nn in enumerate(NNlist):
inputs = nn[0]
outputs = nn[1]
NN = makeNN(inputs, weights, outputs)
NN = ff(NN, weights)
outputValue = max(NN[-1])
if(outputs.index(1) != NN[-1].index(outputValue)):
error_count+=1
accuracy = (len(NNlist)-error_count)/len(NNlist)
print("Validation accuracy: ")
print(accuracy)
x = [a+1 for a in range(epochs)]
plt.plot(x,accuracy_history)
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Training Accuracy over Epochs')
plt.show()
if __name__ == "__main__":
main()