-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn_predict_rectifier.py
61 lines (47 loc) · 1.48 KB
/
nn_predict_rectifier.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
import numpy as np
from numpy import array
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score, classification_report, confusion_matrix
import pickle
def rectifier(x):
return np.log(1.+1./np.exp(-x))
#return ( 1./(1. + np.exp(-x)) )
def predict(model, x):
W, b = model['W'], model['b']
# Forward propagation
num_layers = len(W)-1
z={}
a={}
z[0] = x.dot(W[0]) + b[0]
a[0] = rectifier(z[0])
for i in range(num_layers-1) :
z[i+1] = a[i].dot(W[i+1]) + b[i+1]
a[i+1] = rectifier(z[i+1])
z[num_layers] = a[num_layers-1].dot(W[num_layers]) + b[num_layers]
#print z2
exp_scores = np.exp(z[num_layers])
probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)
return np.argmax(probs, axis=1)
def main():
X=[]
y=[]
with open("data-test.txt","r") as infile :
for lines in infile :
part = lines.split()
lst=[]
for j,i in enumerate(part) :
if (j==0) : continue
if (j==len(part)-1 ) :
y.append(int(i)-1)
else : lst.append(float(i))
X.append(lst)
print (len(X),len(y))
X=array(X)
y=array(y)
print "Reading model from json file"
with open("model0","rb") as infile:
model = pickle.load(infile)
Y_pred = predict(model,X)
print accuracy_score(y,Y_pred)
print classification_report(y,Y_pred)
if __name__ == "__main__":
main()