-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
291 lines (207 loc) · 9.82 KB
/
training.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'''
Yes
'''
import numpy as np
from numpy import ndarray
from feature_extraction import get_image_features
from helpers import extract_images, get_black_white
# pylint: disable=invalid-name
##########################################################################
# #
# START: Build Training, Validation, and Testing Sets #
# #
##########################################################################
def get_training_data(VERBOSE: bool = False) -> ndarray:
'''
Returns a `9990x12` set of `training_data`.
Builds `training_data` from the files corresponding to training
found in the 'train_and_valid' folder.
Each row of the `training_data` corresponds to a handwritten digit.
- row[0:9] contains the 9 `feature` values.
- row[10] contains the `threshold` value.
- row[11] contains the `class label`.
'''
if VERBOSE:
print(f'\n{"-" * 70}')
print('Building Training Set...')
training_data = []
class_labels = []
# pylint: disable-next=unused-variable
for i in range(NUM_FILES := 10):
FILENAME = f'input_files/training_data/handwritten_samples_{i}.csv'
IMAGES, LABELS = extract_images(file=FILENAME)
for label in LABELS:
class_labels.append([label])
if VERBOSE:
print(f'\tComputing Feature Values in < {FILENAME} >')
for image in IMAGES:
binary_image = get_black_white(image)
training_data.append(get_image_features(binary_image))
# Create Column 11 of threshold values = -1.
# 9,990 instead of 10,000 because we dropped the 10 labels.
THRESHOLDS = np.full(shape=(9990, 1), fill_value=-1)
# Concatenate Threshhold Column to TRAIN
training_data = np.concatenate((training_data, THRESHOLDS), axis=1)
# Concatenate Label Column to TRAIN
training_data = np.concatenate((training_data, class_labels), axis=1)
np.random.shuffle(training_data)
assert isinstance(training_data, ndarray)
return training_data
def get_validation_data(VERBOSE: bool = False) -> ndarray:
'''
Returns a `2490x12` set of `validation_data`.
Builds `validation_data` from the files corresponding to validation
found in the 'train_and_valid' folder.
'''
if VERBOSE:
print(f'\n{"-" * 70}')
print('Building Validation Set...')
validation_data = []
class_labels = []
# pylint: disable-next=unused-variable
for i in range(NUM_FILES := 10):
FILENAME = f'input_files/validation_data/handwritten_samples_{i}.csv'
IMAGES, LABELS = extract_images(file=FILENAME)
for label in LABELS:
class_labels.append([label])
if VERBOSE:
print(f'\tComputing Feature Values in < {FILENAME} >')
for image in IMAGES:
binary_image = get_black_white(image)
validation_data.append(get_image_features(binary_image))
# Create Column 11 of threshold values = -1.
# 9,990 instead of 10,000 because we dropped the 10 labels.
thresh_arr = np.full(shape=(2490, 1), fill_value=-1)
# Concatenate Threshhold Column to TRAIN")
validation_data = np.concatenate((validation_data, thresh_arr), axis=1)
# Concatenate Labels Column to TRAIN")
validation_data = np.concatenate((validation_data, class_labels), axis=1)
# Randomly Permuting Rows of Training Data
np.random.shuffle(validation_data)
assert isinstance(validation_data, ndarray)
return validation_data
def get_testing_data(FILE: str, VERBOSE: bool = False) -> ndarray:
'''
Returns a `len(file) x 11` set of `testing_data`.
Builds `testing_data` from the files corresponding to testing
found in the 'test' folder.
'''
assert isinstance(FILE, str)
if VERBOSE:
print(f'\n{"-" * 70}')
print('Building Testing Set...')
testing_data = []
IMAGES, _ = extract_images(FILE, has_label=False)
for image in IMAGES:
binary_image = get_black_white(image)
testing_data.append(get_image_features(binary_image))
THRESHOLDS = np.full(shape=(len(IMAGES), 1), fill_value=-1)
testing_data = np.concatenate((testing_data, THRESHOLDS), axis=1)
assert isinstance(testing_data, ndarray)
return testing_data
##########################################################################
# #
# END: Build Training, Validation, and Testing Sets #
# #
##########################################################################
##########################################################################
# #
# START: Train, Validate, and Test Weights #
# #
##########################################################################
def train(weight_vectors: ndarray, epochs: int) -> tuple[int, int, int]:
'''
- Paramters: An array of 'weight_vectors' to train and the number of
'epochs' to train the weight_vectors over.
- Returns: An ndarray of 'weight_vectors' that have been processed / trained
over the specified number of epochs and determined to be the most promising
for use in classification.
- Prediction Method: argmax {wk, xk} for all k labels / weight vectors.
- Adjustment Method:
- weight_vectors[j] += np.multiply(η, x[0:10])
- weight_vectors[predict] -= np.multiply(η, x[0:10])
- Description: This method will generate the training data and validation
data to train the 'weight_vectors' with.
For each image in the training data, this method will make a prediction
using that image's features and a current set of weights to associate with
those features. If the prediction is correct, our algorithm marks it as a
successful prediction and moves on. If the prediction is incorrect, the
algorithm will count it as an error and increase the weights associated with
the actual digit's feature values while decreasing those of the incorrectly
predicted digit.
This will occur for each epoch up to the specified number of 'epochs',
storing the weight_vectors after each epoch. Then, weight_vectors associated
with the least amount of errors is selected as the 'best_weights' found and
to be returned.
'''
TRAIN = get_training_data(VERBOSE=True)
VALID = get_validation_data(VERBOSE=True)
weights_after_each_epoch = []
successes_for_each_epoch = []
errors_for_each_epoch = []
# pylint: disable-next=non-ascii-name
η = float(0.08) # Learning Constant, η (Eta).
for _ in range(epochs):
for row in TRAIN:
class_label = int(row[10])
features = row[0:10]
logits = [np.dot(weights, features) for weights in weight_vectors]
predicted_label = np.argmax(logits)
if predicted_label != class_label:
weight_vectors[class_label] += np.multiply(η, features)
weight_vectors[predicted_label] -= np.multiply(η, features)
SUCCESSES, ERRORS = validate(weight_vectors, VALID)
weights_after_each_epoch.append(weight_vectors.copy())
successes_for_each_epoch.append(SUCCESSES)
errors_for_each_epoch.append(ERRORS)
BEST_WEIGHTS_IDX = np.argmin(errors_for_each_epoch)
BEST_WEIGHTS = weights_after_each_epoch[BEST_WEIGHTS_IDX]
TOTAL_SUCCESSES = np.sum(successes_for_each_epoch)
TOTAL_ERRORS = np.sum(errors_for_each_epoch)
return BEST_WEIGHTS, TOTAL_SUCCESSES, TOTAL_ERRORS
def validate(WEIGHT_VECTORS: ndarray, VALID: ndarray) -> tuple[int, int]:
'''
Returns the number of successful and unsuccessful predictions made using
the given `WEIGHT_VECTORS` on the given `VALID`ation set.
'''
assert isinstance(WEIGHT_VECTORS, ndarray)
assert isinstance(VALID, ndarray)
num_successes, num_errors = 0, 0
for row in VALID:
class_label = int(row[10])
# res = []
# for weights in WEIGHT_VECTORS:
# res.append(np.dot(weights, features := row[0:10]))
features = row[0:10]
logits = [np.dot(weights, features) for weights in WEIGHT_VECTORS]
predicted_label = np.argmax(logits)
if predicted_label == class_label:
num_successes += 1
else:
num_errors += 1
return num_successes, num_errors
def get_predictions(FILE: str, WEIGHT_VECTORS: ndarray) -> list:
'''
Returns `predictions` for each image / handwritten digit in
the given `FILE` using the given `WEIGHT_VECTORS`.
'''
assert isinstance(FILE, str)
assert isinstance(WEIGHT_VECTORS, ndarray)
predictions = []
TEST = get_testing_data(FILE, VERBOSE=True)
for row in TEST:
# I'm not sure if logits is the right term here.
# logits + softmax = prediction ?
features = row[0:10]
logits = [np.dot(weights, features) for weights in WEIGHT_VECTORS]
predictions.append(prediction := np.argmax(logits))
# less readable version:
# predictions.append(np.argmax(np.dot(WEIGHT_VECTORS, features)))
assert isinstance(predictions, list)
assert all(isinstance(prediction, np.int64) for prediction in predictions)
return predictions
##########################################################################
# #
# END: Train, Validate, and Test Weights #
# #
##########################################################################