-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
529 lines (398 loc) · 17.1 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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import numpy as np
import pandas as pd
import torch
from torch import nn, optim
from torch.utils.data import Dataset, DataLoader, WeightedRandomSampler, Subset
from sklearn.model_selection import train_test_split, StratifiedKFold
import h5py
from collections import Counter
import matplotlib.pyplot as plt
import os
from dataclasses import dataclass
from typing import Callable, Any
from enum import Enum
import csv
###############################################################
# UTILITIES
###############################################################
def new_file_name(name: str, extension: str):
num = 1
files = {f for f in os.listdir('.') if os.path.isfile(f)}
while f"{name}_{num}.{extension}" in files:
num += 1
return f"{name}_{num}.{extension}"
# Redefine print so that we get real-time logs
log_fname = new_file_name("log", "txt")
def print(msg):
with open(log_fname, "a") as file:
file.write(str(msg) + "\n")
print("Beginning log")
###############################################################
# EXTRACT IDXS TRAINING / TESTING
###############################################################
# Function
def extract_ids(tissues, list_excluding_tissues, test_size, num_folds):
# Tissues
tissues_dict = {}
i = 0
for tissue in tissues:
if tissue not in tissues_dict:
tissues_dict[tissue] = i
i += 1
# Full sample
tissue = [tissues_dict[tissue] for tissue in tissues]
idx = np.arange(len(tissue))
# Train / Test outer level
idx_train_out, idx_test_out = train_test_split(idx, test_size=int(len(idx)*test_size), stratify = tissue, random_state=1)
tissue_train_out = [tissue[idx] for idx in idx_train_out]
# Train / Test 2nd level
stratified_kfold = StratifiedKFold(n_splits=num_folds, shuffle=True, random_state=42)
idx_train_in = []
idx_test_in = []
for idx_train, idx_test in stratified_kfold.split(idx_train_out, tissue_train_out):
# Excluding
tissue_exclude = [tissues_dict[tissue] for tissue in list_excluding_tissues]
tissue_train = [tissue[idx] for idx in idx_train]
idx_include = []
idx_exclude = []
for idx, tissue_label in zip(idx_train, tissue_train):
if tissue_label in tissue_exclude:
idx_exclude.append(idx)
else:
idx_include.append(idx)
idx_train = idx_include
idx_train_in.append(idx_train)
idx_test_in.append(idx_test)
#Return
return (idx_train_out, idx_test_out, idx_train_in, idx_test_in, tissue)
# Load tissues
"""
tissues_pd = pd.read_csv("tissues.tsv.gz", sep = "\t", compression = "gzip", header = None)
tissues = tissues_pd.values
tissues = np.squeeze(tissues, axis=1)
tissues = tissues.tolist()[1:]
"""
f_gtex_gene = h5py.File('/dtu-compute/datasets/iso_02456/hdf5/gtex_gene_expression_norm_transposed.hdf5', mode='r')
tissues = list(f_gtex_gene['tissue'][:])
tissues = [byte_tissue.decode('utf-8') for byte_tissue in tissues]
list_excluding_tissues = [
"Brain - Amygdala",
"Brain - Anterior cingulate cortex (BA24)",
"Brain - Caudate (basal ganglia)",
"Brain - Cerebellar Hemisphere",
"Brain - Cerebellum",
"Brain - Cortex",
"Brain - Frontal Cortex (BA9)",
"Brain - Hippocampus",
"Brain - Hypothalamus",
"Brain - Nucleus accumbens (basal ganglia)"
]
test_size = 0.2
num_folds = 5
idx_train_out, idx_test_out, idx_train_in, idx_test_in, tissue = extract_ids(tissues, list_excluding_tissues, test_size, num_folds)
###############################################################
# CREATE CLASS DATASET
###############################################################
class Dataset(torch.utils.data.Dataset):
def __init__(self, model, num_comp_pca):
f_gtex_isoform = h5py.File('/dtu-compute/datasets/iso_02456/hdf5/gtex_isoform_expression_norm_transposed.hdf5', mode='r')
f_gtex_gene = h5py.File('/dtu-compute/datasets/iso_02456/hdf5/gtex_gene_expression_norm_transposed.hdf5', mode='r')
f_gtex_pca = h5py.File('datasets_reduced/PCA.hdf5', mode='r')
f_gtex_VAE_100 = h5py.File("datasets_reduced/VAE_100.hdf5", mode="r")
f_gtex_VAE_500 = h5py.File("datasets_reduced/VAE_500.hdf5", mode="r")
self.dset_isoform = f_gtex_isoform['expressions']
self.dset_gene = f_gtex_gene['expressions']
self.dset_pca = f_gtex_pca['expressions'][:,:num_comp_pca]
self.dset_VAE_100 = f_gtex_VAE_100['expressions']
self.dset_VAE_500 = f_gtex_VAE_500['expressions']
assert(self.dset_gene.shape[0] == self.dset_isoform.shape[0])
assert(self.dset_pca.shape[0] == self.dset_isoform.shape[0])
assert(self.dset_VAE_100.shape[0] == self.dset_isoform.shape[0])
assert(self.dset_VAE_500.shape[0] == self.dset_isoform.shape[0])
match model:
case "original":
self.x = self.dset_gene
case "pca":
self.x = self.dset_pca
case "VAE_100":
self.x = self.dset_VAE_100
case "VAE_500":
self.x = self.dset_VAE_500
def __len__(self):
return self.dset_isoform.shape[0]
def __getitem__(self, idx):
return self.x[idx], self.dset_isoform[idx]
###############################################################
# LOAD DATA
###############################################################
models = ["original","pca","VAE_100","VAE_500"]
num_comp_pca = 100
batch_strat = True
train_out = []
test_out = []
train_in = []
test_in = []
for model in models:
## INNER
dataloader_train_in_list = []
dataloader_test_in_list = []
for fold in range(num_folds):
full_data = Dataset(model, num_comp_pca)
###############################################################
# DATASETS
###############################################################
dataset_train_in = Subset(full_data, indices = idx_train_in[fold])
dataset_test_in = Subset(full_data, indices = idx_test_in[fold])
###############################################################
# DATALOADERS
###############################################################
batch_size = 64
if batch_strat:
# Balance train_loader
tissue_train = [tissue[idx] for idx in idx_train_in[fold]]
tissue_train_tensor = torch.tensor(tissue_train)
class_counts = torch.bincount(tissue_train_tensor)
weights = 1.0 / class_counts[tissue_train]
train_sampler = WeightedRandomSampler(weights, len(weights))
# Dataloaders
dataloader_train_in = DataLoader(dataset_train_in, batch_size=batch_size, sampler = train_sampler)
dataloader_test_in = DataLoader(dataset_test_in, batch_size=batch_size, shuffle=False)
else:
dataloader_train_in = DataLoader(dataset_train_in, batch_size=batch_size, shuffle=True)
dataloader_test_in = DataLoader(dataset_test_in, batch_size=batch_size, shuffle=False)
# Appending
dataloader_train_in_list.append(dataloader_train_in)
dataloader_test_in_list.append(dataloader_test_in)
## OUTER
###############################################################
# DATASETS
###############################################################
dataset_train_out = Subset(full_data, indices = idx_train_out)
dataset_test_out = Subset(full_data, indices = idx_test_out)
###############################################################
# DATALOADERS
###############################################################
if batch_strat:
# Balance train_loader
tissue_train = [tissue[idx] for idx in idx_train_out]
tissue_train_tensor = torch.tensor(tissue_train)
class_counts = torch.bincount(tissue_train_tensor)
weights = 1.0 / class_counts[tissue_train]
train_sampler = WeightedRandomSampler(weights, len(weights))
# Dataloaders
batch_size = 64
dataloader_train_out = DataLoader(dataset_train_out, batch_size=batch_size, sampler = train_sampler)
dataloader_test_out = DataLoader(dataset_test_out, batch_size=batch_size, shuffle=False)
else:
dataloader_train_out = DataLoader(dataset_train_out, batch_size=batch_size, shuffle = True)
dataloader_test_out = DataLoader(dataset_test_out, batch_size=batch_size, shuffle=False)
## APPENDING
train_in.append(dataloader_train_in_list)
test_in.append(dataloader_test_in_list)
train_out.append(dataloader_train_out)
test_out.append(dataloader_test_out)
###############################################################
# 6-FOLD CROSS-VALIDATION
###############################################################
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
NUM_OUTPUT = next(iter(train_in[0][0]))[1].shape[1]
print(f"NUM_OUTPUT: {NUM_OUTPUT}")
NUM_FOLDS = 5
NUM_EPOCHS = 100
# Network model
class Net(nn.Module):
def __init__(self, num_features):
super(Net, self).__init__()
activation_fn = nn.ReLU
num_hidden = 20
self.net = nn.Sequential(
nn.Linear(num_features, num_hidden),
activation_fn(),
nn.Linear(num_hidden, NUM_OUTPUT)
)
def forward(self, x):
return self.net(x)
# Baseline model
class BaselineModel(Callable):
BASELINE_CSV = pd.read_csv("baseline.csv")
AVGS = torch.tensor(BASELINE_CSV["averages"].to_numpy()).to(DEVICE)
def __call__(self, inputs):
return self.AVGS.repeat(inputs.shape[0], 1)
def to(self, _):
pass
def train(self):
pass
def eval(self):
pass
@dataclass
class ModelInfo:
model_type: str
create_model: Callable[[], Callable]
requested_loader: int
is_baseline: bool = False
model_infos = [
ModelInfo("Original", lambda: Net(18965), 0),
ModelInfo("PCA", lambda: Net(100), 1),
ModelInfo("VAE 100", lambda: Net(100), 2),
ModelInfo("VAE 500", lambda: Net(500), 3),
ModelInfo("Baseline", lambda: BaselineModel(), 0, is_baseline = True)
]
@dataclass
class ModelResult:
train_losses: list[Any]
test_loss: Any
@dataclass
class OuterModelResult:
min_train_losses: list[Any]
average_test_loss: Any
average_model_results = []
print("Beginning training")
for model_idx, model_info in enumerate(model_infos):
# For each model, run 5 folds, and find the average test error
# Extract model info
model_type = model_info.model_type
print(f"Using model {model_type}")
requested_loader = model_info.requested_loader
fold_model_results: list[ModelResult] = []
for fold in range(NUM_FOLDS):
print(f"Fold number {fold}")
model = model_info.create_model()
model.to(DEVICE)
# print(model)
loss_fn = nn.MSELoss()
if not model_info.is_baseline:
optimizer = optim.Adam(model.parameters(), lr=1e-3)
### TRAINING
model.train()
i = 0
train_losses = []
for epoch in range(NUM_EPOCHS):
# print(f"epoch number {epoch}")
total_epoch_loss = 0
batch_num = 0
for inputs, targets in train_in[requested_loader][fold]:
# print(f"\tbatch number {batch_num}")
inputs, targets = inputs.to(DEVICE), targets.to(DEVICE)
# Forward pass, compute gradients, perform one training step.
# Your code here!
# Forward pass.
output = model(inputs)
# Compute loss.
loss = loss_fn(output, targets)
# print(f"\tloss: {loss}")
if not model_info.is_baseline:
# Clean up gradients from the model.
optimizer.zero_grad()
# Compute gradients based on the loss from the current batch (backpropagation).
loss.backward()
# Take one optimizer step using the gradients computed in the previous step.
optimizer.step()
# Increment step counter
total_epoch_loss += loss
batch_num += 1
train_epoch_loss = total_epoch_loss / batch_num
# print(f"\ttraining loss: {train_loss}")
train_losses.append(train_epoch_loss)
if not model_info.is_baseline:
torch.save(model.state_dict(), "models_100epochssimple/" + str(model_type) + "_" + str(fold) + ".pth")
print("Finished training.")
### TESTING
with torch.no_grad():
model.eval()
total_loss = 0
batch_num = 0
for inputs, targets in test_in[requested_loader][fold]:
# print(f"\ttesting batch number {batch_num}")
inputs, targets = inputs.to(DEVICE), targets.to(DEVICE)
output = model(inputs)
loss = loss_fn(output, targets)
# print(f"\tloss: {loss}")
batch_num += 1
total_loss += loss
test_loss = total_loss / batch_num
fold_model_results.append(ModelResult(train_losses, test_loss))
average_test_loss = sum(e.test_loss for e in fold_model_results) / NUM_FOLDS
min_train_losses = min(fold_model_results, key = lambda e: e.test_loss).train_losses
average_model_results.append((model_info, OuterModelResult(min_train_losses, average_test_loss)))
# Save results
train_results_dict = {
info.model_type: [e.cpu().detach().numpy() for e in result.min_train_losses] for (info, result) in average_model_results
}
inner_train_loss_file = new_file_name("inner_train_losses", "csv")
pd.DataFrame(train_results_dict).to_csv(inner_train_loss_file, index=False)
print(f"Wrote inner train loss to {inner_train_loss_file}")
test_results_dict = {
info.model_type: [result.average_test_loss.cpu().detach().numpy()] for (info, result) in average_model_results
}
inner_test_loss_file = new_file_name("inner_test_losses", "csv")
pd.DataFrame(test_results_dict).to_csv(inner_test_loss_file, index=False)
print(f"Wrote inner test loss to {inner_test_loss_file}")
###############################################################
# OUTER TESTING
###############################################################
### SELECTION
best_model_info, best_model_results = min(average_model_results, key = lambda e: e[1].average_test_loss)
print(f"Model {best_model_info.model_type} had the best performance with an average loss of {best_model_results.average_test_loss}")
best_model = best_model_info.create_model()
best_model = best_model.to(DEVICE)
best_model_loader = best_model_info.requested_loader
best_model_type = best_model_info.model_type
model = best_model
loader = best_model_loader
### TRAINING
print("Beginning final training")
best_model.train()
optimizer = optim.Adam(best_model.parameters(), lr=1e-3)
NUM_EPOCHS = 10
train_losses = []
for epoch in range(NUM_EPOCHS):
# print(f"epoch number {epoch}")
total_loss = 0
batch_num = 0
for inputs, targets in train_out[best_model_loader]:
# print(f"\tbatch number {batch_num}")
inputs, targets = inputs.to(DEVICE), targets.to(DEVICE)
# Forward pass, compute gradients, perform one training step.
# Your code here!
# Forward pass.
output = best_model(inputs)
# Compute loss.
loss = loss_fn(output, targets)
# print(f"\tloss: {loss}")
optimizer.zero_grad()
# Compute gradients based on the loss from the current batch (backpropagation).
loss.backward()
# Take one optimizer step using the gradients computed in the previous step.
optimizer.step()
# Increment step counter
total_loss += loss
batch_num += 1
train_loss = total_loss / batch_num
# print(f"\ttraining loss: {train_loss}")
train_losses.append(train_loss)
### FINAL TESTING
print(f"Final test on model {best_model_type}")
best_model.eval()
total_loss = 0
batch_num = 0
model_losses = []
with torch.no_grad():
for inputs, targets in test_out[best_model_loader]:
# print(f"Batch number {batch_num}")
inputs, targets = inputs.to(DEVICE), targets.to(DEVICE)
output = best_model(inputs)
loss = loss_fn(output, targets)
total_loss += loss
batch_num += 1
average_loss = total_loss / batch_num
model_losses.append(average_loss)
model_average = sum(model_losses) / len(model_losses)
print(f"Model {best_model_type} had an average loss of {model_average} in the final test")
# Save results
outer_train_loss_file = new_file_name("outer_train_losses", "csv")
pd.DataFrame([e.cpu().detach().numpy() for e in train_losses], columns=[f"{best_model_type} train loss"]).to_csv(outer_train_loss_file, index=False)
print(f"Wrote outer train loss to {outer_train_loss_file}")
outer_test_loss_file = new_file_name("outer_test_loss", "txt")
with open(outer_test_loss_file, "w") as f:
f.write(str(model_average))
print(f"Wrote outer test loss to {outer_test_loss_file}")