-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN.py
140 lines (115 loc) · 4.73 KB
/
CNN.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
import numpy as np
from tqdm import tqdm
import time
class CNN:
def __init__(self, layers: np.array):
self.layers = layers
def fit(self, X_train, y_train, X_val, y_val, loss, epochs=100, rate=0.01):
self.X_train = X_train
self.y_train = y_train
self.X_val = X_val
self.y_val = y_val
self.epochs = epochs
self.loss = loss
self.rate = rate
self.best_accuracy = 0
def shuffle(self):
indices = np.arange(self.X_train.shape[0])
np.random.shuffle(indices)
self.X_train = self.X_train[indices]
self.y_train = self.y_train[indices]
val_size = self.X_train.shape[0] // 8
self.X_val = np.array(self.X_train[:val_size])
self.y_val = np.array(self.y_train[:val_size])
self.X_train = np.array(self.X_train[val_size:])
self.y_train = np.array(self.y_train[val_size:])
def predict(self, X):
for layer in self.layers:
X = layer.forward(X)
return X
def train(self, batch_size=8, report=False, snapshot=True):
self.best_accuracy = 0
accuracy_train = [0 for _ in range(self.epochs)]
accuracy_val = [0 for _ in range(self.epochs)]
error_train = [0 for _ in range(self.epochs)]
for epoch in range(self.epochs):
# self.shuffle()
error = 0
count = 0
if report:
progress = tqdm(range(self.X_train.shape[0]))
progress.set_description(f"Training epoch {epoch+1}/{self.epochs}")
batch = 0
batchX = np.zeros(
(
batch_size,
self.X_train.shape[1],
self.X_train.shape[2],
self.X_train.shape[3],
)
)
batchY = np.zeros(
(batch_size, self.y_train.shape[1], self.y_train.shape[2])
)
for x, y in zip(self.X_train, self.y_train):
if report:
progress.update(1)
batchX[batch] = x
batchY[batch] = y
batch += 1
if batch == batch_size:
batch = 0
pred = self.predict(batchX)
for i in range(batch_size):
error += self.loss.forward(batchY[i], pred[i])
for i in range(batch_size):
count += np.argmax(pred[i]) == np.argmax(batchY[i])
grad = [[] for _ in range(batch_size)]
for i in range(batch_size):
grad[i] = self.loss.backward(batchY[i], pred[i])
grad = np.array(grad)
for layer in reversed(self.layers):
grad = layer.backward(grad, self.rate)
val_accuracy = 0
if self.X_val is not None:
for x, y in zip(self.X_val, self.y_val):
pred = self.predict(np.array([x]))
val_accuracy += np.argmax(pred[0]) == np.argmax(y)
val_accuracy /= self.X_val.shape[0]
if report:
progress.close()
print(f"Error: {error/self.X_train.shape[0]}")
print(f"Traigning accuracy: {count/self.X_train.shape[0]}")
if self.X_val is not None:
print(f"Validation accuracy: {val_accuracy}")
print("\n")
if snapshot:
if val_accuracy > self.best_accuracy:
self.best_accuracy = val_accuracy
self.save("best_snapshot.npy")
accuracy_train[epoch] = count / self.X_train.shape[0]
accuracy_val[epoch] = val_accuracy
error_train[epoch] = error / self.X_train.shape[0]
return accuracy_train, accuracy_val, error_train
def number_of_parameters(self):
print([layer.number_of_parameters() for layer in self.layers])
return sum([layer.number_of_parameters() for layer in self.layers])
def stats(self):
pass
def reset(self):
for layer in self.layers:
if layer.number_of_parameters() != 0:
layer.initialize()
def save(self, path):
record = np.array([])
for layer in self.layers:
if layer.number_of_parameters() != 0:
record = np.append(record, layer.save())
np.save(path, record)
def load(self, path):
record = np.load(path, allow_pickle=True)
index = 0
for layer in self.layers:
if layer.number_of_parameters() != 0:
layer.load(record[index : index + layer.number_of_parameters()])
index += layer.number_of_parameters()