-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.py
67 lines (49 loc) · 1.86 KB
/
Network.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
import numpy as np
from tqdm import tqdm
class Network:
def __init__(self, loss, loss_prime):
self.layers = []
self.loss = loss
self.loss_prime = loss_prime
def add_layer(self, layer):
self.layers.append(layer)
def train(self, x_train, y_train, niter, lr):
average_loss = []
for i in tqdm(range(niter)):
loss = 0
for j in range(len(x_train)):
# sample j
output = x_train[j]
# forward propagation
for layer in self.layers:
output = layer.forward_prop(output)
# loss calculation
loss = loss + self.loss(y_train[j], output)
# backward propagation
error = self.loss_prime(y_train[j], output)
for layer in reversed(self.layers):
error = layer.backward_prop(error, lr)
average_loss.append(loss/len(x_train))
return average_loss
# print('epoch %d/%d error=%f' % (i+1, niter, average_loss))
def predict(self, input):
results = []
# iterate through each input
for i in range(len(input)):
output = input[i]
# run forward propagation
for layer in self.layers:
output = layer.forward_prop(output)
results.append(output)
return results
def accuracy(self, input, truth):
results = []
sum = 0
for i in range(len(input)):
zeros = np.zeros(input[i].shape)
max_index = np.argmax(input[i])
correct_index = np.argmax(truth[i])
sum += max_index == correct_index
zeros[0][max_index] = 1
results.append(zeros)
return results, sum/len(input)