-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
29 lines (26 loc) · 1011 Bytes
/
utils.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
def train(net, optimizer, criterion, data):
net.train()
optimizer.zero_grad()
output = net(data.x, data.adj)
loss = criterion(output[data.train_mask], data.y[data.train_mask])
acc = accuracy(output[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
return loss, acc
def val(net, criterion, data):
net.eval()
output = net(data.x, data.adj)
loss_val = criterion(output[data.val_mask], data.y[data.val_mask])
acc_val = accuracy(output[data.val_mask], data.y[data.val_mask])
return loss_val, acc_val
def test(net, criterion, data):
net.eval()
output = net(data.x, data.adj)
loss_test = criterion(output[data.test_mask], data.y[data.test_mask])
acc_test = accuracy(output[data.test_mask], data.y[data.test_mask])
return loss_test, acc_test
def accuracy(output, labels):
preds = output.max(1)[1].type_as(labels)
correct = preds.eq(labels).double()
correct = correct.sum()
return correct / len(labels)