-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_test.py
More file actions
66 lines (55 loc) · 2.06 KB
/
Copy pathtrain_test.py
File metadata and controls
66 lines (55 loc) · 2.06 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
import torch
import torch.nn.functional as F
def train_model(train_loader, model, optimizer):
model.train()
train_accs = []
for train_data in train_loader:
optimizer.zero_grad()
x, y_true = train_data
x = x.to(model.device)
y_true = y_true.to(model.device)
y_pred = model(x)
train_loss = F.mse_loss(y_pred, y_true)
train_loss.backward()
optimizer.step()
train_acc = F.l1_loss(y_pred, y_true)
train_accs.append(train_acc)
return sum(train_accs) / len(train_accs)
def test_model(test_loader, model):
model.eval()
test_accs = []
with torch.no_grad():
for test_data in test_loader:
x, y_true = test_data
x = x.to(model.device)
y_true = y_true.to(model.device)
y_pred = model(x)
test_acc = F.l1_loss(y_pred, y_true)
test_accs.append(test_acc)
return sum(test_accs) / len(test_accs)
def display_test_stats(test_loader, model):
model.eval()
test_accs = []
print("This ran")
predictions = []
truths = []
with torch.no_grad():
for test_data in test_loader:
x, y_true = test_data
truths.append(y_true)
x = x.to(model.device)
y_true = y_true.to(model.device)
y_pred = model(x) #todo; see what this is predicting by usinjg printouts, can it predict a class 1 or 0?
# can another one be trained to try and predict birth sex?
#print('y_pred', y_pred)
predictions.append(y_pred)
#predictions.append(y_pred)
test_acc = F.l1_loss(y_pred, y_true) #gets the mean element-wise absolute value difference;
# this
test_accs.append(test_acc)
#stats we want; average accuracy (sum of accs / len of accs)
#we want to update this code so that it spits back the accuracy of the scanned ages and birth ages
#
#
mae = F.l1_loss(y_pred, y_true)
return (sum(test_accs) / len(test_accs)), predictions, truths, mae