-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
75 lines (57 loc) · 2.05 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
75
import utils
import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
class ANN(object):
def __init__(self, M):
self.M = M
def fit(self, X, Y, learning_rate = 10e-6, reg = 10e-7, epochs = 1000, show_figure = False):
X, Y = shuffle(X, Y)
x_valid = X[-10:]
y_valid = Y[-10:]
t_valid = utils.y2indicator(y_valid)
x = X[:-10]
y = Y[:-10]
t = utils.y2indicator(y)
N, D = x.shape
K = len(set(y))
self.W1 = np.random.randn(D, self.M)
self.b1 = np.random.randn(self.M)
self.W2 = np.random.randn(self.M, K)
self.b2 = np.random.randn(K)
costs = []
for i in range(epochs):
pY, Z = self.forward(x)
#Updating Weights
D = pY - t
self.W2 -= learning_rate * (Z.T.dot(D) + reg * self.W2)
self.b2 -= learning_rate * (D.sum() + reg * self.b2)
dZ = D.dot(self.W2.T) * Z * (1 - Z)
self.W1 -= learning_rate * (x.T.dot(dZ) + reg * self.W1)
self.b1 -= learning_rate * (dZ.sum() + reg * self.b1)
if i % 10 == 0:
pY_valid, _= self.forward(x_valid)
c = utils.cost(t_valid, pY_valid)
costs.append(c)
e = utils.error_rate(y_valid, np.argmax(pY_valid, axis = 1))
print("i:", i, " cost: ", c, " error: ", e)
if show_figure:
plt.plot(costs)
plt.show()
def forward(self, X):
Z = utils.sigmoid(X.dot(self.W1) + self.b1)
Y = utils.softmax(Z.dot(self.W2) + self.b2)
return Y, Z
def predict(self, X):
pY, _ = self.forward(X)
return pY
def score(self, X, Y):
prediction = self.predict(X)
return utils.error_rate(Y, prediction)
def main():
X, Y = utils.get_data()
Y = utils.encode_labels(Y)
print(Y[-10:])
model = ANN(200)
model.fit(X, Y, reg = 0, show_figure=True)
main()