-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (63 loc) · 2.47 KB
/
main.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
import numpy as np
import pandas as pd
import denselayer
import activationandlosses
class NeuralNetwork:
def __init__(self) -> None:
self.dev_labels, self.dev_data, self.training_labels, self.training_data = self.process_data("mnist_train.csv")
def process_data(self, filepath_or_buffer: str):
'''
number of examples is m
number of pixels is n
Output four items, the dev labels, dev data, training labels and training data
'''
data = pd.read_csv(filepath_or_buffer)
data = np.array(data)
m, n = data.shape
np.random.shuffle(data)
#for cross checking our data, transpose the matrix so each entry is
data_dev = data[0:1000].T
dev_labels = data_dev[0]
dev_data = data_dev[1:n]
data_training = data[1000:m].T
training_labels = data_training[0]
training_data = data_training[1:n]
#normalisation between 0-1 range
for ith_pixel in dev_data:
for unique in ith_pixel:
unique = (1/255) * unique
for ith_pixel in training_data:
for unique in ith_pixel:
unique = (1/255) * unique
print("end of data preprocessing")
dev_data, training_data = dev_data.T, training_data.T
return dev_labels, dev_data, training_labels, training_data
def predict(self, input):
output = input
for layer in self.layers:
output = layer.forward(output)
return output
def train(self, image, label, loss=activationandlosses.meansqaureerror, lossprime=activationandlosses.meansquareerror_integral,epochs=59000, learning_rate=0.1):
print("starting training")
for e in range(epochs):
error = 0
for image, label in zip(image, label):
#forward
output = self.predict(image)
#calculate loss-entropy
error += loss(label, output)
#back propogation
gradient = lossprime(label, output)
for layer in reversed(self.layers):
gradient = layer.backward(gradient, learning_rate)
error /= len(image)
print(f"{e + 1}/{epochs}, error={error}")
network = NeuralNetwork()
network.layers = [
denselayer.DenseLayer(28 * 28, 10),
activationandlosses.TanH(),
denselayer.DenseLayer(28 * 28, 10),
activationandlosses.TanH()
]
print("pretraining")
network.train(network.training_data, network.training_labels)