-
Notifications
You must be signed in to change notification settings - Fork 2
/
neural_network_digit_recognition_group_proj.py
148 lines (101 loc) · 4.03 KB
/
neural_network_digit_recognition_group_proj.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
from random import randint, shuffle
import os
from numpy import *
import numpy as np
from pybrain.datasets import SupervisedDataSet
from pybrain.structure import SigmoidLayer, SoftmaxLayer
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.tools.shortcuts import buildNetwork
from pybrain.tools.xml import NetworkReader
from pybrain.tools.xml import NetworkWriter
from pybrain.utilities import percentError
from scipy import io
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt2
def plotData(image):
# plots the input data '''
# the image matrix will have to be transposed to be viewed correctly
# cmap shows the color map
plt.imshow(image.T, cmap='Greys')
plt.show()
def convert_to_one_of_many(y):
# converts Y to one of many types
# or one output per label
rows, cols = y.shape
classes = unique(y).size # should get 10 classes
new_y = zeros((rows, classes))
for i in range(0, rows):
new_y[i, y[i]] = 1
return new_y
# load the MNIST data
digits = io.loadmat('data_mnist.mat')
# making X and Y numpy arrays
X = digits['X']
Y = digits['y']
Y[Y == 10] = 0 # 0 has the 10th position, this line gives it the 0th position
num_of_labels = unique(Y).size # gets your 10 labels/outputs
# build the dataset
num_of_examples, size_of_example = X.shape
# convert the test data to one of many (10)
Y = convert_to_one_of_many(Y) # splits the data into 10 outputs
# separating training and test data sets
X1 = hstack((X, Y)) # puts into a single one dimensional array
shuffle(X1) # shuffles the data
X = X1[:, 0:size_of_example]
Y = X1[:, size_of_example: X1.shape[1]]
# add the contents of digits to a dataset
train_data = SupervisedDataSet(size_of_example, num_of_labels)
test_data = SupervisedDataSet(size_of_example, num_of_labels)
data_split = int(num_of_examples * 0.9)
for i in range(0, data_split):
train_data.addSample(X[i, :], Y[i, :]) #add .9 of the data to the ds
# setting the field names
train_data.setField('input', X[0:data_split, :])
train_data.setField('target', Y[0:data_split, :])
for i in range(data_split, num_of_examples):
test_data.addSample(X[i, :], Y[i, :])
test_data.setField('input', X[data_split:num_of_examples, :])
test_data.setField('target', Y[data_split:num_of_examples, :])
#if os.path.isfile('extra_layers.xml'):
#net = NetworkReader.readFrom('extra_layers.xml')
#else:
net = buildNetwork(size_of_example, int(size_of_example), int(size_of_example) / 5, num_of_labels,
hiddenclass=SigmoidLayer,
outclass=SoftmaxLayer)
net.sortModules()
test_index = randint(0, X.shape[0])
test_input = X[test_index]
real_train = train_data['target'].argmax(axis=1)
real_test = test_data['target'].argmax(axis=1)
EPOCHS = 3
trainer = BackpropTrainer(net, dataset=train_data, momentum=0.41, learningrate=0.03, verbose=False)
trainResultArr = []
epochs = []
testResultArr = []
for i in range(EPOCHS):
# set the epochs
trainer.trainEpochs(1)
outputTrain = net.activateOnDataset(train_data)
outputTrain = outputTrain.argmax(axis=1) # takes the output with the largest prob
trainResult = percentError(outputTrain, real_train)
outputTest = net.activateOnDataset(test_data)
outputTest = outputTest.argmax(axis=1)
testResult = percentError(outputTest, real_test)
finalTrainResult = 100 - trainResult
finalTestResult = 100 - testResult
print "Epoch: " + str(i + 1) + "\tTraining set accuracy: " + str(finalTrainResult) + "\tTest set accuracy: " + str(
finalTestResult)
# getStatistics( )
trainResultArr.append(finalTestResult)
testResultArr.append(finalTrainResult)
epochs.append(i)
prediction = net.activate(test_input)
# returns the index of the highest value down the columns
p = argmax(prediction, axis=0)
NetworkWriter.writeToFile(net, 'extra_layers.xml')
plt.plot(epochs, trainResultArr)
plt.plot(epochs, testResultArr)
plt.title('Training Result (Orange) vs Test Result of ANN (Blue)')
plt.xlabel('Epochs')
plt.ylabel('Accuracy %')
plt.show()