-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.py
94 lines (77 loc) · 3.25 KB
/
eval.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import torch
import torchtext
from torchtext.data import Field, Iterator
import argparse
from SNLIBatchGenerator import SNLIBatchGenerator
from SNLIClassifier import SNLIClassifier
from nltk import word_tokenize
# Default parameters
BATCH_SIZE_DEFAULT = 64
def get_accuracy(scores, true_labels):
"""
Get the accuracy
:param scores: Matrix of prediction scores of shape no. of batches x no. of classes
:param true_labels: Vector of true labels of shape no. of batches
:return:
"""
pred = torch.argmax(scores, dim=1)
accuracy = torch.sum(pred == true_labels, dtype=torch.float32) / scores.shape[0]
return accuracy
def eval_model():
"""
Evaluate the classifier model
:return:
"""
# Seed
torch.manual_seed(42)
# Set device
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
# Define the text and label fields
TEXT = Field(sequential=True, tokenize=word_tokenize, lower=True, use_vocab=True, batch_first=False, include_lengths=True)
LABEL = Field(sequential=False, use_vocab=True, pad_token=None, unk_token=None, batch_first=False)
# Load the test set
_, _, test_set = torchtext.datasets.SNLI.splits(TEXT, LABEL, root=args.data_path)
# Build the vocabulary
TEXT.build_vocab(test_set)
LABEL.build_vocab(test_set)
# Override the vocabulary from the checkpoint
checkpoint = torch.load(args.checkpoint_path)
TEXT.vocab.stoi = checkpoint['text_vocab']
LABEL.vocab.stoi = checkpoint['label_vocab']
vocab_size = len(TEXT.vocab.stoi)
# Define an iterator over the test set
test_iter = Iterator(dataset=test_set, batch_size=args.batch_size, device=device)
# Custom wrapper over the iterator
test_batch_loader = SNLIBatchGenerator(test_iter)
# Define the model and load it
model = SNLIClassifier(encoder=args.model_type,
vocab_size=vocab_size,
embedding_dim=300,
hidden_dim=2048,
fc_dim=512,
num_classes=3,
pretrained_vectors=None).to(device)
model.load_state_dict(checkpoint['model_state_dict'])
# Evaluation
model.eval()
with torch.no_grad():
test_accuracy = 0
with torch.no_grad():
for batch_id, (premise, hypothesis, label) in enumerate(test_batch_loader):
out = model(premise[0], hypothesis[0], premise[1], hypothesis[1])
test_accuracy += get_accuracy(out, label)
test_accuracy /= batch_id
print("Test accuracy = %f" % test_accuracy)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('model_type', choices={'average', 'uniLSTM', 'biLSTM', 'biLSTMmaxpool'},
help='Type of encoder for the sentences')
parser.add_argument('checkpoint_path', type=str,
help='Path to save/load the checkpoint data')
parser.add_argument('data_path', type=str,
help='Path where data is saved')
parser.add_argument('--batch_size', type=int, default=BATCH_SIZE_DEFAULT,
help='Batch size for evaluating the model')
args = parser.parse_args()
eval_model()