-
Notifications
You must be signed in to change notification settings - Fork 2
/
validation.py
54 lines (42 loc) · 1.6 KB
/
validation.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
import torch
from sklearn import metrics
import torch.nn.functional as F
import wandb
import numpy as np
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def validate(model, data_loader, loss_history):
model.eval()
total_samples = len(data_loader.dataset)
correct_samples = 0
total_loss = 0
global_target = np.array([])
global_pred = np.array([])
with torch.no_grad():
for data, target in data_loader:
res = model(data)
res = res.to(device)
output = F.log_softmax(res, dim=1)
target = target.to(device)
output = output.to(device)
loss = F.nll_loss(output, target, reduction='sum')
_, pred = torch.max(output, dim=1)
total_loss += loss.item()
correct_samples += pred.eq(target).sum()
target = target.cpu().detach().numpy()
pred = pred.cpu().detach().numpy()
global_target = np.concatenate((global_target, target))
global_pred = np.concatenate((global_pred, pred))
avg_loss = total_loss / total_samples
acc = 100.0 * correct_samples / total_samples
loss_history.append(avg_loss)
f1_score = metrics.f1_score(global_target, global_pred, average='micro')
wandb.log({
'val_loss': loss.item(),
'val_f1_score': f1_score,
'val_accuracy': acc
})
print('\nAverage validation loss: ' + '{:.4f}'.format(avg_loss) +
' Accuracy:' + '{:5}'.format(correct_samples) + '/' +
'{:5}'.format(total_samples) + ' (' +
'{:4.2f}'.format(acc) + '%)\n')
return acc