-
Notifications
You must be signed in to change notification settings - Fork 1
/
training.py
137 lines (100 loc) · 3.94 KB
/
training.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import torch
def train_model_single(dataloader, model, criterion, optimizer):
model.train()
running_loss = 0.0
y_pred = []
y_true = []
device = next(model.parameters()).device
for index, batch in enumerate(dataloader, 1):
inputs, labels, _, _ = batch
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs, 1)
loss.backward()
optimizer.step()
running_loss += loss.data.item()
y_pred.append(preds.cpu().numpy())
y_true.append(labels.cpu().numpy())
return running_loss / index, (y_true, y_pred)
def eval_model_single(dataloader, model, criterion):
model.eval()
running_loss = 0.0
y_pred = []
y_true = []
device = next(model.parameters()).device
with torch.no_grad():
for index, batch in enumerate(dataloader, 1):
inputs, labels, _, _ = batch
inputs = inputs.to(device)
labels = labels.to(device)
outputs = model(inputs)
loss = criterion(outputs, labels)
_, preds = torch.max(outputs, 1)
y_pred.append(preds.cpu().numpy())
y_true.append(labels.cpu().numpy())
running_loss += loss.data.item()
return running_loss / index, (y_true, y_pred)
def train_model_multi(dataloader, model, criterion_cat, criterion_cont, optimizer, gcn=False):
model.train()
running_loss = 0.0
running_loss_cat = 0.0
running_loss_cont = 0.0
y_pred = []
y_true = []
device = next(model.parameters()).device
for index, batch in enumerate(dataloader, 1):
inputs, labels, labels_cont, inp = batch
inputs = inputs.to(device)
labels = labels.to(device)
labels_cont = labels_cont.to(device)
inp = inp.to(device)
optimizer.zero_grad()
if gcn:
outputs_cat, outputs_cont = model(inputs, inp)
else:
outputs_cat, outputs_cont = model(inputs)
loss_cat = criterion_cat(outputs_cat, labels)
loss_cont = criterion_cont(outputs_cont.double(), labels_cont.double())
_, preds = torch.max(outputs_cat, 1)
loss = loss_cat + loss_cont
loss.backward()
optimizer.step()
running_loss += loss.data.item()
running_loss_cat += loss_cat.data.item()
running_loss_cont += loss_cont.data.item()
y_pred.append(preds.cpu().numpy())
y_true.append(labels.cpu().numpy())
return running_loss / index, running_loss_cat / index, running_loss_cont / index, (y_true, y_pred)
def eval_model_multi(dataloader, model, criterion_cat, criterion_cont, gcn=False):
model.eval()
running_loss = 0.0
running_loss_cat = 0.0
running_loss_cont = 0.0
y_pred = []
y_true = []
device = next(model.parameters()).device
with torch.no_grad():
for index, batch in enumerate(dataloader, 1):
inputs, labels, labels_cont, inp = batch
inputs = inputs.to(device)
labels = labels.to(device)
labels_cont = labels_cont.to(device)
inp = inp.to(device)
if gcn:
outputs_cat, outputs_cont = model(inputs, inp)
else:
outputs_cat, outputs_cont = model(inputs)
loss_cat = criterion_cat(outputs_cat, labels)
loss_cont = criterion_cont(
outputs_cont.double(), labels_cont.double())
_, preds = torch.max(outputs_cat, 1)
loss = loss_cat + loss_cont
y_pred.append(preds.cpu().numpy())
y_true.append(labels.cpu().numpy())
running_loss += loss.data.item()
running_loss_cat += loss_cat.data.item()
running_loss_cont += loss_cont.data.item()
return running_loss / index, running_loss_cat / index, running_loss_cont / index, (y_true, y_pred)