-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMNIST.py
More file actions
64 lines (51 loc) · 1.42 KB
/
MNIST.py
File metadata and controls
64 lines (51 loc) · 1.42 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
from neural_network import Network
import numpy as np
from activation_functions import *
import os
from keras.datasets import mnist
from matplotlib import pyplot
def predictedValue(prediction):
currentMax = -10
j = 0
for i in range(prediction[0].size):
if prediction[0][i] > currentMax:
currentMax = prediction[0][i]
j = i
return j
(train_X, train_y), (test_X, test_y) = mnist.load_data()
os.system("clear")
'''
for i in range(9):
pyplot.subplot(330 + 1 + i)
pyplot.imshow(train_X[i], cmap=pyplot.get_cmap('gray'))
pyplot.show()
'''
# zmiana X
Xtrain = np.reshape(train_X, (60000, 1, 28 * 28))
Xtest = np.reshape(test_X, (10000, 1, 28 * 28))
# zmiana Y
Ytrain = []
for y in train_y:
z = np.zeros(10)
z[y] = 1
Ytrain.append(z)
Ytrain = np.array(Ytrain)
network = Network((28 * 28, 60, 40, 20, 10),
(tanh, tanh, tanh, tanh),
(tanhDerivative, tanhDerivative, tanhDerivative, tanhDerivative))
network.chunkLearn(data=Xtrain, expected=Ytrain, chunkSize=100, iterations=1000, learningRate=0.1)
# test
print("testing started")
correct = 0
#i = 0 # a jebać ładny styl
for x, y in zip(Xtest, test_y):
predicted = predictedValue(network.forward(x))
if predicted == y:
correct += 1
'''
print(predicted)
pyplot.imshow(test_X[i], cmap=pyplot.get_cmap('gray'))
pyplot.show()
i += 1
'''
print(f'OVERALL SCORE: ', correct / test_y.size)