-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_test_utils.py
360 lines (290 loc) · 15.9 KB
/
train_test_utils.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import torch
from sklearn.metrics import balanced_accuracy_score, accuracy_score, f1_score, mean_squared_error
import itertools
import numpy as np
def binary_acc(y_pred, y_test, seq2seq):
y_pred_tag = torch.round(torch.sigmoid(y_pred))
if seq2seq:
correct_results_sum = (y_pred_tag == y_test._base).sum().float()
acc = correct_results_sum / len(y_test)
else:
correct_results_sum = (y_pred_tag == y_test).sum().float()
acc = correct_results_sum / y_test.unsqueeze(axis=0).shape[0]
acc = torch.round(acc * 100)
return acc
# https://stackoverflow.com/questions/58172188/how-to-add-l1-regularization-to-pytorch-nn-model
def l1_regularizer(model, lambda_l1=0.01, weight_or_bias='weight'):
lossl1 = 0
for model_param_name, model_param_value in model.named_parameters():
if weight_or_bias in model_param_name:
lossl1 += lambda_l1 * model_param_value.abs().sum()
return lossl1
def train_gru(model=None, criterion=None, optimizer=None, max_epochs=30, train_loader=None, val_loader=None, device=None, seq2seq=True, params=None):
# Loop over epochs
for epoch in range(max_epochs):
# Training
avg_loss = 0.
avg_acc = 0.
counter = 0
for local_batch, local_labels in train_loader:
counter += 1
h = model.init_hidden(batch_size=params['batch_size'])
h = h.to(device)
local_batch, local_labels = local_batch.to(device), local_labels.to(device)
if seq2seq:
local_batch = local_batch.squeeze(axis=0)
local_labels = local_labels.squeeze(axis=0)
local_batch = local_batch.unsqueeze(axis=1)
local_labels = local_labels.unsqueeze(axis=1)
else:
local_labels = local_labels[:, -1].to(device)
optimizer.zero_grad()
h = h.data
out, h = model(local_batch.float(), h.float())
loss = criterion(out.squeeze().to(device), local_labels.squeeze().float().to(device))
acc = binary_acc(out.squeeze(), local_labels, seq2seq)
total_loss = loss + l1_regularizer(model, lambda_l1=0.001, weight_or_bias='weight')
total_loss.backward()
optimizer.step()
avg_loss += total_loss.item()
avg_acc += acc.item()
if counter % 1000 == 0:
print(
"Epoch {}... Step: {}/{}... Average Loss for Epoch: {}... Accuracy: {}".format(epoch, counter, len(train_loader),
avg_loss / counter, avg_acc / counter))
if seq2seq:
evaluate_all_timesteps_per_subject(model=model, val_loader=val_loader, hidden=h, device=device)
else:
evaluate_last_timestep(model=model, val_loader=val_loader, device=device)
model.train()
return model, h
def train_gru_age(model=None, criterion=None, optimizer=None, max_epochs=30, train_loader=None, val_loader=None, device=None,
params=None):
# Loop over epochs
for epoch in range(max_epochs):
# Training
avg_loss = 0.
avg_acc = 0.
counter = 0
for local_batch, local_labels, local_ages in train_loader:
counter += 1
h = model.init_hidden(batch_size=local_batch.shape[0])
h = h.to(device)
local_batch, local_labels, local_ages = local_batch.to(device), local_labels.to(device), local_ages.to(device)
local_batch = local_batch.squeeze(axis=0)
local_labels = local_labels.squeeze(axis=0)
local_batch = local_batch.unsqueeze(axis=1)
local_labels = local_labels.unsqueeze(axis=1)
local_ages = local_ages.squeeze(axis=0)
optimizer.zero_grad()
h = h.data
out, h, out_age = model(local_batch.float(), h.float())
loss_score = criterion['score'](out.squeeze().to(device), local_labels.squeeze().float().to(device))
loss_age = criterion['age'](out_age.squeeze(), local_ages.squeeze().float())
# The loss is a combination of the BCE and the MSE for the age.
# I have weighted higher the BCE loss in this case after hyperparameter tuning
loss = loss_score + 0.2*loss_age
acc = binary_acc(out.squeeze(), local_labels)
# I am adding L1 regularization for the weights to minimize overfitting since out dataset is so small
total_loss = loss + l1_regularizer(model, lambda_l1=0.001, weight_or_bias='weight')
total_loss.backward()
optimizer.step()
avg_loss += total_loss.item()
avg_acc += acc.item()
# Select how often you want your results to be printed during training
if counter % 6000 == 0:
print(
"Epoch {}... Step: {}/{}... Average Loss for Epoch: {}... Accuracy: {}".format(epoch, counter, len(train_loader),
avg_loss / counter, avg_acc / counter))
#evaluate_all_timesteps_age(model=model, val_loader=val_loader, hidden=h, device=device)
# This function returns also the subject-level accuracy and macro accuracy
evaluate_all_timesteps_age_per_subject(model=model, val_loader=val_loader, hidden=h, device=device)
model.train()
return model, h
def evaluate_all_timesteps_age_per_subject(model=None, val_loader=None, hidden=None, device=None):
y_pred_list = []
y_pred_ages = []
y_test = []
y_test_ages = []
model.eval()
subject_acc = []
subject_bacc_control = []
subject_bacc_diseased = []
with torch.no_grad():
for local_batch, local_labels, local_ages in val_loader:
h = model.init_hidden(batch_size=local_batch.shape[0])
h = h.to(device)
local_batch, local_labels, local_ages = local_batch.to(device), local_labels.to(device), local_ages.to(device)
local_batch = local_batch.squeeze(axis=0)
local_labels = local_labels.squeeze(axis=0)
local_batch = local_batch.unsqueeze(axis=1)
local_labels = local_labels.unsqueeze(axis=1)
local_ages = local_ages.squeeze(axis=0)
h = h.data
out, h, out_ages = model(local_batch.float(), h.float())
y_pred_tag = torch.round(torch.sigmoid(out.squeeze()))
out_ages = out_ages.squeeze()
# We need to change the labels from an array of arrays to a normal array otherwise the accuracy is not
# calculated correctly
label_list = local_labels._base.cpu().numpy().tolist()
label_list = list(itertools.chain.from_iterable(label_list))
# Create a list of all the predictions and calculate the subject-level accuracy over all visits
if len(y_pred_tag.shape) == 0:
y_pred_list = y_pred_list + [(y_pred_tag.cpu().numpy().tolist())]
y_pred_ages = y_pred_ages + [(out_ages.cpu().numpy().tolist())]
subject_acc.append(accuracy_score(label_list, [y_pred_tag.cpu().numpy()]))
else:
y_pred_list = y_pred_list + (y_pred_tag.cpu().numpy().tolist())
y_pred_ages = y_pred_ages + (out_ages.cpu().numpy().tolist())
subject_acc.append(accuracy_score(label_list, y_pred_tag.cpu().numpy()))
# Keep the accuracies of control and diseased subjects separately so we can calculate the
# overall subject-level macro accuracy
if torch.sum(local_labels) > 0:
subject_bacc_diseased.append(subject_acc[-1])
else:
subject_bacc_control.append(subject_acc[-1])
y_test = y_test + (local_labels._base.cpu().numpy().tolist())
y_test_ages = y_test_ages + (local_ages._base.cpu().numpy().tolist())
y_test = list(itertools.chain.from_iterable(y_test))
y_test_ages = list(itertools.chain.from_iterable(y_test_ages))
subject_macro_accuracy = (sum(subject_bacc_diseased)/len(subject_bacc_diseased)
+ sum(subject_bacc_control)/len(subject_bacc_control))/2
results_dict = {'subject_accuracy': sum(subject_acc)/len(subject_acc),
'subject_macro_accuracy': subject_macro_accuracy,
'accuracy': accuracy_score(y_test, y_pred_list),
'balanced_accuracy': balanced_accuracy_score(y_test, y_pred_list),
'f1-score': f1_score(y_test, y_pred_list, average='macro'),
'mse-age': mean_squared_error(np.array(y_test_ages), np.array(y_pred_ages))}
# Subject-level means we calculate one score per subject over all visits.
# That way, subjects with more visits do not influence the results more than subjects with just one visit
# Afterwards we also calculate the overall results over all visits, regardless of subject
return results_dict
def evaluate_all_timesteps_per_subject(model=None, val_loader=None, hidden=None, device=None):
y_pred_list = []
y_test = []
model.eval()
subject_acc = []
subject_bacc_control = []
subject_bacc_diseased = []
with torch.no_grad():
for local_batch, local_labels in val_loader:
h = model.init_hidden(batch_size=local_batch.shape[0])
h = h.to(device)
local_batch, local_labels = local_batch.to(device), local_labels.to(device)
local_batch = local_batch.squeeze(axis=0)
local_labels = local_labels.squeeze(axis=0)
local_batch = local_batch.unsqueeze(axis=1)
local_labels = local_labels.unsqueeze(axis=1)
h = h.data
out, h = model(local_batch.float(), h.float())
y_pred_tag = torch.round(torch.sigmoid(out.squeeze()))
# We need to change it for an array of arrays to a normal array otherwise the accuracy is not
# calculated correctly
patata = local_labels._base.cpu().numpy().tolist()
patata = list(itertools.chain.from_iterable(patata))
# Create a list of all the predictions and calculate the subject-level accuracy
if len(y_pred_tag.shape) == 0:
y_pred_list = y_pred_list + [(y_pred_tag.cpu().numpy().tolist())]
subject_acc.append(accuracy_score(patata, [y_pred_tag.cpu().numpy()]))
else:
y_pred_list = y_pred_list + (y_pred_tag.cpu().numpy().tolist())
subject_acc.append(accuracy_score(patata, y_pred_tag.cpu().numpy()))
# Keep the accuracies of control and diseased subjects separately so we can calculate the
# overall subject-level macro accuracy
if torch.sum(local_labels) > 0:
subject_bacc_diseased.append(subject_acc[-1])
else:
subject_bacc_control.append(subject_acc[-1])
y_test = y_test + (local_labels._base.cpu().numpy().tolist())
y_test = list(itertools.chain.from_iterable(y_test))
subject_macro_accuracy = (sum(subject_bacc_diseased)/len(subject_bacc_diseased)
+ sum(subject_bacc_control)/len(subject_bacc_control))/2
results_dict = {'subject_accuracy': sum(subject_acc)/len(subject_acc),
'subject_macro_accuracy': subject_macro_accuracy,
'accuracy': accuracy_score(y_test, y_pred_list),
'balanced_accuracy': balanced_accuracy_score(y_test, y_pred_list),
'f1-score': f1_score(y_test, y_pred_list, average='macro')}
return results_dict
def acc_per_run_dataset_cross_sectional():
baseline_acc = 0.761871
baseline_bacc = 0.722522
baseline_f1 = 0.64044
return baseline_acc, baseline_bacc, baseline_f1
def evaluate_last_timestep(model=None, val_loader=None, device=None):
y_pred_list = []
y_test = []
model.eval()
with torch.no_grad():
# No age
if len(next(iter(val_loader))) == 2:
for local_batch, local_labels in val_loader:
local_batch = local_batch.to(device)
# local_labels = torch.max(local_labels)
local_labels = local_labels[:, -1].to(device)
h = model.init_hidden(batch_size=local_batch.shape[0])
h = h.to(device)
out, h = model(local_batch.float(), h.float())
y_pred_tag = torch.round(torch.sigmoid(out.squeeze()))
y_pred_list.append(y_pred_tag.cpu().numpy())
y_test.append(local_labels.cpu().numpy())
# With age
elif len(next(iter(val_loader))) == 3:
for local_batch, local_labels, local_ages in val_loader:
local_batch = local_batch.to(device)
# local_labels = torch.max(local_labels)
local_labels = local_labels[:, -1].to(device)
h = model.init_hidden(batch_size=local_batch.shape[0])
h = h.to(device)
out, h, out_age = model(local_batch.float(), h.float())
y_pred_tag = torch.round(torch.sigmoid(out.squeeze()))
y_pred_list.append(y_pred_tag.cpu().numpy())
y_test.append(local_labels.cpu().numpy())
y_pred_list = [a.squeeze().tolist() for a in y_pred_list]
y_test = [a.squeeze().tolist() for a in y_test]
# In this case subject accuracy and accuracy are the same since we have only one visit per subject
results_dict = {'subject_accuracy': accuracy_score(y_test, y_pred_list),
'subject_macro_accuracy': balanced_accuracy_score(y_test, y_pred_list),
'accuracy': accuracy_score(y_test, y_pred_list),
'balanced_accuracy': balanced_accuracy_score(y_test, y_pred_list),
'f1-score': f1_score(y_test, y_pred_list, average='macro')}
#print(results_dict)
return results_dict
def train_gru(model=None, criterion=None, optimizer=None, max_epochs=30, train_loader=None, val_loader=None, device=None, seq2seq=True, params=None):
# Loop over epochs
for epoch in range(max_epochs):
# Training
avg_loss = 0.
avg_acc = 0.
counter = 0
for local_batch, local_labels in train_loader:
counter += 1
h = model.init_hidden(batch_size=params['batch_size'])
h = h.to(device)
local_batch, local_labels = local_batch.to(device), local_labels.to(device)
if seq2seq:
local_batch = local_batch.squeeze(axis=0)
local_labels = local_labels.squeeze(axis=0)
local_batch = local_batch.unsqueeze(axis=1)
local_labels = local_labels.unsqueeze(axis=1)
else:
local_labels = local_labels[:, -1].to(device)
optimizer.zero_grad()
h = h.data
out, h = model(local_batch.float(), h.float())
loss = criterion(out.squeeze().to(device), local_labels.squeeze().float().to(device))
acc = binary_acc(out.squeeze(), local_labels, seq2seq)
total_loss = loss + l1_regularizer(model, lambda_l1=0.001, weight_or_bias='weight')
total_loss.backward()
optimizer.step()
avg_loss += total_loss.item()
avg_acc += acc.item()
if counter % 1000 == 0:
print(
"Epoch {}... Step: {}/{}... Average Loss for Epoch: {}... Accuracy: {}".format(epoch, counter, len(train_loader),
avg_loss / counter, avg_acc / counter))
if seq2seq:
evaluate_all_timesteps_per_subject(model=model, val_loader=val_loader, hidden=h, device=device)
else:
evaluate_last_timestep(model=model, val_loader=val_loader, device=device)
model.train()
return model, h