-
Notifications
You must be signed in to change notification settings - Fork 9
/
test.py
74 lines (52 loc) · 1.9 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import argparse
import torch
from torch import nn
from model.model import NN4SNLI
from model.data import SNLI
def test(model, data, mode='test'):
if mode == 'dev':
iterator = iter(data.dev_iter)
else:
iterator = iter(data.test_iter)
criterion = nn.CrossEntropyLoss()
model.eval()
acc, loss, size = 0, 0, 0
for batch in iterator:
pred = model(batch)
batch_loss = criterion(pred, batch.label)
loss += batch_loss.data[0]
_, pred = pred.max(dim=1)
acc += (pred == batch.label).sum().float()
size += len(pred)
acc /= size
acc = acc.cpu().data[0]
return loss, acc
def load_model(args, data):
model = NN4SNLI(args, data)
model.load_state_dict(torch.load(args.model_path))
if args.gpu > -1:
model.cuda(args.gpu)
return model
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--batch-size', default=64, type=int)
parser.add_argument('--block-size', default=-1, type=int)
parser.add_argument('--data-type', default='SNLI')
parser.add_argument('--dropout', default=0.1, type=float)
parser.add_argument('--gpu', default=0, type=int)
#parser.add_argument('--hidden-size', default=300, type=int)
parser.add_argument('--mSA-scalar', default=5.0, type=float)
parser.add_argument('--word-dim', default=300, type=int)
parser.add_argument('--model-path', required=True)
args = parser.parse_args()
print('loading SNLI data...')
data = SNLI(args)
setattr(args, 'word_vocab_size', len(data.TEXT.vocab))
setattr(args, 'class_size', len(data.LABEL.vocab))
# if block size is lower than 0, a heuristic for block size is applied.
if args.block_size < 0:
args.block_size = data.block_size
print('loading model...')
model = load_model(args, data)
_, acc = test(model, data)
print(f'test acc: {acc:.3f}')