-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
74 lines (62 loc) · 1.96 KB
/
Copy pathevaluation.py
File metadata and controls
74 lines (62 loc) · 1.96 KB
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
from __future__ import annotations
from dataclasses import dataclass
import numpy as np
import torch
from sklearn.metrics import (
accuracy_score,
f1_score,
matthews_corrcoef,
precision_score,
recall_score,
roc_auc_score,
)
from tqdm import tqdm
@dataclass(frozen=True)
class BinaryMetrics:
auc: float
f1: float
precision: float
recall: float
accuracy: float
mcc: float
def compute_binary_metrics(
scores: np.ndarray,
labels: np.ndarray,
threshold: float = 0.5,
) -> BinaryMetrics:
predictions = (scores > threshold).astype(int)
auc = float("nan")
if np.unique(labels).size > 1:
auc = float(roc_auc_score(labels, scores))
return BinaryMetrics(
auc=auc,
f1=float(f1_score(labels, predictions, zero_division=0)),
precision=float(precision_score(labels, predictions, zero_division=0)),
recall=float(recall_score(labels, predictions, zero_division=0)),
accuracy=float(accuracy_score(labels, predictions)),
mcc=float(matthews_corrcoef(labels, predictions)),
)
def evaluate_loader(
model: torch.nn.Module,
loader,
device: str | torch.device,
threshold: float = 0.5,
max_batches: int | None = None,
) -> BinaryMetrics:
model.eval()
preds = []
ground_truths = []
for batch_index, sampled_data in enumerate(tqdm(loader, desc="Evaluating")):
if max_batches is not None and batch_index >= max_batches:
break
with torch.no_grad():
sampled_data = sampled_data.to(device)
preds.append(model(sampled_data).detach().cpu())
ground_truths.append(
sampled_data["MH", "link", "MH"].edge_label.detach().cpu()
)
if not preds:
raise ValueError("No batches were evaluated.")
scores = torch.cat(preds, dim=0).numpy()
labels = torch.cat(ground_truths, dim=0).numpy()
return compute_binary_metrics(scores, labels, threshold=threshold)