-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
33 lines (30 loc) · 1.1 KB
/
test.py
File metadata and controls
33 lines (30 loc) · 1.1 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
import CNN
import torch
from FEN_conversion import fen_to_tensor
import json
import torch.nn as nn
def test(path):
model = CNN.CNN()
model.load_state_dict(torch.load(path))
model.eval()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
with torch.no_grad():
with open("Dataset/test_dataset") as file:
total_actual = []
total_pred = []
for line in file:
data = json.loads(line)
fen = data["fen"]
cp = data["cp"]
tensor = fen_to_tensor(fen).to(device)
tensor = tensor.unsqueeze(0)
cp_final = torch.tensor([data["cp"] / 100.0], dtype=torch.float32).to(device)
pred = model(tensor)
total_pred.append(pred.item())
total_actual.append(cp_final.item())
MSE = nn.MSELoss()
final_prediction = torch.tensor(total_pred)
final_actual = torch.tensor(total_actual)
loss = MSE(final_prediction, final_actual)
return loss.item()