forked from TinyPandar/VisualClassifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestMLP.py
102 lines (79 loc) · 3.04 KB
/
TestMLP.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
import MLP
import os
import struct
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_openml
# def load_mnist(path, kind='train'):
# labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind)
# images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind)
# with open(labels_path, 'rb') as lbpath:
# magic, n = struct.unpack('>II', lbpath.read(8))
# labels = np.fromfile(lbpath, dtype=np.uint8)
# with open(images_path, 'rb') as imgpath:
# magic, num, rows, cols = struct.unpack(">IIII", imgpath.read(16))
# images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784)
# return images, labels
# # print(os.listdir("../archive"))
# X_train, y_train = load_mnist('../archive', kind='train')
# X_test, y_test = load_mnist('../archive', kind='t10k')
from sklearn.datasets import fetch_openml
mnist = fetch_openml('mnist_784')
X, y = mnist["data"].to_numpy(), mnist["target"].to_numpy()
X_train, X_test, y_train, y_test = X[:60000], X[60000:], y[:60000], y[60000:]
# print(y_train)
# print(y_test)
y_train = y_train.astype(np.int)
y_test = y_test.astype(np.int)
print(y_train)
print(y_test)
nn = MLP.NeuralNetMLP(n_output=10,
n_features=X_train.shape[1],
n_hidden=50,
l2=0.1,
l1=0.1,
epochs=100,
eta=0.001,
alpha=0.001,
decrease_const=0.00001,
minibatches=50,
shuffle=True,
random_state=1)
nn.fit(X_train, y_train, print_progress=True)
plt.plot(range(len(nn.cost_)), nn.cost_)
plt.ylim([0, 2000])
plt.ylabel('Cost')
plt.xlabel('Epochs * 50')
plt.tight_layout()
# plt.savefig('./figures/cost.png', dpi=300)
plt.show()
batches = np.array_split(range(len(nn.cost_)), 1000)
cost_ary = np.array(nn.cost_)
cost_avgs = [np.mean(cost_ary[i]) for i in batches]
plt.plot(range(len(cost_avgs)), cost_avgs, color='red')
plt.ylim([0, 2000])
plt.ylabel('Cost')
plt.xlabel('Epochs')
plt.tight_layout()
#plt.savefig('./figures/cost2.png', dpi=300)
plt.show()
y_train_pred = nn.predict(X_train)
acc = np.sum(y_train == y_train_pred, axis=0) / X_train.shape[0]
print('Training accuracy: %.2f%%' % (acc * 100))
y_test_pred = nn.predict(X_test)
acc = np.sum(y_test == y_test_pred, axis=0) / X_test.shape[0]
print('Test accuracy: %.2f%%' % (acc * 100))
miscl_img = X_test[y_test != y_test_pred][:25]
correct_lab = y_test[y_test != y_test_pred][:25]
miscl_lab = y_test_pred[y_test != y_test_pred][:25]
fig, ax = plt.subplots(nrows=5, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(25):
img = miscl_img[i].reshape(28, 28)
ax[i].imshow(img, cmap='Greys', interpolation='nearest')
ax[i].set_title('%d) t: %d p: %d' % (i+1, correct_lab[i], miscl_lab[i]))
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
# plt.savefig('./figures/mnist_miscl.png', dpi=300)
plt.show()