-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
executable file
·51 lines (47 loc) · 1.46 KB
/
test.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
#coding:utf-8
from classifiers.cnn import *
from datareader.data_utils import *
from layers.layers import *
from layers.layer_utils import *
from solver.solver import *
from time import *
import cPickle
def check_accuracy(X, y, model, num_samples=None, batch_size=100):
N = X.shape[0]
if num_samples is not None and N > num_samples:
mask = np.random.choice(N, num_samples)
N = num_samples
X = X[mask]
y = y[mask]
#batch的个数
num_batches = N / batch_size
if N % batch_size != 0:
num_batches += 1
y_pred = []
#计算每个batch预测值
for i in xrange(num_batches):
start = i * batch_size
end = (i + 1) * batch_size
scores = model.loss(X[start:end])
y_pred.append(np.argmax(scores, axis=1))
y_pred = np.hstack(y_pred)
acc = np.mean(y_pred == y)
return acc
if __name__ == "__main__":
#读取数据
data = get_MNIST_data()
f = open('paramdata/model','rb')
MyModel = cPickle.load(f)
print check_accuracy(data['X_test'], data['y_test'],MyModel)
f.close()
#print best_params['W1'][0]
#MyModel.params = best_params
#y_test_pred = np.argmax(MyModel.loss(data['X_val']),axis=1)
#print (y_test_pred==data['y_val']).mean()
#print best_params.keys()
#print MyModel.params.keys()
#for k in MyModel.params.keys():
# MyModel.params[k] = best_params[k]
#print 'load done'
#print check_accuracy(data['X_train'], data['y_train'],MyModel,100)
#f.close()