-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
28 lines (22 loc) · 765 Bytes
/
utils.py
File metadata and controls
28 lines (22 loc) · 765 Bytes
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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from sklearn.metrics import roc_auc_score, average_precision_score, f1_score
import networkx as nx
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import random
def cal_accuracy(preds, trues):
preds = torch.argmax(preds, dim=-1)
correct = (preds == trues).sum()
return correct / len(trues)
def cal_F1(preds, trues):
preds = torch.argmax(preds, dim=-1)
weighted_f1 = f1_score(trues, preds, average='weighted')
macro_f1 = f1_score(trues, preds, average='macro')
return weighted_f1, macro_f1
def cal_AUC_AP(scores, trues):
auc = roc_auc_score(trues, scores)
ap = average_precision_score(trues, scores)
return auc, ap