-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
35 lines (26 loc) · 831 Bytes
/
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
import argparse
import torch
from torch import nn
from model.model import SECOVARC
from model.data import ARC
def test(model, data, mode='test'):
if mode == 'dev':
iterator = iter(data.dev_iter)
else:
iterator = iter(data.test_iter)
criterion = nn.BCEWithLogitsLoss()
model.eval()
acc, loss, size = 0, 0, 0
single_acc = 0
for batch in iterator:
pred = model(batch).squeeze()
batch_loss = criterion(pred, batch.label)
loss += batch_loss.data[0]
single_acc += ((pred > 0).long() == batch.label.long()).sum().float()
acc += (pred[::2] > pred[1::2]).sum().float()
size += len(pred) // 2
acc /= size
acc = acc.cpu().data[0]
single_acc /= (size * 2)
single_acc = single_acc.cpu().data[0]
return loss, acc, single_acc