-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonkey_utils.py
More file actions
59 lines (46 loc) · 1.96 KB
/
Copy pathmonkey_utils.py
File metadata and controls
59 lines (46 loc) · 1.96 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
import torch
import torch.nn as nn
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import auc
from sklearn.metrics import r2_score
from scipy.stats import ttest_rel
def train_network(net, train_loader, epochs = 50):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net.to(device)
criterion = nn.MSELoss()
optimizer = optim.SGD(net.parameters(), lr = 0.001, momentum=0.9)
for epoch in range(epochs):
running_loss = 0.0
for i, data in enumerate(train_loader):
inputs,labels = data
inputs, labels = inputs.to(device), labels.to(device)
if np.isnan(inputs.cpu()).any().item() > 0 or np.isnan(labels.cpu().any().item()) > 0:
print(np.isnan(inputs.cpu()).any(), np.isnan(labels.cpu().any()))
optimizer.zero_grad()
outputs, states = net(inputs, reset_states = True)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 500 == 499:
print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 500:.3f}')
running_loss = 0.0
return net
def test_network(net, test_loader):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
r2_res = 0
net.eval()
with torch.no_grad():
all_preds = []
all_labels = []
for data in test_loader:
inputs, labels = data
inputs, labels = inputs.to(device), labels.to(device)
preds, states = net(inputs, reset_states = True)
preds = preds.squeeze(); labels = labels.squeeze()
all_preds.append(preds); all_labels.append(labels)
all_preds, all_labels = torch.cat(all_preds).cpu(), torch.cat(all_labels).cpu()
r2 = r2_score(all_labels, all_preds)
return r2