-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
47 lines (38 loc) · 980 Bytes
/
test_model.py
File metadata and controls
47 lines (38 loc) · 980 Bytes
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
# # #
# Test my_keras_model.h5
# Please be sure that both CPU and GPU are used.
# Tested with Intel Core i7 & NVIDIA GeForce 840M
# Result: accuracy = 100%
# # #
from keras.models import Sequential
from keras.layers import Dense
from keras.models import load_model
import keras.utils
import numpy as np
# randomize for initial weights
np.random.seed(7)
# Generate Dataset for testing
# This dataset has 2 classes:
# [0]: y = x
# [1]: y = x^2
X = []
for i in range(1001, 1100):
X.append([i, i, 0])
X.append([i, i**2, 1])
X = np.array(X)
Y = X[:, 2]
X = X[:, [0,1]]
# # #
# Load model
# # #
model = load_model('my_keras_model.h5')
# # #
# Test
# # #
# remember: the evaluate method also relies on one-hot encoding
# Convert labels to categorical one-hot encoding
one_hot_Y = keras.utils.to_categorical(Y, num_classes=2)
# evaluate using data test
scores = model.evaluate(X, one_hot_Y)
# print accuracy
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))