-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.py
69 lines (56 loc) · 2.58 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
68
69
"""
9/18/17
kasim
"""
import numpy as np
from functions import softmax, cross_entropy
class Network:
def __init__(self, input_size, feature_size):
self.input_size = input_size
self.feature_size = feature_size
self.weights_one = np.random.randn(self.feature_size, self.input_size)
self.weights_two = np.random.randn(self.input_size, self.feature_size)
self.features = np.zeros((self.input_size, self.feature_size))
def forward(self, _input):
z = np.dot(self.weights_one, _input)
self.features[np.argmax(_input)] = z.reshape(z.size);
g = np.dot(self.weights_two, z)
s = softmax.func(g)
return z, g, s
def feed_forward(self, x_mini_batch, y_mini_batch):
weights_two_jacobian = np.zeros((self.input_size, self.feature_size))
weights_one_jacobian = np.zeros((self.feature_size, self.input_size))
totat_cost = 0
counter = 0
j=0
for x, y in zip(x_mini_batch, y_mini_batch):
_x = x.reshape(x.size, 1)
_y = y.reshape(y.size, 1)
z, g, s = self.forward(_x)
if s.argmax() == _y.argmax():
counter += 1
j+=1
totat_cost += cross_entropy.cost(_y, s)
weights_two_jacobian += cross_entropy.weights_two_jacobian(_y, s, z, (self.input_size, self.feature_size))
weights_one_jacobian += cross_entropy.weights_one_jacobian(_y, s, self.weights_two, _x,
(self.feature_size, self.input_size))
weights_two_jacobian = weights_two_jacobian / x_mini_batch.size
weights_one_jacobian = weights_one_jacobian / x_mini_batch.size
return counter, totat_cost, weights_two_jacobian, weights_one_jacobian
def train(self, epoch, eta, mini_batch_size, data):
initial_cost = 0;
for e in range(epoch):
np.random.shuffle(data)
mini_batch = data[:mini_batch_size]
x_mini_batch = mini_batch[:, :self.input_size]
y_mini_batch = mini_batch[:, self.input_size:]
counter, cost, weights_two_jacobian, weights_one_jacobian = self.feed_forward(x_mini_batch, y_mini_batch)
self.weights_two -= eta * weights_two_jacobian
self.weights_one -= eta * weights_one_jacobian
if e == 0:
initial_cost = cost
print "epoch ", e, " cost = ", cost, " correct = ", counter,"\n"
print"initial cost was :", initial_cost, "\n"
# print self.features
return self.features