-
Notifications
You must be signed in to change notification settings - Fork 150
/
ann.py
79 lines (61 loc) · 2.43 KB
/
ann.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
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
from util import getData, softmax, cost2, y2indicator, error_rate, relu
from sklearn.utils import shuffle
class ANN(object):
def __init__(self, M):
self.M = M
def fit(self, X, Y, Xvalid, Yvalid, learning_rate=1e-6, reg=1e-6, epochs=10000, show_fig=False):
N, D = X.shape
K = len(set(Y))
T = y2indicator(Y)
self.W1 = np.random.randn(D, self.M) / np.sqrt(D)
self.b1 = np.zeros(self.M)
self.W2 = np.random.randn(self.M, K) / np.sqrt(self.M)
self.b2 = np.zeros(K)
costs = []
best_validation_error = 1
for i in range(epochs):
# forward propagation and cost calculation
pY, Z = self.forward(X)
# gradient descent step
pY_T = pY - T
self.W2 -= learning_rate*(Z.T.dot(pY_T) + reg*self.W2)
self.b2 -= learning_rate*(pY_T.sum(axis=0) + reg*self.b2)
# dZ = pY_T.dot(self.W2.T) * (Z > 0) # relu
dZ = pY_T.dot(self.W2.T) * (1 - Z*Z) # tanh
self.W1 -= learning_rate*(X.T.dot(dZ) + reg*self.W1)
self.b1 -= learning_rate*(dZ.sum(axis=0) + reg*self.b1)
if i % 10 == 0:
pYvalid, _ = self.forward(Xvalid)
c = cost2(Yvalid, pYvalid)
costs.append(c)
e = error_rate(Yvalid, np.argmax(pYvalid, axis=1))
print("i:", i, "cost:", c, "error:", e)
if e < best_validation_error:
best_validation_error = e
print("best_validation_error:", best_validation_error)
if show_fig:
plt.plot(costs)
plt.show()
def forward(self, X):
# Z = relu(X.dot(self.W1) + self.b1)
Z = np.tanh(X.dot(self.W1) + self.b1)
return softmax(Z.dot(self.W2) + self.b2), Z
def predict(self, X):
pY, _ = self.forward(X)
return np.argmax(pY, axis=1)
def score(self, X, Y):
prediction = self.predict(X)
return 1 - error_rate(Y, prediction)
def main():
Xtrain, Ytrain, Xvalid, Yvalid = getData()
model = ANN(200)
model.fit(Xtrain, Ytrain, Xvalid, Yvalid, reg=0, show_fig=True)
print(model.score(Xvalid, Yvalid))
if __name__ == '__main__':
main()