-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrt.py
1133 lines (816 loc) · 41.8 KB
/
rt.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
# coding=utf-8
# Copyright 2023 Thang V Pham
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import sys
import argparse
import re
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorboard.plugins.hparams import api as hp
DATA_DIR = os.environ['HOME'] + '/data'
#region ms_data
def min_max_scale(x, min, max):
new_x = 1.0 * (x - min) / (max - min)
return new_x
def min_max_scale_rev(x, min, max):
old_x = x * (max - min) + min
return old_x
class data_phospho():
@classmethod
def load_training_transformer(cls):
x_train = np.load(DATA_DIR + '/HumanPhosphoproteomeDB_rt_x_train.npy')
y_train = np.load(DATA_DIR + '/HumanPhosphoproteomeDB_rt_y_train.npy')
x_val = np.load(DATA_DIR + '/HumanPhosphoproteomeDB_rt_x_val.npy')
y_val = np.load(DATA_DIR + '/HumanPhosphoproteomeDB_rt_y_val.npy')
return (x_train, y_train), (x_val, y_val)
@classmethod
def load_testing_transformer(cls):
x_test = np.load(DATA_DIR + '/HumanPhosphoproteomeDB_rt_x_test.npy')
y_test = np.load(DATA_DIR + '/HumanPhosphoproteomeDB_rt_y_test.npy')
return (x_test, y_test)
class data_autort():
@classmethod
def load_training_transformer(cls, max_sequence_length = 48):
x_train = np.load(DATA_DIR + '/PXD006109_rt_x_train.npy')
y_train = np.load(DATA_DIR + '/PXD006109_rt_y_train.npy')
x_val = np.load(DATA_DIR + '/PXD006109_rt_x_val.npy')
y_val = np.load(DATA_DIR + '/PXD006109_rt_y_val.npy')
return (x_train, y_train), (x_val, y_val)
@classmethod
def load_testing_transformer(cls, max_sequence_length = 48):
x_test = np.load(DATA_DIR + '/PXD006109_rt_x_test.npy')
y_test = np.load(DATA_DIR + '/PXD006109_rt_y_test.npy')
return (x_test, y_test)
class data_deepdia():
@classmethod
def load_training_transformer(cls, max_sequence_length = 50):
x_train = np.load(DATA_DIR + '/PXD005573_rt_x_train.npy')
y_train = np.load(DATA_DIR + '/PXD005573_rt_y_train.npy')
x_val = np.load(DATA_DIR + '/PXD005573_rt_x_val.npy')
y_val = np.load(DATA_DIR + '/PXD005573_rt_y_val.npy')
return (x_train, y_train), (x_val, y_val)
@classmethod
def load_testing_transformer(cls, max_sequence_length = 50):
x_test = np.load(DATA_DIR + '/PXD005573_rt_x_test.npy')
y_test = np.load(DATA_DIR + '/PXD005573_rt_y_test.npy')
return (x_test, y_test)
class data_prosit():
@classmethod
def load_training(cls, max_sequence_length = 30):
x_train = np.load(DATA_DIR + '/prosit_rt_updated/X_train.npy')
y_train = np.load(DATA_DIR + '/prosit_rt_updated/Y_train.npy')
x_val = np.load(DATA_DIR + '/prosit_rt_updated/X_validation.npy')
y_val = np.load(DATA_DIR + '/prosit_rt_updated/Y_validation.npy')
return (x_train, y_train), (x_val, y_val)
@classmethod
def load_testing(cls, max_sequence_length = 30):
x_test = np.load(DATA_DIR + '/prosit_rt_updated/X_holdout.npy')
y_test = np.load(DATA_DIR + '/prosit_rt_updated/Y_holdout.npy')
return (x_test, y_test)
class data_generics():
@classmethod
def sequence_to_integer(cls, sequences, max_sequence_length):
Prosit_ALPHABET = {
'A': 1,
'C': 2,
'D': 3,
'E': 4,
'F': 5,
'G': 6,
'H': 7,
'I': 8,
'K': 9,
'L': 10,
'M': 11,
'N': 12,
'P': 13,
'Q': 14,
'R': 15,
'S': 16,
'T': 17,
'V': 18,
'W': 19,
'Y': 20,
'o': 21,
}
array = np.zeros([len(sequences), max_sequence_length], dtype=int)
for i, sequence in enumerate(sequences):
for j, s in enumerate(re.sub('M\(ox\)', 'o', sequence)):
array[i, j] = Prosit_ALPHABET[s]
return array
@classmethod
def load_deepdia(cls, filename, seq_header = 'sequence', rt_header = 'rt'):
min_sequence_length = 7
max_sequence_length = 50
d = pd.read_csv(filename)
if seq_header not in d:
print('No column in the data: ' + seq_header)
exit(0)
has_rt = rt_header in d
print(d.shape[0], ' peptides')
selected_peptides = {}
for index, row in d.iterrows():
s = row[seq_header][1:-1] # remove _xxx_
if s.find('(') < 0 and (min_sequence_length <= len(s) <= max_sequence_length):
if has_rt :
selected_peptides[s] = row['rt']
else :
selected_peptides[s] = 0
print(len(selected_peptides), ' peptides selected')
df = pd.DataFrame.from_dict(selected_peptides, orient = 'index', columns = ['rt'])
x = cls.sequence_to_integer(df.index.values, max_sequence_length)
return (x, df['rt'].to_numpy())
@classmethod
def load_prosit(cls, filename, seq_header = 'sequence', rt_header = 'rt'):
min_sequence_length = 7
max_sequence_length = 30
d = pd.read_csv(filename)
if seq_header not in d:
print('No column in the data: ' + seq_header)
exit(0)
has_rt = rt_header in d
print(d.shape[0], ' peptides')
selected_peptides = {}
for index, row in d.iterrows():
s = row[seq_header][1:-1] # remove _xxx_
s = re.sub('M\(ox\)', 'o', s)
if s.find('(') < 0 and (min_sequence_length <= len(s) <= max_sequence_length):
if has_rt :
selected_peptides[s] = row['rt']
else :
selected_peptides[s] = 0
print(len(selected_peptides), ' peptides selected')
df = pd.DataFrame.from_dict(selected_peptides, orient = 'index', columns = ['rt'])
x = cls.sequence_to_integer(df.index.values, max_sequence_length)
return (x, df['rt'].to_numpy())
@classmethod
def load_autort(cls, filename, seq_header = 'sequence', rt_header = 'rt'):
min_sequence_length = 7
max_sequence_length = 48
d = pd.read_csv(filename)
if seq_header not in d:
print('No column in the data: ' + seq_header)
exit(0)
has_rt = rt_header in d
print(d.shape[0], ' peptides')
selected_peptides = {}
for index, row in d.iterrows():
s = row[seq_header][1:-1] # remove _xxx_
s = re.sub('M\(ox\)', 'o', s)
if s.find('(') < 0 and (min_sequence_length <= len(s) <= max_sequence_length):
if has_rt :
selected_peptides[s] = row[rt_header]
else :
selected_peptides[s] = 0
print(len(selected_peptides), ' peptides selected')
df = pd.DataFrame.from_dict(selected_peptides, orient = 'index', columns = ['rt'])
x = cls.sequence_to_integer(df.index.values, max_sequence_length)
return (x, df['rt'].to_numpy())
@classmethod
def integer_to_sequence(cls, X):
int2seq = '_ACDEFGHIKLMNPQRSTVWYo'
int2seqf = lambda x : ''.join([int2seq[c] for c in x if c > 0])
return ([int2seqf(x) for x in X])
@classmethod
def sequence_to_integer_phospho(cls, sequences, max_sequence_length = 60):
deepphospho_ALPHABET = {
"A": 1,
"C": 2,
"D": 3,
"E": 4,
"F": 5,
"G": 6,
"H": 7,
"I": 8,
"K": 9,
"L": 10,
"M": 11,
"N": 12,
"P": 13,
"Q": 14,
"R": 15,
"S": 16,
"T": 17,
"V": 18,
"W": 19,
"Y": 20,
"1": 21,
"2": 22,
"3": 23,
"4": 24,
"*": 25
}
array = np.zeros([len(sequences), max_sequence_length], dtype=int)
for i, sequence in enumerate(sequences):
for j, s in enumerate(re.sub('@', '', sequence)):
array[i, j] = deepphospho_ALPHABET[s]
return array
@classmethod
def load_phospho(cls, filename, seq_header = 'IntPep', rt_header = 'iRT'):
min_sequence_length = 6
max_sequence_length = 60
d = pd.read_csv(filename)
if seq_header not in d:
print('No column in the data: ' + seq_header)
exit(0)
has_rt = rt_header in d
print(d.shape[0], ' peptides')
selected_peptides = {}
for index, row in d.iterrows():
s = row[seq_header]
s = re.sub('@', '', s)
if s.find('(') < 0 and (min_sequence_length <= len(s) <= max_sequence_length):
if has_rt :
selected_peptides[s] = row[rt_header]
else :
selected_peptides[s] = 0
else:
print(s)
print(len(selected_peptides), ' peptides selected')
df = pd.DataFrame.from_dict(selected_peptides, orient = 'index', columns = ['rt'])
x = cls.sequence_to_integer_phospho(df.index.values, max_sequence_length)
return (x, df['rt'].to_numpy())
@classmethod
def integer_to_sequence_phospho(cls, X):
int2seq = '_ACDEFGHIKLMNPQRSTVWY1234*'
int2seqf = lambda x : ''.join([int2seq[c] for c in x if c > 0])
return ([int2seqf(x) for x in X])
#endregion
#region Transformer
# Code in the Transformer region is based on https://www.tensorflow.org/text/tutorials/transformer
def get_angles(pos, i, d_model):
angle_rates = 1 / np.power(10000, (2 * (i // 2)) / np.float32(d_model))
return pos * angle_rates
# Position
def positional_encoding(position, d_model):
angle_rads = get_angles(
np.arange(position)[:, np.newaxis], np.arange(d_model)[np.newaxis, :], d_model
)
# apply sin to even indices in the array; 2i
angle_rads[:, 0::2] = np.sin(angle_rads[:, 0::2])
# apply cos to odd indices in the array; 2i+1
angle_rads[:, 1::2] = np.cos(angle_rads[:, 1::2])
pos_encoding = angle_rads[np.newaxis, ...]
return tf.cast(pos_encoding, dtype=tf.float32)
# Masking
def create_padding_mask(seq):
seq = tf.cast(tf.math.equal(seq, 0), tf.float32)
# add extra dimensions to add the padding
# to the attention logits.
return seq[:, tf.newaxis, tf.newaxis, :] # (batch_size, 1, 1, seq_len)
# Scaled dot product attention
def scaled_dot_product_attention(q, k, v, mask):
'''
Calculate the attention weights.
q, k, v must have matching leading dimensions.
k, v must have matching penultimate dimension, i.e.: seq_len_k = seq_len_v.
The mask has different shapes depending on its type(padding or look ahead)
but it must be broadcastable for addition.
Args:
q: query shape == (..., seq_len_q, depth)
k: key shape == (..., seq_len_k, depth)
v: value shape == (..., seq_len_v, depth_v)
mask: Float tensor with shape broadcastable
to (..., seq_len_q, seq_len_k). Defaults to None.
Returns:
output, attention_weights
'''
matmul_qk = tf.matmul(q, k, transpose_b=True) # (..., seq_len_q, seq_len_k)
# scale matmul_qk
dk = tf.cast(tf.shape(k)[-1], tf.float32)
scaled_attention_logits = matmul_qk / tf.math.sqrt(dk)
# add the mask to the scaled tensor.
if mask is not None:
scaled_attention_logits += mask * -1e9
# softmax is normalized on the last axis (seq_len_k) so that the scores
# add up to 1.
attention_weights = tf.nn.softmax(
scaled_attention_logits, axis=-1
) # (..., seq_len_q, seq_len_k)
output = tf.matmul(attention_weights, v) # (..., seq_len_q, depth_v)
return output, attention_weights
# Multi-head attention
class multi_head_attention(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads):
super(multi_head_attention, self).__init__()
self.num_heads = num_heads
self.d_model = d_model
assert d_model % self.num_heads == 0
self.depth = d_model // self.num_heads
self.wq = tf.keras.layers.Dense(d_model)
self.wk = tf.keras.layers.Dense(d_model)
self.wv = tf.keras.layers.Dense(d_model)
self.dense = tf.keras.layers.Dense(d_model)
def split_heads(self, x, batch_size):
'''
Split the last dimension into (num_heads, depth).
Transpose the result such that the shape is (batch_size, num_heads, seq_len, depth)
'''
x = tf.reshape(x, (batch_size, -1, self.num_heads, self.depth))
return tf.transpose(x, perm=[0, 2, 1, 3])
def call(self, v, k, q, mask):
batch_size = tf.shape(q)[0]
q = self.wq(q) # (batch_size, seq_len, d_model)
k = self.wk(k) # (batch_size, seq_len, d_model)
v = self.wv(v) # (batch_size, seq_len, d_model)
q = self.split_heads(q, batch_size) # (batch_size, num_heads, seq_len_q, depth)
k = self.split_heads(k, batch_size) # (batch_size, num_heads, seq_len_k, depth)
v = self.split_heads(v, batch_size) # (batch_size, num_heads, seq_len_v, depth)
# scaled_attention.shape == (batch_size, num_heads, seq_len_q, depth)
# attention_weights.shape == (batch_size, num_heads, seq_len_q, seq_len_k)
scaled_attention, attention_weights = scaled_dot_product_attention(
q, k, v, mask
)
scaled_attention = tf.transpose(
scaled_attention, perm=[0, 2, 1, 3]
) # (batch_size, seq_len_q, num_heads, depth)
concat_attention = tf.reshape(
scaled_attention, (batch_size, -1, self.d_model)
) # (batch_size, seq_len_q, d_model)
# BERT dense layer is here. see class TFBertSelfOutput(tf.keras.layers.Layer):
output = self.dense(concat_attention) # (batch_size, seq_len_q, d_model)
return output, attention_weights
# Point wise feed forward network
def point_wise_feed_forward_network(d_model, dff):
return tf.keras.Sequential(
[
tf.keras.layers.Dense(dff, activation='gelu'), # (batch_size, seq_len, dff)
tf.keras.layers.Dense(d_model), # (batch_size, seq_len, d_model)
]
)
# Encoder and decoder
class encoder_layer(tf.keras.layers.Layer):
def __init__(self, d_model, num_heads, dff, rate=0.1):
super(encoder_layer, self).__init__()
self.mha = multi_head_attention(d_model, num_heads)
self.ffn = point_wise_feed_forward_network(d_model, dff)
self.layernorm1 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.layernorm2 = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.dropout1 = tf.keras.layers.Dropout(rate)
self.dropout2 = tf.keras.layers.Dropout(rate)
def call(self, x, training, mask):
out1 = self.layernorm1(x) # (batch_size, input_seq_len, d_model)
attn_output, _ = self.mha(out1, out1, out1, mask) # (batch_size, input_seq_len, d_model)
attn_output = self.dropout1(attn_output, training = training)
out1 = x + attn_output # (batch_size, input_seq_len, d_model)
out2 = self.layernorm2(out1)
ffn_output = self.ffn(out2) # (batch_size, input_seq_len, d_model)
ffn_output = self.dropout2(ffn_output, training=training)
out2 = out1 + ffn_output
return out2
# Encoder
class encoder_block(tf.keras.layers.Layer):
def __init__(
self,
num_layers,
d_model,
num_heads,
dff,
input_vocab_size,
maximum_position_encoding,
rate = 0.1,
):
super(encoder_block, self).__init__(name = 'transformer')
self.d_model = d_model
self.num_layers = num_layers
self.num_heads = num_heads
self.dff = dff
self.input_vocab_size = (input_vocab_size,)
self.maximum_position_encoding = maximum_position_encoding
self.rate = rate
self.embedding = tf.keras.layers.Embedding(input_vocab_size, d_model)
#self.pos_encoding = positional_encoding(maximum_position_encoding, d_model)
self.pos_embedding = tf.keras.layers.Embedding(maximum_position_encoding, d_model)
self.layernorm = tf.keras.layers.LayerNormalization(epsilon=1e-6)
self.enc_layers = [
encoder_layer(d_model, num_heads, dff, rate) for _ in range(num_layers)
]
self.dropout = tf.keras.layers.Dropout(rate)
def call(self, x, training):
seq_len = tf.shape(x)[1]
enc_padding_mask = create_padding_mask(x)
# adding embedding and position encoding.
x = self.embedding(x) # (batch_size, input_seq_len, d_model)
x *= tf.math.sqrt(tf.cast(self.d_model, tf.float32))
#x += self.pos_encoding[:, :seq_len, :]
indices = tf.range(self.maximum_position_encoding)
x += self.pos_embedding(indices)
x = self.dropout(x, training=training)
for i in range(self.num_layers):
x = self.enc_layers[i](x, training, enc_padding_mask)
x = self.layernorm(x)
return x # (batch_size, input_seq_len, d_model)
def get_config(self):
config = super().get_config().copy()
config.update(
{
'num_layers': self.num_layers,
'd_model': self.d_model,
'num_head': self.num_heads,
'dff': self.dff,
'input_vocab_size': self.input_vocab_size,
'maximum_position_encoding': self.maximum_position_encoding,
'rate': self.rate,
}
)
return config
class custom_schedule(tf.keras.optimizers.schedules.LearningRateSchedule):
def __init__(self, d_model, warmup_steps = 4000):
super(custom_schedule, self).__init__()
self.d_model = d_model
self.d_model = tf.cast(self.d_model, tf.float32)
self.warmup_steps = tf.cast(warmup_steps, tf.float32)
def __call__(self, step):
arg1 = tf.math.rsqrt(tf.cast(step, dtype = tf.float32))
arg2 = tf.cast(step, dtype = tf.float32) * (self.warmup_steps ** -1.5)
return tf.math.rsqrt(self.d_model) * tf.math.minimum(arg1, arg2)
#endregion
#region Model training
def build_model(num_layers, d_model, num_heads, d_ff, dropout_rate, vocab_size, max_len):
coded_input = tf.keras.layers.Input(shape=(max_len,), name = 'input')
encoder = encoder_block(num_layers, d_model, num_heads, d_ff, vocab_size, max_len, dropout_rate)
enc_output = encoder(coded_input) # (batch_size, inp_seq_len, d_model)
net = enc_output[:, 0, :]
net = tf.keras.layers.Dense(512, activation = 'gelu', name = 'predict_1')(net)
net = tf.keras.layers.Dropout(dropout_rate, name = 'predict_dropout_1')(net)
net = tf.keras.layers.Dense(512, activation = 'gelu', name = 'predict_2')(net)
net = tf.keras.layers.Dropout(dropout_rate, name = 'predict_dropout_2')(net)
#net = tf.keras.layers.Dense(1, activation = 'linear', name = 'output')(net)
net = tf.keras.layers.Dense(1, activation = 'sigmoid', name = 'output')(net)
m = tf.keras.Model(coded_input, net, name = 'rt_transformer')
m.layers[2]._name = 'CLS_token'
return m
#endregion
def main():
if len(sys.argv) == 1:
print('python rt.py [tune, train, predict]')
sys.exit(0)
else:
mode = sys.argv[1]
if mode == 'train':
'''
Example:
python ./rt.py train -data prosit -logs logs-train-prosit -epochs 5000 -n_layers 8 -n_heads 8 -dropout 0.1 -batch_size 128 -d_model 256 -d_ff 1024
'''
parser = argparse.ArgumentParser()
parser.add_argument('-data', '--data', default = 'prosit', type = str, help = 'Data for training, default prosit.')
parser.add_argument('-batch_size', '--batch_size', default = 1024, type = int, help = 'Batch size for training, default 1024.')
parser.add_argument('-d_model', '--d_model', default = 512, type = int, help = 'd_model, default 512.')
parser.add_argument('-n_layers', '--n_layers', default = 10, type = int, help = 'n_layers, default 10.')
parser.add_argument('-n_heads', '--n_heads', default = 8, type = int, help = 'n_heads, default 8.')
parser.add_argument('-d_ff', '--d_ff', default = 512, type = int, help = 'd_ff, default 512.')
parser.add_argument('-dropout', '--dropout', default = 0.1, type = float, help = 'dropout, default 0.1.')
parser.add_argument('-epochs', '--epochs', default = 2000, type = int, help = 'Number of epochs, default 2000.')
parser.add_argument('--gpu', default=True, action=argparse.BooleanOptionalAction)
parser.add_argument('-logs', '--logs', default = 'logs', type = str, help = 'Directory for logging')
args = parser.parse_args(sys.argv[2:len(sys.argv)])
print('data =', args.data)
print('batch_size =', args.batch_size)
print('d_model =', args.d_model)
print('d_ff =', args.d_ff)
print('n_layers =', args.n_layers)
print('n_heads =', args.n_heads)
print('dropout =', args.dropout)
print('epochs =', args.epochs)
if not args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = ''
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
if args.data == 'autort':
(x_train, y_train), (x_val, y_val) = data_autort.load_training_transformer()
min_val = 0.0
max_val = 101.33
elif args.data == 'prosit':
(x_train, y_train), (x_val, y_val) = data_prosit.load_training()
min_val = min(y_train.min(), y_val.min()) - 0.01
max_val = max(y_train.max(), y_val.max()) + 0.01
print("(min, max) = ", (min_val, max_val))
y_train = min_max_scale(y_train, min = min_val, max = max_val)
y_val = min_max_scale(y_val, min = min_val, max = max_val)
elif args.data == 'deepdia':
(x_train, y_train), (x_val, y_val) = data_deepdia.load_training_transformer()
min_val = min(y_train.min(), y_val.min()) - 0.01
max_val = max(y_train.max(), y_val.max()) + 0.01
print("(min, max) = ", (min_val, max_val))
y_train = min_max_scale(y_train, min = min_val, max = max_val)
y_val = min_max_scale(y_val, min = min_val, max = max_val)
elif args.data == 'phospho':
(x_train, y_train), (x_val, y_val) = data_phospho.load_training_transformer()
min_val = 0.0
max_val = 1.0
else:
print('Unknown data')
exit(0)
CLS = x_train.max() + 1
x_train = np.concatenate((np.full((x_train.shape[0], 1), CLS), x_train), axis = 1)
x_val = np.concatenate((np.full((x_val.shape[0], 1), CLS), x_val), axis = 1)
print(len(x_train), 'Training sequences')
print(len(x_val), 'Validation sequences')
print('CLS =', CLS)
cp_path = './' + args.data + '-b' + str(args.batch_size) + '-dm' + str(args.d_model) + '-df' + str(args.d_ff) + '-nl' + str(args.n_layers) + '-nh' + str(args.n_heads) + '-dr' + str(args.dropout) + '-ep' + str(args.epochs) + '/'
print(cp_path)
from pathlib import Path
Path(cp_path).mkdir(parents=True, exist_ok=True)
pd.DataFrame({'parameter': ['data', 'batch_size', 'd_model', 'd_ff', 'n_layers', 'n_heads', 'dropout',
'epochs', 'vocab_size', 'max_length', 'min_val', 'max_val'],
'value': [args.data, str(args.batch_size), str(args.d_model), str(args.d_ff), str(args.n_layers), str(args.n_heads), str(args.dropout),
str(args.epochs), str(CLS + 1), str(x_train.shape[1]), str(min_val), str(max_val)]}).to_csv(cp_path + 'parameters.txt', sep= '\t', index = False)
checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath = cp_path + 'weights.{epoch:04d}.hdf5',
save_best_only = True,
#save_best_only = False,
save_weights_only = True,
verbose = 1)
csv_logger = tf.keras.callbacks.CSVLogger(cp_path + 'model_history_log.csv', append = False)
tf.random.set_seed(1)
np.random.seed(1)
model = build_model(
num_layers = args.n_layers,
d_model = args.d_model,
num_heads = args.n_heads,
d_ff = args.d_ff,
dropout_rate = args.dropout,
vocab_size = CLS + 1, # number of aminoacids
max_len = x_train.shape[1] # maximal peptide length
)
np.random.seed(1)
tf.random.set_seed(1)
learning_rate = custom_schedule(args.d_model)
optimizer = tf.keras.optimizers.Adam(learning_rate, beta_1 = 0.9, beta_2 = 0.98, epsilon = 1e-9)
model.compile(optimizer = optimizer, loss = tf.keras.losses.MeanAbsoluteError())
model.summary()
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=args.logs, histogram_freq=1)
earlystopper = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience = 5000, verbose=1)
cb = [checkpoint, csv_logger, tensorboard_callback, earlystopper]
training_history = model.fit(
x_train,
y_train,
batch_size = args.batch_size,
epochs = args.epochs,
validation_data = (x_val, y_val),
callbacks = cb)
print('Min test loss: ', np.min(training_history.history['loss']))
elif mode == 'tune':
# Tuning using Tensorboard https://www.tensorflow.org/tensorboard/hyperparameter_tuning_with_hparams
parser = argparse.ArgumentParser()
parser.add_argument('-data', '--data', default = 'prosit', type = str, help = 'Data for training, default prosit.')
parser.add_argument('-batch_size', '--batch_size', nargs = '+', default = 1024, type = int, help = 'Batch size for training, default 1024.')
parser.add_argument('-d_model', '--d_model', nargs = '+', default = 512, type = int, help = 'd_model, default 512.')
parser.add_argument('-n_layers', '--n_layers', nargs = '+', default = 10, type = int, help = 'n_layers, default 10.')
parser.add_argument('-n_heads', '--n_heads', nargs = '+', default = 8, type = int, help = 'n_heads, default 8.')
parser.add_argument('-d_ff', '--d_ff', nargs = '+', default = 512, type = int, help = 'd_ff, default 512.')
parser.add_argument('-dropout', '--dropout', nargs = '+', default = 0.1, type = float, help = 'dropout, default 0.1.')
parser.add_argument('-epochs', '--epochs', default = 200, type = int, help = 'Number of epochs, default 200.')
parser.add_argument('--gpu', default=True, action=argparse.BooleanOptionalAction)
parser.add_argument('-logs', '--logs', default = 'logs-random-search', type = str, help = 'Directory for logging')
parser.add_argument('-seed', '--seed', default = 0, type = int, help = 'Random seed')
parser.add_argument('-n_random_samples', '--n_random_samples', default = 200, type = int, help = 'Number of random samples')
parser.add_argument('-begin', '--begin', default = 0, type = int, help = 'begin')
parser.add_argument('-end', '--end', default = 200, type = int, help = 'end')
args = parser.parse_args(sys.argv[2:len(sys.argv)])
print('data =', args.data)
print('batch_size =', args.batch_size)
print('d_model =', args.d_model)
print('d_ff =', args.d_ff)
print('n_layers =', args.n_layers)
print('n_heads =', args.n_heads)
print('dropout =', args.dropout)
print('epochs =', args.epochs)
prefix = args.logs + '-' + str(args.begin) + '-' + str(args.end)
if not args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = ''
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
if args.data == 'autort':
(x_train, y_train), (x_val, y_val) = data_autort.load_training_transformer()
min_val = 0.0
max_val = 101.33
elif args.data == 'prosit':
(x_train, y_train), (x_val, y_val) = data_prosit.load_training()
min_val = min(y_train.min(), y_val.min()) - 0.01
max_val = max(y_train.max(), y_val.max()) + 0.01
y_train = min_max_scale(y_train, min = min_val, max = max_val)
y_val = min_max_scale(y_val, min = min_val, max = max_val)
elif args.data == 'deepdia':
(x_train, y_train), (x_val, y_val) = data_deepdia.load_training_transformer()
min_val = min(y_train.min(), y_val.min()) - 0.01
max_val = max(y_train.max(), y_val.max()) + 0.01
y_train = min_max_scale(y_train, min = min_val, max = max_val)
y_val = min_max_scale(y_val, min = min_val, max = max_val)
else:
print('Unknown data')
exit(0)
CLS = x_train.max() + 1
x_train = np.concatenate((np.full((x_train.shape[0], 1), CLS), x_train), axis = 1)
x_val = np.concatenate((np.full((x_val.shape[0], 1), CLS), x_val), axis = 1)
print(len(x_train), 'Training sequences')
print(len(x_val), 'Validation sequences')
print('CLS =', CLS)
tf.random.set_seed(1)
np.random.seed(1)
# hyperparameters
HP_N_LAYERS = hp.HParam('n_layers', hp.Discrete(args.n_layers))
HP_N_HEADS = hp.HParam('n_heads', hp.Discrete(args.n_heads))
HP_DROPOUT = hp.HParam('dropout', hp.Discrete(args.dropout))
HP_BATCHSIZE = hp.HParam('batch_size', hp.Discrete(args.batch_size))
HP_D_MODEL = hp.HParam('d_model', hp.Discrete(args.d_model))
HP_D_FF = hp.HParam('d_ff', hp.Discrete(args.d_ff))
METRIC_ACCURACY = 'val_loss'
with tf.summary.create_file_writer(prefix + '/hparam_tuning').as_default():
hp.hparams_config(
hparams = [HP_N_LAYERS, HP_DROPOUT, HP_N_HEADS, HP_BATCHSIZE, HP_D_MODEL, HP_D_FF],
metrics = [hp.Metric(METRIC_ACCURACY, display_name = 'loss')],
)
def train_test_model(hparams):
np.random.seed(1)
tf.random.set_seed(1)
model = build_model(
num_layers = hparams[HP_N_LAYERS],
d_model = hparams[HP_D_MODEL],
num_heads = hparams[HP_N_HEADS],
d_ff = hparams[HP_D_FF],
dropout_rate = hparams[HP_DROPOUT],
vocab_size = CLS + 1, # number of amino acids, including CLS
max_len = x_train.shape[1] # maximal peptide length
)
learning_rate = custom_schedule(hparams[HP_D_MODEL])
optimizer = tf.keras.optimizers.Adam(learning_rate, beta_1 = 0.9, beta_2 = 0.98, epsilon = 1e-9)
model.compile(optimizer = optimizer, loss = tf.keras.losses.MeanAbsoluteError())
earlystopper = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience = 20, verbose=1)
csv_name = 'model-history-' + args.data + '-b' + str(hparams[HP_BATCHSIZE]) + '-dm' + str(hparams[HP_D_MODEL]) + \
'-df' + str(hparams[HP_D_FF]) + '-nl' + str(hparams[HP_N_LAYERS]) + \
'-nh' + str(hparams[HP_N_HEADS]) + '-dr' + str(hparams[HP_DROPOUT]) + '-ep' + str(args.epochs) + '.csv'
csv_logger = tf.keras.callbacks.CSVLogger(csv_name, append = False)
training_history = model.fit(
x_train,
y_train,
batch_size = hparams[HP_BATCHSIZE],
epochs = args.epochs,
validation_data = (x_val, y_val),
callbacks = [earlystopper, csv_logger],
verbose = 1)
a = pd.read_csv(csv_name)
return np.min(a['val_loss'])
def run(run_dir, hparams):
with tf.summary.create_file_writer(run_dir).as_default():
hp.hparams(hparams) # record the values used in this trial
loss = train_test_model(hparams)
tf.summary.scalar(METRIC_ACCURACY, loss, step=1)
return loss
# create a list of parameters
import random
random.seed(args.seed)
parameter_set = []
completed = set()
for session_num in range(args.n_random_samples):
while True :
n_layers = random.sample(HP_N_LAYERS.domain.values, 1)[0]
n_heads = random.sample(HP_N_HEADS.domain.values, 1)[0]
dropout_rate = random.sample(HP_DROPOUT.domain.values, 1)[0]
batch_size = random.sample(HP_BATCHSIZE.domain.values, 1)[0]
d_model = random.sample(HP_D_MODEL.domain.values, 1)[0]
d_ff = random.sample(HP_D_FF.domain.values, 1)[0]
signature = ''.join([str(n_layers), str(n_heads), str(dropout_rate), str(batch_size), str(d_model), str(d_ff)])
if signature not in completed:
completed.add(signature)
break
hparams = {
HP_N_LAYERS: n_layers,
HP_N_HEADS: n_heads,
HP_DROPOUT: dropout_rate,
HP_BATCHSIZE: batch_size,
HP_D_MODEL: d_model,
HP_D_FF: d_ff,
}
print({h.name: hparams[h] for h in hparams})
parameter_set.append(hparams)
with open(prefix + '.txt', 'w') as myfile:
myfile.write('N\td_model\td_ff\th\tr\tbatch_size\tloss\n')
for session_num in range(args.begin, args.end):
hparams = parameter_set[session_num]
run_name = 'run-%d' % session_num
print('--- Starting trial: %s' % run_name)
print({h.name: hparams[h] for h in hparams})
loss = run(prefix + '/hparam_tuning/' + run_name, hparams)
with open(prefix + '.txt', 'a') as myfile:
myfile.write(str(hparams[HP_N_LAYERS]) + '\t' +
str(hparams[HP_D_MODEL]) + '\t' +
str(hparams[HP_D_FF]) + '\t' +
str(hparams[HP_N_HEADS]) + '\t' +
str(hparams[HP_DROPOUT]) + '\t' +
str(hparams[HP_BATCHSIZE]) + '\t' +
str(loss) + '\n')
elif mode == 'predict':
if len(sys.argv) < 3:
print('python rt.py predict model_directory ...')
sys.exit(0)
model_path = sys.argv[2]