-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitPerceptron.py
218 lines (176 loc) · 7.68 KB
/
DigitPerceptron.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import logging
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
import time
from evaluation import calc_accuracy, confusion_matrix
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
DATA_DIR = './data'
class DigitPerceptron:
def __init__(self):
logger.info('2/1+t')
self.epochs = 20
self.alpha = 1.0
self.num_classes = 10
self.col = 28
self.row = 28
self.bias = False
self.random_weights = True
if (self.random_weights):
self.feature_weight_vectors = np.random.uniform(-2, 2, (self.num_classes, self.row, self.col))
else:
self.feature_weight_vectors = np.zeros((self.num_classes, self.row, self.col))
if (self.bias):
self.bias_val = 1
self.bias_weight = np.zeros(self.num_classes)
def decay_alpha(self, t):
self.alpha = self.alpha / t
def train_decision(self, model):
dot_products = [0]*self.num_classes
for i in range(self.num_classes):
for y in range(self.row):
for x in range(self.col):
dot_products[i] += model[y][x] * self.feature_weight_vectors[i][y][x]
if self.bias:
dot_products[i] += self.bias_weight[i] * self.bias_val
return np.argmax(dot_products)
def update_weights(self, decision, label, model):
for y in range(self.row):
for x in range(self.col):
self.feature_weight_vectors[decision][y][x] -= self.alpha * model[y][x]
self.feature_weight_vectors[label][y][x] += self.alpha * model[y][x]
if self.bias:
self.bias_weight[decision] -= self.alpha * self.bias_val
self.bias_weight[label] += self.alpha * self.bias_val
def sorted_train(self):
training_label_path = DATA_DIR + '/traininglabels'
training_images_path = DATA_DIR + '/trainingimages'
labels = []
with open(training_label_path) as f:
for line in f:
labels.append(int(line))
sorted_label_index = np.argsort(labels).tolist()
num_images = len(labels)
training_images = [[None for _ in range(self.row)] for _ in range(num_images)]
with open(training_images_path) as f:
for n in range(num_images):
for y in range(self.row):
training_images[n][y] = list(f.readline().rstrip('\n'))
start_time = time.time()
for t in range(1, self.epochs + 1):
self.decay_alpha(t)
total = 0
correct = 0
for n in sorted_label_index:
model = np.zeros((self.row,self.col))
total += 1
for y in range(self.row):
for x in range(self.col):
if training_images[n][y][x] in ['+', '#']:
model[y][x] = 1
decision = self.train_decision(model)
if decision != labels[n]:
self.update_weights(decision, labels[n], model)
else:
correct += 1
accuracy = 100*correct/total
logger.info('Correct: {0}, Total: {1}'.format(correct,total))
logger.info('Accuracy for epoch {0} is {1:.2f}%'.format(t, accuracy))
logger.info('Finished training in {0:.2f} seconds'.format(time.time() - start_time))
def train(self):
training_label_path = DATA_DIR + '/traininglabels'
training_images_path = DATA_DIR + '/trainingimages'
labels = []
with open(training_label_path) as f:
for line in f:
labels.append(int(line))
num_images = len(labels)
training_images = [[None for _ in range(self.row)] for _ in range(num_images)]
with open(training_images_path) as f:
for n in range(num_images):
for y in range(self.row):
training_images[n][y] = list(f.readline().rstrip('\n'))
start_time = time.time()
for t in range(1, self.epochs + 1):
self.decay_alpha(t)
total = 0
correct = 0
for n in range(len(labels)):
model = np.zeros((self.row,self.col))
total += 1
for y in range(self.row):
for x in range(self.col):
if training_images[n][y][x] in ['+', '#']:
model[y][x] = 1
decision = self.train_decision(model)
if decision != labels[n]:
self.update_weights(decision, labels[n], model)
else:
correct += 1
accuracy = 100*correct/total
logger.info('Correct: {0}, Total: {1}'.format(correct,total))
logger.info('Accuracy for epoch {0} is {1:.2f}%'.format(t, accuracy))
logger.info('Finished training in {0:.2f} seconds'.format(time.time() - start_time))
if self.bias:
logger.info(self.bias_weight)
def predict(self, info=True):
test_label_path = DATA_DIR + '/testlabels'
test_images_path = DATA_DIR + '/testimages'
correct_labels = []
with open(test_label_path) as f:
for line in f:
correct_labels.append(int(line))
num_images = len(correct_labels)
test_images = [[None for _ in range(self.row)] for _ in range(num_images)]
with open(test_images_path) as f:
for n in range(num_images):
for y in range(self.row):
test_images[n][y] = list(f.readline().rstrip('\n'))
predicted_labels = []
for n in range(num_images):
model = np.zeros((self.row,self.col))
for y in range(self.row):
for x in range(self.col):
if test_images[n][y][x] in ['+', '#']:
model[y][x] = 1
decision = self.train_decision(model)
predicted_labels.append(decision)
truths = np.array(correct_labels)
predictions = np.array(predicted_labels)
accuracy = calc_accuracy(truths, predictions)
logger.info('NB model is {0:.2f}% accurate on the digit data'.format(accuracy))
X = np.array(range(self.col))
Y = np.fliplr(np.atleast_2d(np.array(range(self.row))))[0]
if info:
confm = confusion_matrix(truths, predictions, self.num_classes)
class_accuracies = [confm[n][n] for n in range(self.num_classes)]
# Class accuracies
for n, x in enumerate(class_accuracies):
logger.info('Class {0} has an accuracy of {1:.2f}%'.format(n, 100 * x))
# Confusion matrixx
plt.figure()
plt.imshow(confm, cmap=plt.get_cmap('Greens'), interpolation='nearest')
plt.title('Confusion Matrix')
plt.xticks(np.arange(self.num_classes))
plt.yticks(np.arange(self.num_classes))
plt.xlabel('Predictions')
plt.ylabel('Truths')
X, Y = np.meshgrid(range(self.col), range(self.row))
Y = Y[::-1]
for i in range(self.num_classes):
hf = plt.figure()
ha = hf.gca(projection = '3d')
ha.plot_surface(X, Y, self.feature_weight_vectors[i], rstride=1, cstride=1,
linewidth=0, cmap=cm.coolwarm, antialiased = False)
ha.set_xlabel('X')
ha.set_ylabel('Y')
ha.set_zlabel('weigh')
plt.show()
def main():
dpt = DigitPerceptron()
dpt.train()
dpt.predict()
if __name__ == '__main__':
main()