-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.py
36 lines (28 loc) · 1.04 KB
/
evaluation.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
import torch
import torch.nn as nn
from torchmetrics.classification import MultilabelF1Score
from dataset import num_labels
from device import device
def evaluate(model, batch_loader):
model = model.to(device)
model.eval()
def evaluate_batch(node_features, edge_index, labels):
f1_score = MultilabelF1Score(num_labels, average='micro').to(device)
loss_fcn = nn.BCEWithLogitsLoss()
model.eval()
with torch.no_grad():
logits = model(node_features, edge_index)
pred = torch.where(logits >= 0, 1, 0)
return (loss_fcn(logits, labels), f1_score(pred, labels))
total_score = 0
total_loss = 0
for (batch_id, batched_graph) in enumerate(batch_loader):
node_features = batched_graph.x.to(device)
edge_index = batched_graph.edge_index.to(device)
labels = batched_graph.y.to(device)
loss, score = evaluate_batch(node_features, edge_index, labels)
total_loss += loss
total_score += score
avg_loss = total_loss / (batch_id + 1)
avg_score = total_score / (batch_id + 1)
return avg_loss, avg_score