-
Notifications
You must be signed in to change notification settings - Fork 6
/
mnist-test.py
132 lines (92 loc) · 4.07 KB
/
mnist-test.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
import numpy as np
import lib.mlputils as mlputils
import lib.trainingutils as tu
from lib.mlp import NeuralNetwork
from lib.mlputils import RegularizationL2
from lib.mlputils import RegularizationL1
from mnist import MNIST
def train_model(nn, dataset, eta, epoches, n_folds, reg_lmbda, debug=False):
performance ={"acc":[],
"cost":[],
}
# dataset.shape[0]
for j in range(epoches):
cost_epoch=0
acc_epoch=0
kfold = tu.k_fold(n_folds, dataset_size)
if debug:
print("*********************************************")
print("Epoch {}".format(j))
print("*********************************************")
for i in range(n_folds):
cost, accuracy = tu.train_and_validate_kfold(nn=mlp,
dataset=dataset,
folds=kfold,
validation_fold=i,
eta=eta,
reg_lmbda=reg_lmbda)
if debug:
print("Fold[{}]: Cost={}, Accu={}".format(i, cost, accuracy))
cost_epoch += cost
acc_epoch += accuracy
if debug:
print("_____________________________________________")
print(">>> Cost:{}, Acc: {}".format(cost_epoch/n_folds, acc_epoch/n_folds))
print("_____________________________________________")
performance["acc"].append(acc_epoch/n_folds)
performance["cost"].append(cost_epoch/n_folds)
return performance
if __name__ == "__main__":
print("MLP Test using MNIST Data Set")
dirname = "./mnist-dataset"
mndata = MNIST(dirname, return_type='numpy')
training, labels = mndata.load_training()
training_size = training.shape[0]
input_size = training.shape[1]
hidden_size = 100
output_size = 10
dataset = np.zeros((training_size, input_size+output_size))
# Example:
# Output Layer for class 4 = [0,0,0,0,1,0,0,0,0,0]
# Output Layer for class 5 = [0,0,0,0,0,1,0,0,0,0]
# Output Layer for class 0 = [1,0,0,0,0,0,0,0,0,0]
for i in range(training_size):
l = mlputils.get_one_zero_nn_output(output_size, labels[i])
if l is not None:
dataset[i] = np.append(training[i]/255, l)
# print("======= Dataset =========\n{}".format(dataset[2]))
max_col = np.amax(dataset, axis=0)
min_col = np.amin(dataset, axis=0)
# dataset = dataset/255.0
# print("MAX: {}, MIN: {}".format(max_col, min_col))
nn_size = [input_size, hidden_size, output_size]
# dataset_size = dataset.shape[0]
dataset_size = 100
mlp = NeuralNetwork(layer_size=nn_size)
# randomly sort the dataset. So we
# avoid selecting alway the first examples
# in case we are using only a subset of dataset.
examples_used = np.random.permutation(
np.arange(dataset_size))
performance_training = train_model(nn=mlp,
dataset=dataset[examples_used],
eta=0.1,
epoches=5,
n_folds=5,
reg_lmbda=0.01,
debug=True)
print("Performance:\n{}".format(performance_training))
print("Testing the Model trained...")
test, test_labels = mndata.load_testing()
dataset_test = np.zeros((test.shape[0], input_size+output_size))
for i in range(test.shape[0]):
tl = mlputils.get_one_zero_nn_output(output_size, test_labels[i])
if tl is not None:
dataset_test[i] = np.append(test[i]/255, tl)
acc_validation = tu.validate_model(nn=mlp, dataset=dataset_test)
print(">> Validation Dataset Accuracy: {}".format(acc_validation))
mlp.save("model.json")
mlp2 = NeuralNetwork()
mlp2.load("model.json")
acc_validation = tu.validate_model(nn=mlp2, dataset=dataset_test)
print(">> Validation Dataset Accuracy: {}".format(acc_validation))