-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
63 lines (55 loc) · 2.34 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
import torch
import torch.nn.functional as F
@torch.no_grad()
def evaluate(model, dataset, split_idx, eval_func, criterion, args, result=None):
if result is not None:
out = result
else:
model.eval()
out = model(dataset.graph['node_feat'], dataset.graph['edge_index'])
train_acc = eval_func(
dataset.label[split_idx['train']], out[split_idx['train']])
valid_acc = eval_func(
dataset.label[split_idx['valid']], out[split_idx['valid']])
test_acc = eval_func(
dataset.label[split_idx['test']], out[split_idx['test']])
if args.dataset in ('questions'):
if dataset.label.shape[1] == 1:
true_label = F.one_hot(dataset.label, dataset.label.max() + 1).squeeze(1)
else:
true_label = dataset.label
valid_loss = criterion(out[split_idx['valid']], true_label.squeeze(1)[
split_idx['valid']].to(torch.float))
else:
out = F.log_softmax(out, dim=1)
valid_loss = criterion(
out[split_idx['valid']], dataset.label.squeeze(1)[split_idx['valid']])
return train_acc, valid_acc, test_acc, valid_loss, out
@torch.no_grad()
def evaluate_cpu(model, dataset, split_idx, eval_func, criterion, args, device, result=None):
if result is not None:
out = result
else:
model.eval()
model.to(torch.device("cpu"))
dataset.label = dataset.label.to(torch.device("cpu"))
edge_index, x = dataset.graph['edge_index'], dataset.graph['node_feat']
out = model(x, edge_index)
train_acc = eval_func(
dataset.label[split_idx['train']], out[split_idx['train']])
valid_acc = eval_func(
dataset.label[split_idx['valid']], out[split_idx['valid']])
test_acc = eval_func(
dataset.label[split_idx['test']], out[split_idx['test']])
if args.dataset in ('questions'):
if dataset.label.shape[1] == 1:
true_label = F.one_hot(dataset.label, dataset.label.max() + 1).squeeze(1)
else:
true_label = dataset.label
valid_loss = criterion(out[split_idx['valid']], true_label.squeeze(1)[
split_idx['valid']].to(torch.float))
else:
out = F.log_softmax(out, dim=1)
valid_loss = criterion(
out[split_idx['valid']], dataset.label.squeeze(1)[split_idx['valid']])
return train_acc, valid_acc, test_acc, valid_loss, out