-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
29 lines (23 loc) · 849 Bytes
/
Copy pathtest.py
File metadata and controls
29 lines (23 loc) · 849 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
29
import torch
import numpy as np
def test_model(test_loader, model, criterion, device):
# Testing
model.eval()
predictions = [] # initialize an empty list for predictions
y_trues = [] # initialize an empty list for targets
with torch.no_grad():
test_loss = 0.0
for inputs, targets in test_loader:
inputs = inputs.to(device)
targets = targets.to(device)
outputs = model(inputs)
y_pred = outputs.detach().cpu().numpy()
y_true = targets.detach().cpu().numpy()
predictions.append(y_pred)
y_trues.append(y_true)
test_loss += criterion(outputs, targets).item()
predictions = np.concatenate(predictions, axis=0)
y_trues = np.concatenate(y_trues, axis=0)
test_loss /= len(test_loader)
print(f'Test Loss: {test_loss:.4f}')
return test_loss, predictions, y_trues