-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfontANN.py
More file actions
112 lines (82 loc) · 2.59 KB
/
Copy pathfontANN.py
File metadata and controls
112 lines (82 loc) · 2.59 KB
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
import numpy as np
import matplotlib.pyplot as plt
from process import get_data
from tempfile import TemporaryFile
from process import process_data
def sigmoid(Z):
return 1/(1+np.exp(-Z))
def feedforward(X, W1, W2, B1, B2):
A1 = X.dot(W1) + B1 #N x M
Z = sigmoid(A1) #N x M
A2 = Z.dot(W2) + B2 # N x K
Z2 = sigmoid(A2) #N x K
return Z2, Z
def gradient_w1 (Y, T, Z2, W2, Z1, X):
dw2 = ((gradient_BCE(Y, T)) * Z2 * (1 - Z2)).dot(W2.T) #N x M
return X.T.dot(dw2 * Z1 * (1 - Z1))
def gradient_w2(Y, T, Z2, Z1):
return Z1.T.dot((gradient_BCE(Y, T)) * Z2 * (1 - Z2))
def gradient_b2(Y, T, Z):
return ((gradient_BCE(Y, T)) * Z * (1 - Z)).sum(axis = 0)
def gradient_b1(Y, T, Z2, W2, Z1):
dw2 = ((gradient_BCE(Y, T)) * Z2 * (1 - Z2)).dot(W2.T)
return np.sum(dw2 * Z1 * (1 - Z1), axis = 0)
def class_rate(Y, T):
correct = 0
for i in range(len(Y)):
if(T[i] == Y[i]):
correct = correct+1
class_rate = correct/len(Y)
return class_rate
def loss(Y, T):
return (T * np.log(Y) + (1 - T) * np.log(1 - Y)).sum()
def gradient_BCE(Y, T):
return (T/Y) - (1-T)/(1-Y)
def main():
X, Y = get_data()
D = X.shape[1]
M = 16
K = 1
W1 = np.random.randn(D, M)
B1 = np.random.randn(M)
W2 = np.random.randn(M, K)
B2 = np.random.randn(K)
train_size = .85
epochs = batch_size = 50000
X_train = X[:(int)(X.shape[0] * train_size),:]
X_test = X[(int)(X.shape[0] * train_size):,:]
Y_train = Y[:(int)(Y.shape[0] * train_size)]
Y_test = Y[(int)(Y.shape[0] * train_size):]
X_batch = np.split(X_train, batch_size)
Y_batch = np.split(Y_train, batch_size)
learning_rate = 1e-5
losses = []
rates = []
for epoch in range(epochs):
X = X_batch[epoch]
Y = Y_batch[epoch]
YP, Z1 = feedforward(X, W1, W2, B1, B2)
Z2 = YP
l = loss(YP, Y)
losses.append(-l)
LP = np.rint(YP)
r = class_rate(LP, Y)
rates.append(r)
if(epoch % 1000 == 0):
print("loss: {} class rate: {}".format(l, r))
W2 += learning_rate * gradient_w2(YP, Y, Z2, Z1)
B2 += learning_rate * gradient_b2(YP, Y, Z2)
W1 += learning_rate * gradient_w1(YP, Y, Z2, W2, Z1, X)
B1 += learning_rate * gradient_b1(YP, Y, Z2, W2, Z1)
plt.plot(losses)
plt.show()
plt.plot(rates)
plt.show()
Y, Z = feedforward(X_test, W1, W2, B1, B2)
r = class_rate(np.rint(Y), Y_test)
print("test-set class rate: ",r)
np.savez('matrix.npz', W2 = W2, B2 = B2, W1 = W1, B1 = B1)
if __name__ == "__main__":
main()
def nada():
return 0