-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_model.py
More file actions
56 lines (44 loc) · 1.57 KB
/
test_model.py
File metadata and controls
56 lines (44 loc) · 1.57 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
import torch
from dataset import test_loader
import model_0
def test_model():
# Hyper-parameters
num_of_samples = 500
batch_size = 50
input_size = 1
num_classes = 2
# Initialize the model and its parameters.
model = model_0.Model(input_size, num_classes)
with open('model_0_parameters', 'rb') as f:
model.load_state_dict(torch.load(f))
with torch.no_grad():
correct = 0
total = 0
for data, labels in test_loader(num_of_samples, batch_size):
data = data.reshape(-1, input_size)
outputs = model(data)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy on {} samples: {}'.format(num_of_samples, correct / total))
def human_input_test_data():
# Hyper-parameters
input_size = 1
num_classes = 2
# Initialize the model and its parameters.
model = model_0.Model(input_size, num_classes)
with open('model_0_parameters', 'rb') as f:
model.load_state_dict(torch.load(f))
with torch.no_grad():
while True:
data = int(input('Input Data (0, 1): '))
data = torch.Tensor([data])
data = data.reshape(-1, input_size)
outputs = model(data)
_, predicted = torch.max(outputs.data, 1)
print('Predicted label:', int(predicted[0].data))
def main():
test_model()
# human_input_test_data()
if __name__ == '__main__':
main()