forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathann_predict.py
38 lines (29 loc) · 867 Bytes
/
ann_predict.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
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
from process import get_data
X, Y, _, _ = get_data()
# randomly initialize weights
M = 5
D = X.shape[1]
K = len(set(Y))
W1 = np.random.randn(D, M)
b1 = np.zeros(M)
W2 = np.random.randn(M, K)
b2 = np.zeros(K)
# make predictions
def softmax(a):
expA = np.exp(a)
return expA / expA.sum(axis=1, keepdims=True)
def forward(X, W1, b1, W2, b2):
Z = np.tanh(X.dot(W1) + b1)
return softmax(Z.dot(W2) + b2)
P_Y_given_X = forward(X, W1, b1, W2, b2)
print("P_Y_given_X.shape:", P_Y_given_X.shape)
predictions = np.argmax(P_Y_given_X, axis=1)
# calculate the accuracy
def classification_rate(Y, P):
return np.mean(Y == P)
print("Score:", classification_rate(Y, predictions))