forked from maloooon/SuMo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FCNN.py
1193 lines (890 loc) · 52.5 KB
/
FCNN.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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import pandas as pd
import torch
from torch import nn
import numpy as np
from pycox import models
import torchtuples as tt
from pycox.evaluation import EvalSurv
from torch.optim import Adam
import os
import optuna
import matplotlib as plt
class NN_changeable(nn.Module):
def __init__(self,views,in_features,n_hidden_layers_dims =None,
activ_funcs = None,dropout_prob = None, dropout_layers = None,
batch_norm = None, dropout_bool = None, batch_norm_bool = None,print_bool = False,
prelu_init = 0.25):
"""
Fully Connected Neural Net with changeable hyperparameters. Each view has a FCNN itself, finally the output of
each view is concatenated and passed through a final layer, which compresses values to a single dimensional
value used for the Proportional Hazards Model.
:param views: Views (Omes) ; dtype : List of Strings
:param in_features: Input dimensions for each view : List of Int
:param n_hidden_layers_dims: Hidden layers for each view : List of Lists of Int
:param activ_funcs: Activation Functions (for each view) aswell as for the last layer
; dtype : List of Lists of Strings ['relu', 'sigmoid', 'prelu']
:param dropout : Probability of Neuron Dropouts ; dtype : Int
:param dropout_layers : Layers in which to apply Dropout ; dtype : List of Lists of Strings ['yes','no']
:param batch_norm : Layers in which to apply Batch Normalization ; dtype : List of Lists of Strings ['yes','no']
:param dropout_bool : Decide whether Dropout is to be applied or not ; dtype : Boolean
:param batch_norm_bool : Decide whether Batch Normalization is to be applied or not ; dtype : Boolean
:param ae_bool : Check if data input comes from an Autoencoder [needed because of different data structure]
; dtype : Boolean
:param print_bool : Decide whether to print the model ; dtype : Boolean
:param prelu_init : Initial Value for PreLU activation ; dtype : Int
"""
super().__init__()
self.views =views
self.in_features = in_features
self.n_hidden_layers_dims = n_hidden_layers_dims
self.activ_funcs = activ_funcs
self.dropout_prob = dropout_prob
self.dropout_layers = dropout_layers
self.batch_norm = batch_norm
self.dropout_bool = dropout_bool
self.batch_norm_bool = batch_norm_bool
# Create list of lists which will store each hidden layer call for each view
self.hidden_layers = nn.ParameterList([nn.ParameterList([]) for x in range(len(in_features))])
self.print_bool = print_bool
self.prelu_init = prelu_init
# If we just input one activation function, use this activation function for each view and also the final layer
if len(activ_funcs) == 1 and type(activ_funcs[0]) is not list:
func = activ_funcs[0]
activ_funcs = [[func] for x in range(len(views) + 1)]
if len(activ_funcs) == len(views) + 1:
for c,view in enumerate(activ_funcs):
# If only one activ function given in sublist, use this for each layer
if len(activ_funcs[c]) == 1 and c != len(views):
# -1 because we already have one activ func in our activ funcs list
for x in range(len(n_hidden_layers_dims[c]) -1):
activ_funcs[c].append(activ_funcs[c][0])
# Replace strings with actual activation functions
for c2,activfunc in enumerate(view):
if activfunc.lower() == 'relu':
activ_funcs[c][c2] = nn.ReLU()
elif activfunc.lower() == 'sigmoid':
activ_funcs[c][c2] = nn.Sigmoid()
elif activfunc.lower() == 'prelu':
activ_funcs[c][c2] = nn.PReLU(init=prelu_init)
else:
raise ValueError("Your activation function input seems to be wrong. Check if it is a list of lists with a"
" sublist for each view and one list for the output layer or just a single activation function"
" value in a list")
# Assign Layers
for c,view in enumerate(n_hidden_layers_dims):
for c2 in range(len(view)):
if c2 == 0: # First Layer
# Batch Normalization
if batch_norm_bool == True and batch_norm[c][c2] == 'yes':
# Use an activation function
if activ_funcs[c][c2] != 'none':
self.hidden_layers[c].append(nn.Sequential(nn.Linear(in_features[c],
n_hidden_layers_dims[c][c2]),
nn.BatchNorm1d(n_hidden_layers_dims[c][c2]),
activ_funcs[c][c2]))
# Use no activation function
else:
self.hidden_layers[c].append(nn.Sequential(nn.Linear(in_features[c],
n_hidden_layers_dims[c][c2]),
nn.BatchNorm1d(n_hidden_layers_dims[c][c2])
))
# No Batch Normalization
else:
# Use an activation function
if activ_funcs[c][c2] != 'none':
self.hidden_layers[c].append(nn.Sequential(nn.Linear(in_features[c],
n_hidden_layers_dims[c][c2]),
activ_funcs[c][c2]))
# Use no activation function
else:
self.hidden_layers[c].append(nn.Sequential(nn.Linear(in_features[c],
n_hidden_layers_dims[c][c2])
))
else: # Other Layers
# Batch Normalization
if batch_norm_bool == True and batch_norm[c][c2] == 'yes':
# Use an activation function
if activ_funcs[c][c2] != 'none':
self.hidden_layers[c].append(nn.Sequential(nn.Linear(n_hidden_layers_dims[c][c2-1],
n_hidden_layers_dims[c][c2]),
nn.BatchNorm1d(n_hidden_layers_dims[c][c2]),
activ_funcs[c][c2]))
# Use no activation function
else:
self.hidden_layers[c].append(nn.Sequential(nn.Linear(n_hidden_layers_dims[c][c2-1],
n_hidden_layers_dims[c][c2]),
nn.BatchNorm1d(n_hidden_layers_dims[c][c2])
))
# No Batch Normalization
else:
# Use an activation function
if activ_funcs[c][c2] != 'none':
self.hidden_layers[c].append(nn.Sequential(nn.Linear(n_hidden_layers_dims[c][c2-1],
n_hidden_layers_dims[c][c2]),
activ_funcs[c][c2]))
# Use no activation function
else:
self.hidden_layers[c].append(nn.Sequential(nn.Linear(n_hidden_layers_dims[c][c2-1],
n_hidden_layers_dims[c][c2])
))
sum_dim_last_layers = sum([dim[-1] for dim in n_hidden_layers_dims])
# Final Layer
if activ_funcs[-1][0] != 'none':
# Activation function
if batch_norm_bool == True and batch_norm[-1][0] == 'yes':
# Batch Normalization
self.final_out = nn.Sequential(nn.Linear(sum_dim_last_layers,1),nn.BatchNorm1d(1), activ_funcs[-1][0])
else:
# No Batch Normalization
self.final_out = nn.Sequential(nn.Linear(sum_dim_last_layers,1), activ_funcs[-1][0])
else:
# No activation function
if batch_norm_bool == True and batch_norm[-1][0] == 'yes':
self.final_out = nn.Sequential(nn.Linear(sum_dim_last_layers,1),nn.BatchNorm1d(1))
else:
self.final_out = nn.Sequential(nn.Linear(sum_dim_last_layers,1))
# Dropout
self.dropout = nn.Dropout(self.dropout_prob)
if print_bool == True:
# Print the model
print("Data input has the following views : {}, each containing {} features.".format(self.views,
self.in_features))
print("Dropout : {}, Batch Normalization : {}".format(dropout_bool, batch_norm_bool))
for c,_ in enumerate(self.views):
print("The view {} has the following pipeline : {}".format(_, self.hidden_layers[c],
))
if dropout_bool == True:
print("dropout in layers : {}".format(dropout_layers[c]))
print("Finally, the last output of each layer is summed up ({} features) and casted to a single element, "
"the hazard".format(sum_dim_last_layers))
def forward(self,*x):
"""
Forward function of the Fully Connected Neural Net
:param x: Data Input (for each view) ; dtype : Tuple/List of Tensor(n_samples_in_batch, n_features)
:return: "Risk ratio" ; dtype : Tensor(n_samples_in_batch,1)
"""
if type(x[0]) is list:
x = tuple(x[0])
# List of lists to store encoded features for each view
encoded_features = [[] for x in range(len(self.views))]
# Data ordered by view
data_ordered = list(x)
# Take arbitrary view for batch size, since for each view same batch size
batch_size = x[0].size(0)
# Pass data through layers and apply Dropout if wanted
for c,view in enumerate(self.hidden_layers):
for c2,encoder in enumerate(view):
if c2 == 0: #first layer
# Apply dropout layer
if self.dropout_bool == True and self.dropout_layers[c][c2] == 'yes':
# encoded_features[c][c2] = self.dropout(encoded_features[c][c2])
data_ordered[c] = self.dropout(data_ordered[c])
encoded_features[c].append(self.hidden_layers[c][c2](data_ordered[c]))
else : # other layers
if self.dropout_bool == True and self.dropout_layers[c][c2] == 'yes':
encoded_features[c][c2-1] = self.dropout(encoded_features[c][c2-1])
encoded_features[c].append(self.hidden_layers[c][c2](encoded_features[c][c2-1]))
# Concatenate output for final layer
final_in = torch.cat(tuple([dim[-1] for dim in encoded_features]), dim=-1)
if self.dropout_bool == True and self.dropout_layers[-1][0] == 'yes':
final_in = self.dropout(final_in)
predict = self.final_out(final_in)
return predict
def objective(trial, n_fold, t_preprocess,feature_selection_type,cancer,mode):
"""
Optuna Optimization for Hyperparameters.
:param trial: Settings of the current trial of Hyperparameters
:param t_preprocess : Type of preprocessing ; dtype : String
:param feature_selection_type : Type of feature selection ; dtype : String
:param cancer : Name of cancer (folder) ; dtype : String
:return: Concordance Index ; dtype : Float
"""
#JUMPER1
direc_set = 'SUMO'
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load in data
preprocess_type = t_preprocess
if mode == 'prepared_data':
dir = os.path.expanduser('~/{}/Project/PreparedData/{}/{}/{}/'.format(direc_set,cancer,feature_selection_type,preprocess_type))
else:
dir = os.path.expanduser('~/{}/Project/PreparedData/'.format(direc_set))
trainset_0,trainset_1,trainset_2,trainset_3,trainset_4,valset_0,valset_1,valset_2,valset_3,valset_4,testset_0,testset_1,testset_2,testset_3,testset_4,trainset_feat_0, \
trainset_feat_1,trainset_feat_2,trainset_feat_3,trainset_feat_4,view_names= load_data(data_dir = dir)
# For tests, we just look at mRNA and DNA
view_names = ['MRNA','DNA']
# Feature offsets need to be the same in train/val/test for each fold, otherwise NN wouldn't work (diff dimension inputs)
feat_offs = [trainset_feat_0,trainset_feat_1,trainset_feat_2,trainset_feat_3,trainset_feat_4]
for c2,_ in enumerate(feat_offs):
feat_offs[c2] = list(feat_offs[c2].values)
for idx,_ in enumerate(feat_offs[c2]):
feat_offs[c2][idx] = feat_offs[c2][idx].item()
# Split data in feature values, duration, event
trainset = [trainset_0 ,trainset_1,trainset_2,trainset_3,trainset_4]
valset = [valset_0 ,valset_1,valset_2,valset_3,valset_4]
testset = [testset_0,testset_1,testset_2,testset_3,testset_4]
n_folds = len(trainset)
train_data_folds = []
train_duration_folds = []
train_event_folds = []
val_data_folds = []
val_duration_folds = []
val_event_folds = []
test_data_folds = []
# LOAD IN DATA
for c2,_ in enumerate(trainset):
train_data = []
for c,feat in enumerate(feat_offs[c2]):
if c < len(feat_offs[c2]) - 5: # train data views # CHANGED -3 to -5 TO NOT LOOK AT microRNA and RPPA
data_np = np.array((trainset[c2].iloc[:, feat_offs[c2][c] : feat_offs[c2][c+1]]).values).astype('float32')
data_tensor = torch.from_numpy(data_np).to(torch.float32)
data_tensor = data_tensor.to(device)
train_data.append(data_tensor)
elif c == len(feat_offs[c2]) - 3: # duration
duration_np = (np.array((trainset[c2].iloc[:, feat_offs[c2][c] : feat_offs[c2][c+1]]).values).astype('float32')).squeeze(axis=1)
duration_tensor = torch.from_numpy(duration_np).to(torch.float32)
duration_tensor = duration_tensor.to(device)
train_duration = duration_tensor
elif c == len(feat_offs[c2]) -2: # event
event_np = (np.array((trainset[c2].iloc[:, feat_offs[c2][c] : feat_offs[c2][c+1]]).values).astype('float32')).squeeze(axis=1)
event_tensor = torch.from_numpy(event_np).to(torch.float32)
event_tensor = event_tensor.to(device)
train_event = event_tensor
train_data = tuple(train_data)
val_data = []
for c,feat in enumerate(feat_offs[c2]):
if c < len(feat_offs[c2]) - 5: # train data views # CHANGED -3 to -5 TO NOT LOOK AT microRNA and RPPA
data_np = np.array((valset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')
data_tensor = torch.from_numpy(data_np).to(torch.float32)
data_tensor = data_tensor.to(device)
val_data.append(data_tensor)
elif c == len(feat_offs[c2]) - 3: # duration
duration_np = (np.array((valset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
duration_tensor = torch.from_numpy(duration_np).to(torch.float32)
duration_tensor = duration_tensor.to(device)
val_duration = duration_tensor
elif c == len(feat_offs[c2]) -2: # event
event_np = (np.array((valset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
event_tensor = torch.from_numpy(event_np).to(torch.float32)
event_tensor = event_tensor.to(device)
val_event = event_tensor
test_data = []
for c,feat in enumerate(feat_offs[c2]):
if c < len(feat_offs[c2]) - 5: # train data views # CHANGED -3 to -5 TO NOT LOOK AT microRNA and RPPA
data_np = np.array((testset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')
data_tensor = torch.from_numpy(data_np).to(torch.float32)
data_tensor = data_tensor.to(device)
test_data.append(data_tensor)
elif c == len(feat_offs[c2]) - 3: # duration
duration_np = (np.array((testset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
duration_tensor = torch.from_numpy(duration_np).to(torch.float32)
duration_tensor = duration_tensor.to(device)
test_duration = duration_tensor
elif c == len(feat_offs[c2]) -2: # event
event_np = (np.array((testset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
event_tensor = torch.from_numpy(event_np).to(torch.float32)
event_tensor = event_tensor.to(device)
test_event = event_tensor
train_data_folds.append(train_data)
val_data_folds.append(val_data)
train_duration_folds.append(train_duration)
val_duration_folds.append(val_duration)
train_event_folds.append(train_event)
val_event_folds.append(val_event)
test_data_folds.append(test_data)
# Rename so we have same structure as in train function
train_data = train_data_folds
train_duration = train_duration_folds
train_event = train_event_folds
val_data = val_data_folds
val_duration = val_duration_folds
val_event = val_event_folds
test_data = test_data_folds
#JUMPER1
# Current fold to be optimized
c_fold = n_fold
# Optimize each fold on its own
##################################### HYPERPARAMETER SEARCH SETTINGS ##############################################
l2_regularization_bool = trial.suggest_categorical('l2_regularization_bool', [True,False])
learning_rate = trial.suggest_float("learning_rate", 1e-5,1e-1,log=True)
l2_regularization_rate = trial.suggest_float("l2_regularization_rate", 1e-6,1e-3, log=True)
# batch_size = trial.suggest_int("batch_size", 5, 200)
batch_size = trial.suggest_categorical("batch_size", [7,17,33,64,128,256])
# n_epochs = trial.suggest_int("n_epochs", 10,100)
n_epochs = 100
dropout_prob = trial.suggest_float("dropout_prob", 0,0.5,step=0.1)
dropout_bool = trial.suggest_categorical('dropout_bool', [True,False])
batchnorm_bool = trial.suggest_categorical('batchnorm_bool',[True,False])
prelu_rate = trial.suggest_float('prelu_rate',0,1,step=0.05)
layers = []
activation_functions = []
dropouts = []
batchnorms = []
if 'MRNA' in view_names:
layers_1_mRNA = trial.suggest_categorical('layers_1_mRNA', [32,64,96])
layers_2_mRNA = trial.suggest_categorical('layers_2_mRNA', [8,16,32])
layers_1_mRNA_activfunc = trial.suggest_categorical('layers_1_mRNA_activfunc', ['relu','sigmoid','prelu'])
layers_2_mRNA_activfunc = trial.suggest_categorical('layers_2_mRNA_activfunc', ['relu','sigmoid','prelu'])
layers_1_mRNA_dropout = trial.suggest_categorical('layers_1_mRNA_dropout', ['yes','no'])
layers_2_mRNA_dropout = trial.suggest_categorical('layers_2_mRNA_dropout', ['yes','no'])
layers_1_mRNA_batchnorm = trial.suggest_categorical('layers_1_mRNA_batchnorm', ['yes', 'no'])
layers_2_mRNA_batchnorm = trial.suggest_categorical('layers_2_mRNA_batchnorm', ['yes', 'no'])
layers.append([layers_1_mRNA,layers_2_mRNA])
activation_functions.append([layers_1_mRNA_activfunc, layers_2_mRNA_activfunc])
dropouts.append([layers_1_mRNA_dropout, layers_2_mRNA_dropout])
batchnorms.append([layers_1_mRNA_batchnorm, layers_2_mRNA_batchnorm])
if 'DNA' in view_names:
layers_1_DNA = trial.suggest_categorical('layers_1_DNA', [32,64,96])
layers_2_DNA = trial.suggest_categorical('layers_2_DNA', [8,16,32])
layers_1_DNA_activfunc = trial.suggest_categorical('layers_1_DNA_activfunc', ['relu','sigmoid','prelu'])
layers_2_DNA_activfunc = trial.suggest_categorical('layers_2_DNA_activfunc', ['relu','sigmoid','prelu'])
layers_1_DNA_dropout = trial.suggest_categorical('layers_1_DNA_dropout', ['yes','no'])
layers_2_DNA_dropout = trial.suggest_categorical('layers_2_DNA_dropout', ['yes','no'])
layers_1_DNA_batchnorm = trial.suggest_categorical('layers_1_DNA_batchnorm', ['yes', 'no'])
layers_2_DNA_batchnorm = trial.suggest_categorical('layers_2_DNA_batchnorm', ['yes', 'no'])
layers.append([layers_1_DNA,layers_2_DNA])
activation_functions.append([layers_1_DNA_activfunc, layers_2_DNA_activfunc])
dropouts.append([layers_1_DNA_dropout, layers_2_DNA_dropout])
batchnorms.append([layers_1_DNA_batchnorm, layers_2_DNA_batchnorm])
if 'MICRORNA' in view_names:
layers_1_microRNA = trial.suggest_int('layers_1_microRNA', 32, 96)
layers_2_microRNA = trial.suggest_int('layers_2_microRNA', 8, 32)
layers_1_microRNA_activfunc = trial.suggest_categorical('layers_1_microRNA_activfunc', ['relu','sigmoid','prelu'])
layers_2_microRNA_activfunc = trial.suggest_categorical('layers_2_microRNA_activfunc', ['relu','sigmoid','prelu'])
layers_1_microRNA_dropout = trial.suggest_categorical('layers_1_microRNA_dropout', ['yes','no'])
layers_2_microRNA_dropout = trial.suggest_categorical('layers_2_microRNA_dropout', ['yes','no'])
layers_1_microRNA_batchnorm = trial.suggest_categorical('layers_1_microRNA_batchnorm', ['yes', 'no'])
layers_2_microRNA_batchnorm = trial.suggest_categorical('layers_2_microRNA_batchnorm', ['yes', 'no'])
layers.append([layers_1_microRNA,layers_2_microRNA])
activation_functions.append([layers_1_microRNA_activfunc, layers_2_microRNA_activfunc])
dropouts.append([layers_1_microRNA_dropout, layers_2_microRNA_dropout])
batchnorms.append([layers_1_microRNA_batchnorm, layers_2_microRNA_batchnorm])
if 'RPPA' in view_names:
layers_1_RPPA = trial.suggest_int('layers_1_RPPA', 32, 96)
layers_2_RPPA = trial.suggest_int('layers_2_RPPA', 8, 32)
layers_1_RPPA_activfunc = trial.suggest_categorical('layers_1_RPPA_activfunc', ['relu','sigmoid','prelu'])
layers_2_RPPA_activfunc = trial.suggest_categorical('layers_2_RPPA_activfunc', ['relu','sigmoid','prelu'])
layers_1_RPPA_dropout = trial.suggest_categorical('layers_1_RPPA_dropout', ['yes','no'])
layers_2_RPPA_dropout = trial.suggest_categorical('layers_2_RPPA_dropout', ['yes','no'])
layers_1_RPPA_batchnorm = trial.suggest_categorical('layers_1_RPPA_batchnorm', ['yes', 'no'])
layers_2_RPPA_batchnorm = trial.suggest_categorical('layers_2_RPPA_batchnorm', ['yes', 'no'])
layers.append([layers_1_RPPA,layers_2_RPPA])
activation_functions.append([layers_1_RPPA_activfunc, layers_2_RPPA_activfunc])
dropouts.append([layers_1_RPPA_dropout, layers_2_RPPA_dropout])
batchnorms.append([layers_1_RPPA_batchnorm, layers_2_RPPA_batchnorm])
# Last layer
layer_final_activfunc = trial.suggest_categorical('layers_final_activfunc', ['relu','sigmoid','prelu','none'])
layer_final_dropout = trial.suggest_categorical('layer_final_dropout', ['yes','no'])
layer_final_batchnorm = trial.suggest_categorical('layer_final_batchnorm', ['yes','no'])
activation_functions.append([layer_final_activfunc])
dropouts.append([layer_final_dropout])
batchnorms.append([layer_final_batchnorm])
dimensions_train = [x.shape[1] for x in train_data[c_fold]]
dimensions_val = [x.shape[1] for x in val_data[c_fold]]
dimensions_test = [x.shape[1] for x in test_data[c_fold]]
assert (dimensions_train == dimensions_val == dimensions_test), 'Feature mismatch between train/test'
dimensions = dimensions_train
# Transforms for PyCox
train_surv = (train_duration[c_fold], train_event[c_fold])
val_data_full = (val_data[c_fold], (val_duration[c_fold], val_event[c_fold]))
net = NN_changeable(views=view_names,
in_features=dimensions,
n_hidden_layers_dims=layers,
activ_funcs=activation_functions,
dropout_prob=dropout_prob,
dropout_layers=dropouts,
batch_norm=batchnorms,
dropout_bool=dropout_bool,
batch_norm_bool=batchnorm_bool,
print_bool=False,
prelu_init= prelu_rate
).to(device)
if l2_regularization_bool == True:
optimizer = Adam(net.parameters(), lr=learning_rate, weight_decay=l2_regularization_rate)
else:
optimizer = Adam(net.parameters(), lr=learning_rate)
callbacks = [tt.callbacks.EarlyStopping(patience=10)]
model = models.CoxPH(net,optimizer)
model.set_device(torch.device(device))
print_loss = False
# Fit model
log = model.fit(train_data[c_fold],
train_surv,
batch_size,
n_epochs,
callbacks = callbacks,
val_data=val_data_full,
val_batch_size= batch_size,
verbose=print_loss)
# Plot it
# _ = log.plot()
# Change for EvalSurv-Function
try:
test_duration = test_duration.cpu().detach().numpy()
test_event = test_event.cpu().detach().numpy()
except AttributeError:
pass
for c,fold in enumerate(train_data):
try:
train_duration[c_fold] = train_duration[c_fold].cpu().detach().numpy()
train_event[c_fold] = train_event[c_fold].cpu().detach().numpy()
val_duration[c_fold] = val_duration[c_fold].cpu().detach().numpy()
val_event[c_fold] = val_event[c_fold].cpu().detach().numpy()
except AttributeError: # in this case already numpy arrays
pass
# Since Cox semi parametric, we calculate a baseline hazard to introduce a time variable
_ = model.compute_baseline_hazards()
# Predict based on validation data
surv = model.predict_surv_df(val_data[c_fold])
# Plot it
# surv.iloc[:, :5].plot()
# plt.ylabel('S(t | x)')
# _ = plt.xlabel('Time')
# Evaluate with concordance, brier score and binomial log-likelihood
ev = EvalSurv(surv, val_duration[c_fold], val_event[c_fold], censor_surv='km') # censor_surv : Kaplan-Meier
# Concordance Index ; Used for Optimization
concordance_index = ev.concordance_td()
if concordance_index < 0.5:
concordance_index = 1 - concordance_index
# These two scores can also be used for Optimization if wanted
#Brier score
# time_grid = np.linspace(test_duration.min(), test_duration.max(), 100)
# _ = ev.brier_score(time_grid).plot
# brier_score = ev.integrated_brier_score(time_grid)
#Binomial log-likelihood
# binomial_score = ev.integrated_nbll(time_grid)
# SAVING MODEL POSSIBILITY
# dir = os.path.expanduser(r'~/SUMO/Project/Trial/Models/Fold_{}_Trial_{}'.format(c_fold,trial.number))
# torch.save(net,dir)
return concordance_index
def test_model(n_fold,t_preprocess,feature_selection_type,cancer):
"""Function to test the model on optimized hyperparameter settings.
:param n_fold : Number of the fold to test ; dtype : Int
:param t_preprocess : Type of preprocessing ; dtype : String
:param feature_selection_type : Feature selection type ; dtype : String
:param cancer : Name of the cancer folder ; dtype : String"""
#JUMPER1
direc_set = 'SUMO'
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Load in data
preprocess_type = t_preprocess
dir = os.path.expanduser('~/{}/Project/PreparedData/{}/{}/{}/'.format(direc_set,cancer,feature_selection_type,preprocess_type))
trainset_0,trainset_1,trainset_2,trainset_3,trainset_4,valset_0,valset_1,valset_2,valset_3,valset_4,testset_0,testset_1,testset_2,testset_3,testset_4,trainset_feat_0, \
trainset_feat_1,trainset_feat_2,trainset_feat_3,trainset_feat_4,view_names= load_data(data_dir = dir)
# For tests, we just look at mRNA and DNA
view_names = ['MRNA','DNA']
# Feature offsets need to be the same in train/val/test for each fold, otherwise NN wouldn't work (diff dimension inputs)
feat_offs = [trainset_feat_0,trainset_feat_1,trainset_feat_2,trainset_feat_3,trainset_feat_4]
for c2,_ in enumerate(feat_offs):
feat_offs[c2] = list(feat_offs[c2].values)
for idx,_ in enumerate(feat_offs[c2]):
feat_offs[c2][idx] = feat_offs[c2][idx].item()
# Split data in feature values, duration, event
trainset = [trainset_0 ,trainset_1,trainset_2,trainset_3,trainset_4]
valset = [valset_0 ,valset_1,valset_2,valset_3,valset_4]
testset = [testset_0,testset_1,testset_2,testset_3,testset_4]
n_folds = len(trainset)
train_data_folds = []
train_duration_folds = []
train_event_folds = []
val_data_folds = []
val_duration_folds = []
val_event_folds = []
test_data_folds = []
# LOAD IN DATA
for c2,_ in enumerate(trainset):
train_data = []
for c,feat in enumerate(feat_offs[c2]):
if c < len(feat_offs[c2]) - 5: # train data views # CHANGED -3 to -5 TO NOT LOOK AT microRNA and RPPA
data_np = np.array((trainset[c2].iloc[:, feat_offs[c2][c] : feat_offs[c2][c+1]]).values).astype('float32')
data_tensor = torch.from_numpy(data_np).to(torch.float32)
data_tensor = data_tensor.to(device)
train_data.append(data_tensor)
elif c == len(feat_offs[c2]) - 3: # duration
duration_np = (np.array((trainset[c2].iloc[:, feat_offs[c2][c] : feat_offs[c2][c+1]]).values).astype('float32')).squeeze(axis=1)
duration_tensor = torch.from_numpy(duration_np).to(torch.float32)
duration_tensor = duration_tensor.to(device)
train_duration = duration_tensor
elif c == len(feat_offs[c2]) -2: # event
event_np = (np.array((trainset[c2].iloc[:, feat_offs[c2][c] : feat_offs[c2][c+1]]).values).astype('float32')).squeeze(axis=1)
event_tensor = torch.from_numpy(event_np).to(torch.float32)
event_tensor = event_tensor.to(device)
train_event = event_tensor
train_data = tuple(train_data)
val_data = []
for c,feat in enumerate(feat_offs[c2]):
if c < len(feat_offs[c2]) - 5: # train data views # CHANGED -3 to -5 TO NOT LOOK AT microRNA and RPPA
data_np = np.array((valset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')
data_tensor = torch.from_numpy(data_np).to(torch.float32)
data_tensor = data_tensor.to(device)
val_data.append(data_tensor)
elif c == len(feat_offs[c2]) - 3: # duration
duration_np = (np.array((valset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
duration_tensor = torch.from_numpy(duration_np).to(torch.float32)
duration_tensor = duration_tensor.to(device)
val_duration = duration_tensor
elif c == len(feat_offs[c2]) -2: # event
event_np = (np.array((valset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
event_tensor = torch.from_numpy(event_np).to(torch.float32)
event_tensor = event_tensor.to(device)
val_event = event_tensor
test_data = []
for c,feat in enumerate(feat_offs[c2]):
if c < len(feat_offs[c2]) - 5: # train data views # CHANGED -3 to -5 TO NOT LOOK AT microRNA and RPPA
data_np = np.array((testset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')
data_tensor = torch.from_numpy(data_np).to(torch.float32)
data_tensor = data_tensor.to(device)
test_data.append(data_tensor)
elif c == len(feat_offs[c2]) - 3: # duration
duration_np = (np.array((testset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
duration_tensor = torch.from_numpy(duration_np).to(torch.float32)
duration_tensor = duration_tensor.to(device)
test_duration = duration_tensor
elif c == len(feat_offs[c2]) -2: # event
event_np = (np.array((testset[c2].iloc[:, feat_offs[c2][c]: feat_offs[c2][c + 1]]).values).astype('float32')).squeeze(axis=1)
event_tensor = torch.from_numpy(event_np).to(torch.float32)
event_tensor = event_tensor.to(device)
test_event = event_tensor
train_data_folds.append(train_data)
val_data_folds.append(val_data)
train_duration_folds.append(train_duration)
val_duration_folds.append(val_duration)
train_event_folds.append(train_event)
val_event_folds.append(val_event)
test_data_folds.append(test_data)
# Rename so we have same structure as in train function
train_data = train_data_folds
train_duration = train_duration_folds
train_event = train_event_folds
val_data = val_data_folds
val_duration = val_duration_folds
val_event = val_event_folds
test_data = test_data_folds
#JUMPER1
# Current fold to be optimized
c_fold = n_fold
# Optimize each fold on its own
dimensions_train = [x.shape[1] for x in train_data[c_fold]]
dimensions_val = [x.shape[1] for x in val_data[c_fold]]
dimensions_test = [x.shape[1] for x in test_data[c_fold]]
assert (dimensions_train == dimensions_val == dimensions_test), 'Feature mismatch between train/test'
dimensions = dimensions_train
# Transforms for PyCox
train_surv = (train_duration[c_fold], train_event[c_fold])
val_data_full = (val_data[c_fold], (val_duration[c_fold], val_event[c_fold]))
params={'l2_regularization_bool': True, 'learning_rate': 0.03179547997936081, 'l2_regularization_rate': 3.158678050616449e-05, 'batch_size': 64, 'dropout_prob': 0.1, 'dropout_bool': False, 'batchnorm_bool': True, 'prelu_rate': 0.75, 'layers_1_mRNA': 64, 'layers_2_mRNA': 32, 'layers_1_mRNA_activfunc': 'prelu', 'layers_2_mRNA_activfunc': 'sigmoid', 'layers_1_mRNA_dropout': 'no', 'layers_2_mRNA_dropout': 'no', 'layers_1_mRNA_batchnorm': 'no', 'layers_2_mRNA_batchnorm': 'no', 'layers_1_DNA': 32, 'layers_2_DNA': 16, 'layers_1_DNA_activfunc': 'sigmoid', 'layers_2_DNA_activfunc': 'sigmoid', 'layers_1_DNA_dropout': 'no', 'layers_2_DNA_dropout': 'no', 'layers_1_DNA_batchnorm': 'no', 'layers_2_DNA_batchnorm': 'yes', 'layers_final_activfunc': 'prelu', 'layer_final_dropout': 'yes', 'layer_final_batchnorm': 'yes'}
# LOAD MODEL IN DIRECTLY
# dir = os.path.expanduser(r'~/SUMO/Project/Trial/Models/Fold_{}_Trial_1'.format(c_fold))
# net = torch.load(dir).to(device)
net = NN_changeable(views=view_names,
in_features=dimensions,
n_hidden_layers_dims=[[params['layers_1_mRNA'], params['layers_2_mRNA']],
[params['layers_1_DNA'], params['layers_2_DNA']]],
activ_funcs=[[params['layers_1_mRNA_activfunc'], params['layers_2_mRNA_activfunc']],
[params['layers_1_DNA_activfunc'], params['layers_2_DNA_activfunc']],['none']],
dropout_prob=params['dropout_prob'],
dropout_layers=[[params['layers_1_mRNA_dropout'],params['layers_2_mRNA_dropout']],
[params['layers_1_DNA_dropout'],params['layers_2_DNA_dropout']]],
batch_norm=[[params['layers_1_mRNA_batchnorm'], params['layers_2_mRNA_batchnorm']],
[params['layers_1_DNA_batchnorm'], params['layers_2_DNA_batchnorm']]],
dropout_bool=params['dropout_bool'],
batch_norm_bool=params['batchnorm_bool'],
print_bool=False,
prelu_init= params['prelu_rate']
).to(device)
if params['l2_regularization_bool'] == True:
optimizer = Adam(net.parameters(), lr=params['learning_rate'] ,weight_decay=params['l2_regularization_rate'])
else:
optimizer = Adam(net.parameters(), lr=params['learning_rate'])
callbacks = [tt.callbacks.EarlyStopping(patience=10)]
model = models.CoxPH(net,optimizer)
model.set_device(torch.device(device))
print_loss = False
# Fit model
log = model.fit(train_data[c_fold],
train_surv,
params['batch_size'],
100,
callbacks = callbacks,
val_data=val_data_full,
val_batch_size= params['batch_size'],
verbose=print_loss)
# Plot it
# _ = log.plot()
# Change for EvalSurv-Function
try:
test_duration = test_duration.cpu().detach().numpy()
test_event = test_event.cpu().detach().numpy()
except AttributeError:
pass
for c,fold in enumerate(train_data):
try:
train_duration[c_fold] = train_duration[c_fold].cpu().detach().numpy()
train_event[c_fold] = train_event[c_fold].cpu().detach().numpy()
val_duration[c_fold] = val_duration[c_fold].cpu().detach().numpy()
val_event[c_fold] = val_event[c_fold].cpu().detach().numpy()
except AttributeError: # in this case already numpy arrays
pass
# Since Cox semi parametric, we calculate a baseline hazard to introduce a time variable
_ = model.compute_baseline_hazards()
# Predict based on validation data
surv = model.predict_surv_df(test_data[c_fold])
# Plot it
# surv.iloc[:, :5].plot()
# plt.ylabel('S(t | x)')
# _ = plt.xlabel('Time')
# Evaluate with concordance, brier score and binomial log-likelihood
ev = EvalSurv(surv, test_duration, test_event, censor_surv='km') # censor_surv : Kaplan-Meier
# Concordance Index ; Used for Optimization
concordance_index = ev.concordance_td()
if concordance_index < 0.5:
concordance_index = 1 - concordance_index
# These two scores can also be used for Optimization if wanted
#Brier score
# time_grid = np.linspace(test_duration.min(), test_duration.max(), 100)
# _ = ev.brier_score(time_grid).plot
# brier_score = ev.integrated_brier_score(time_grid)
#Binomial log-likelihood
# binomial_score = ev.integrated_nbll(time_grid)
print(concordance_index)
def optuna_optimization(n_fold,t_preprocess,feature_selection_type,cancer, mode):
"""
Optuna Optimization for Hyperparameters.
"""
# Set amount of different trials
EPOCHS = 2
func = lambda trial: objective(trial, n_fold, t_preprocess,feature_selection_type,cancer,mode)
study = optuna.create_study(directions=['maximize'],sampler=optuna.samplers.TPESampler(),pruner=optuna.pruners.MedianPruner())
study.optimize(func, n_trials = EPOCHS)
trial = study.best_trials
direc_set = 'SUMO'
dir = os.path.expanduser(r'~/{}/Project/Trial/FCNN_{}_{}_{}_BEST_{}_VAR2L.txt'.format(direc_set,cancer,t_preprocess,feature_selection_type,n_fold))
with open(dir, 'w') as fp:
for item in trial:
# write each item on a new line
fp.write("%s\n" % item)
# Show change of c-Index across folds
fig = optuna.visualization.plot_optimization_history(study)
dir = os.path.expanduser(r'~/{}/Project/Trial/FCNN_{}_{}_{}_BEST_{}_C-INDICES_VAR2L.png'.format(direc_set,cancer,t_preprocess,feature_selection_type,n_fold))
# fig.show()
fig.write_image(dir)
# fig.show(renderer='browser')
# Show hyperparameter importance
fig = optuna.visualization.plot_param_importances(study)
dir = os.path.expanduser(r'~/{}/Project/Trial/FCNN_{}_{}_{}_BEST_{}_HPARAMIMPORTANCE_VAR2L.png'.format(direc_set,cancer,t_preprocess,feature_selection_type,n_fold))
fig.write_image(dir)
#JUMPER1
# Save the best trial for each fold
# Save all trials in dataframe
# df = study.trials_dataframe()
# df = df.sort_values('value')
# df.to_csv("~/SUMO/Project/Trial/FCNN_KIRC3_Standardize_PCA.csv")
# print("Best Concordance Sum", trial.value)
# print("Best Hyperparameters : {}".format(trial.params))
def train(train_data,val_data,test_data,
train_duration,val_duration,test_duration,
train_event,val_event,test_event,
n_epochs,
batch_size,
l2_regularization,
l2_regularization_rate,
learning_rate,
prelu_rate,
layers,
activation_layers,
dropout,
dropout_rate,
dropout_layers,
batchnorm,
batchnorm_layers,
view_names):
"""
Training Function for the Fully Connected Neural Net, which connects the FCNN with the PH-Model.
:param train_data: Training Data for each fold for each view ; dtype : List of Lists [for each view] of Tensors(n_samples,n_features)
:param val_data: Validation Data for each fold for each view ; dtype : List of Lists [for each view] of Tensors(n_samples,n_features)
:param test_data: Test Data for each fold for each view ; dtype : List of Lists [for each view] of Tensors(n_samples,n_features)
:param train_duration: Training Duration for each fold ; dtype : List of Tensors(n_samples,)
:param val_duration: Validation Duration for each fold ; dtype : List of Tensors(n_samples,)
:param test_duration: Test Duration for each fold ; dtype : List of Tensors(n_samples,)
:param train_event: Training Event for each fold ; dtype : List of Tensors(n_samples,)
:param val_event: Validation Event for each fold ; dtype : List of Tensors(n_samples,)
:param test_event: Test Event for each fold ; dtype : List of Tensors(n_samples,)
:param n_epochs: Number of Epochs ; dtype : Int
:param batch_size: Batch Size ; dtype : Int
:param l2_regularization: Decide whether to apply L2 regularization ; dtype : Boolean
:param l2_regularization_rate: L2 regularization rate ; dtype : Float
:param learning_rate: Learning rate ; dtype : Float
:param prelu_rate: Initial Value for PreLU activation ; dtype : Float [between 0 and 1]
:param layers: Dimension of Layers for each view ; dtype : List of lists of Ints
:param activation_layers: Activation Functions (for each view) aswell as for the last layer, the last layer can
have no activation function ['none'] ; dtype : List of Lists of Strings ['relu', 'sigmoid', 'prelu']
:param dropout: Decide whether Dropout is to be applied or not ; dtype : Boolean
:param dropout_rate: Probability of Neuron Dropouts ; dtype : Int
:param dropout_layers: Layers in which to apply Dropout ; dtype : List of Lists of Strings ['yes','no']
:param batchnorm: Decide whether Batch Normalization is to be applied or not ; dtype : Boolean
:param batchnorm_layers: Layers in which to apply Batch Normalization ; dtype : List of Lists of Strings ['yes','no']
:param view_names: Names of used views ; dtype : List of Strings
"""
for c,fold in enumerate(train_data):
# Need tuple structure for PyCox
train_data[c] = tuple(train_data[c])
val_data[c] = tuple(val_data[c])
test_data[c] = tuple(test_data[c])
############################# FOLD X ###################################
for c_fold,fold in enumerate(train_data):
for c2,view in enumerate(fold):
# For GPU acceleration, we need to have everything as tensors for the training loop, but pycox EvalSurv
# Needs duration & event to be numpy arrays, thus at the start we set duration/event to tensors
# and before EvalSurv to numpy
try:
test_duration = torch.from_numpy(test_duration).to(torch.float32)
test_event = torch.from_numpy(test_event).to(torch.float32)
except TypeError:
pass
for c,fold in enumerate(train_data):
try:
train_duration[c] = torch.from_numpy(train_duration[c]).to(torch.float32)
train_event[c] = torch.from_numpy(train_event[c]).to(torch.float32)
val_duration[c] = torch.from_numpy(val_duration[c]).to(torch.float32)
val_event[c] = torch.from_numpy(val_event[c]).to(torch.float32)
except TypeError:
pass
print("Train data has shape : {} for view {}".format(train_data[c_fold][c2].shape, view_names[c2]))
print("Validation data has shape : {} for view {}".format(val_data[c_fold][c2].shape, view_names[c2]))
print("Test data has shape : {} for view {}".format(test_data[c_fold][c2].shape, view_names[c2]))