-
Notifications
You must be signed in to change notification settings - Fork 0
/
NeuralNetworkBatch.py
365 lines (312 loc) · 12.3 KB
/
NeuralNetworkBatch.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import pickle
import numpy as np
import ActivationFunctions.ReLU
import ActivationFunctions.Sigmoid
from ActivationFunctions.SoftMax import SoftMax
from MNISTHandler import MNISTHandler
from NetworkStructure.DataBatch import Data
from NetworkStructure.ValueLayerBatch import ValueLayerBatch
from NetworkStructure.WeightLayer import WeightLayer
ALPHA = 0.1
TRAINING_SIZE = 60_000
TEST_SIZE = 10_000
BATCH_SIZE = 100
DEFAULT_FUNCTION = ActivationFunctions.ReLU.ReLU
OUTPUT_FUNCTION = ActivationFunctions.SoftMax.SoftMax
WEIGHT_RANGE_LOWER = -0.1
WEIGHT_RANGE_UPPER = 0.1
# Pre-defined strings
TRAIN_OR_TEST_MESS = "Training (0) or testing (1) data? "
NO_DATA_MESS = "No data available "
class NeuralNetwork:
def __init__(self, inputSize, firstLayerSize):
self.values = list()
self.weightLayers = list()
self.training = list()
self.testing = list()
self.inputSize = inputSize
self.outputSize = firstLayerSize
self.weightLayers.append(
WeightLayer(
abs(WEIGHT_RANGE_UPPER - WEIGHT_RANGE_LOWER)
* np.random.rand(firstLayerSize, inputSize)
+ WEIGHT_RANGE_LOWER
)
)
self.values.append(
ValueLayerBatch(BATCH_SIZE, firstLayerSize, OUTPUT_FUNCTION)
)
self.blank_data()
# todo: On further inspection, this makes no sense.
# todo: It doesn't seem to be used anywhere either.
# TODO: Remove, I guess.
def is_empty(self) -> bool:
return len(self.weightLayers) > 0
def has_data(self, target) -> bool:
return len(target) > 0
def blank_data(self):
self.training = list()
def get_output_layer(self):
return self.values[-1]
def display(self):
for i, (w, v) in enumerate(zip(self.weightLayers, self.values)):
print(f"{w} w[{i}]\n{v} v[{i}] ({v.activationFunction.__name__})")
def add_layer(
self,
batchSize,
size,
minValue=WEIGHT_RANGE_LOWER,
maxValue=WEIGHT_RANGE_UPPER,
):
# Append a new weight layer with random values in the defined range
weights = (maxValue - minValue) * np.random.rand(
size, self.values[-1].getSize()
) + minValue
self.weightLayers.append(WeightLayer(weights))
# Set the former output layer's method to the default function
self.values[-1].setMethod(DEFAULT_FUNCTION)
# Append a new output value layer with no activation method
self.values.append(ValueLayerBatch(batchSize, size, OUTPUT_FUNCTION))
self.outputSize = size
# Remove old data, which might no longer be suitable for the new shape
self.blank_data()
def refresh_values(self):
# Generate empty value layers
self.values = [
ValueLayerBatch(BATCH_SIZE, layer.getShape()[0], DEFAULT_FUNCTION)
for layer in self.weightLayers
]
# Remove the final layer's activation method
self.values[-1].setMethod()
def load(self, filename):
with open(filename, "rb") as handle:
self.weightLayers = pickle.load(handle)
self.refresh_values()
self.training.clear()
self.testing.clear()
self.inputSize = self.weightLayers[0].getShape()[1]
self.outputSize = self.values[-1].getSize()
def save(self, filename):
with open(filename, "wb") as handle:
pickle.dump(
self.weightLayers, handle, protocol=pickle.HIGHEST_PROTOCOL
)
def forward_propagate(self, inputData):
if inputData.shape[1] != self.inputSize:
print(
f"Invalid input data size, {inputData.shape[1]} != {self.inputSize}"
)
return
# Forward propagate input through the network
# inputData is used to store the previous layer's values
for i in range(len(self.values)):
self.values[i].values = inputData.dot(
self.weightLayers[i].weights.T
)
self.values[i].applyMethod()
self.values[i].applyDropoutNewMask()
inputData = self.values[i].values
return self.values[-1].values
def fit(self):
for batch in self.training:
output = self.forward_propagate(batch.input)
self.values[-1].delta = (
2 / self.outputSize * (output - batch.output)
)
if self.values[-1].activationFunction.__name__ == "SoftMax":
self.values[-1].delta /= batch.output.shape[0]
# Hidden layer delta calculation
for i in range(len(self.values) - 2, -1, -1):
self.values[i].delta = (
self.values[i + 1].delta.dot(
self.weightLayers[i + 1].weights
)
* self.values[i].getAfterDeriv()
)
self.values[i].applyMaskToDelta()
# Backpropagation
for i in reversed(range(len(self.weightLayers))):
grad = (
batch.input.T.dot(self.values[i].delta).T
if i == 0
else self.values[i - 1].values.T.dot(self.values[i].delta).T
)
self.weightLayers[i].weights -= ALPHA * grad
def update_latest_data_manual(self, target):
for i in range(len(target[-1].input[0])):
target[-1].input[0][i] = float(input("Enter input value: "))
print(target[-1].input[0])
for i in range(len(target[-1].output[0])):
target[-1].output[0][i] = float(input("Enter output value: "))
print(target[-1].output[0])
def add_sample_manual(self, target):
target.append(
Data(
np.ones((1, self.inputSize)),
np.ones((1, self.outputSize)),
)
)
network.update_latest_data_manual(target)
def add_sample_random(self, target):
target.append(
Data(
np.random.rand(1, self.inputSize),
np.random.rand(1, self.outputSize),
)
)
def display_dataset(self, target):
print(target[0])
def add_sample_colour(
self, r: float, g: float, b: float, colour: int, target
):
target.append(
Data(
np.zeros((1, network.inputSize)),
np.zeros((1, network.outputSize)),
)
)
print(f"Val = {target[-1].input[0][0]}")
target[-1].input[0][0] = r
target[-1].input[0][1] = g
target[-1].input[0][2] = b
print(f"colour = {colour}")
target[-1].output[0][colour - 1] = 1
print(
f"Appending input{target[-1].input[0]}, output = {target[-1].output[0]}"
)
def load_colour_file(self, filename, target):
with open(filename, "r") as handle:
data = list(map(float, handle.read().split()))
for i in range(0, len(data), 4):
r, g, b, out = data[i : i + 4]
self.add_sample_colour(r, g, b, int(out), target)
def validate_multi_class(self, target):
total, correct = 0, 0
for sampleBatch in target:
resultBatch = self.forward_propagate(sampleBatch.input)
for result, sample in zip(resultBatch, sampleBatch.output):
total += 1
correct += np.argmax(result) == np.argmax(sample)
return float(correct / total * 100)
def activation_method_test(self):
self.values[0].applyMethod()
def set_weights(self, index):
self.weightLayers[index].weights = np.array(
[
[float(input("Enter weight value: ")) for _ in row]
for row in self.weightLayers[index].weights
]
)
# Overwrites the train and test data with MNIST
def load_MNIST(self):
handler = MNISTHandler()
# Load training data
train_input = handler.get_train_input(TRAINING_SIZE)
train_output = handler.get_train_output(TRAINING_SIZE)
self.training = [
Data(
train_input[i : i + BATCH_SIZE],
train_output[i : i + BATCH_SIZE],
)
for i in range(0, TRAINING_SIZE, BATCH_SIZE)
]
# Load testing data
test_input = handler.get_test_input(TEST_SIZE)
test_output = handler.get_test_output(TEST_SIZE)
self.testing = [
Data(
test_input[i : i + BATCH_SIZE], test_output[i : i + BATCH_SIZE]
)
for i in range(0, TEST_SIZE, BATCH_SIZE)
]
def single_out_data(self, target):
temp = target[0]
target.clear()
target.append(temp)
if __name__ == "__main__":
inputData = np.ones((BATCH_SIZE, int(input("Enter input data size: "))))
firstLayerSize = int(input("Enter first layer size: "))
network = NeuralNetwork(inputData.shape[1], firstLayerSize)
while True:
print(
"0 - Add quick layer\n"
"1 - Add custom layer\n"
"2 - Fit\n"
"3 - Display\n"
"4 - Predict\n"
"5 - Save\n"
"6 - Load\n"
"7 - Overwrite latest data\n"
"8 - Append new data\n"
"9 - Append random data\n"
"10- Load colour file (REQUIRES 3/4 I/O FORMAT)\n"
"11- Validate multi-class\n"
"12- Set weights\n"
"13- Load MNIST (REQUIRES 784/10 I/O FORMAT)\n"
)
operation = int(input("Choose operation: "))
if operation == 0:
network.add_layer(BATCH_SIZE, int(input("Enter layer size: ")))
elif operation == 1:
network.add_layer(
int(input("Enter layer size: ")),
int(input("Enter min weight value: ")),
int(input("Enter max weight value: ")),
)
elif operation == 2:
if network.has_data(network.training):
count = int(input("How many times? "))
for i in range(count):
network.fit()
print(
f"{i}: {network.validate_multi_class(network.training)}%"
)
else:
print(NO_DATA_MESS)
elif operation == 3:
network.display_dataset(network.training)
network.display()
elif operation == 4:
choice = int(input(TRAIN_OR_TEST_MESS))
target = network.training if choice == 0 else network.testing
if network.has_data(target):
for sample in target:
print(network.forward_propagate(sample.input))
else:
print(NO_DATA_MESS)
elif operation == 5:
network.save("data.pickle")
elif operation == 6:
network.load("data.pickle")
elif operation == 7:
choice = int(input(TRAIN_OR_TEST_MESS))
target = network.training if choice == 0 else network.testing
if network.has_data(target):
network.update_latest_data_manual(target)
network.display_dataset(target)
else:
print(NO_DATA_MESS)
elif operation == 8:
choice = int(input(TRAIN_OR_TEST_MESS))
target = network.training if choice == 0 else network.testing
network.add_sample_manual(target)
network.display_dataset(target)
elif operation == 9:
choice = int(input(TRAIN_OR_TEST_MESS))
target = network.training if choice == 0 else network.testing
network.add_sample_random(target)
network.display_dataset(target)
elif operation == 10:
choice = int(input(TRAIN_OR_TEST_MESS))
target = network.training if choice == 0 else network.testing
network.load_colour_file(str(input("Enter file name: ")), target)
elif operation == 11:
choice = int(input(TRAIN_OR_TEST_MESS))
target = network.training if choice == 0 else network.testing
print(f"{network.validate_multi_class(target)}% correct")
elif operation == 12:
network.set_weights(int(input("Enter weight layer index: ")))
elif operation == 13:
network.load_MNIST()
else:
print("Invalid operation!")