-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
498 lines (330 loc) · 18.5 KB
/
models.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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 10 21:25:55 2023
@author: timshen2
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
import numpy as np
from sklearn.model_selection import KFold
import random
from keras import optimizers
from keras.layers import Conv1D, MaxPool1D, Flatten,Input
from keras.models import Sequential
from keras.layers import LSTM, Embedding, Dense,Bidirectional,concatenate,GlobalMaxPooling1D,multiply
from keras import optimizers,Model
from keras.layers import Add
from keras.layers import SimpleRNN
# CNN_AA
vocabulary = 100
embedding_dim = 32
word_num = 24
state_dim = 32
# Define input shape for the model
inputs = Input(shape=(word_num,))
# Add an embedding layer with vocabulary size, embedding dimension, and input length
embedding = Embedding(vocabulary, embedding_dim, input_length=word_num)(inputs)
# Add a 1D convolutional layer with 256 filters, kernel size 5, 'same' padding, and ReLU activation
conv1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(embedding)
# Add a max pooling layer with pool size 5, stride 1, and 'same' padding
pool1 = MaxPool1D(pool_size=5, strides=1, padding="same")(conv1)
# Add another 1D convolutional layer with 256 filters, kernel size 5, 'same' padding, and ReLU activation
conv2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(pool1)
# Add another max pooling layer with pool size 5, stride 1, and 'same' padding
pool2 = MaxPool1D(pool_size=5, strides=1, padding="same")(conv2)
# Add another 1D convolutional layer with 256 filters, kernel size 5, 'same' padding, and ReLU activation
conv3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(pool2)
# Add another max pooling layer with pool size 5, stride 1, and 'same' padding
pool3 = MaxPool1D(pool_size=5, strides=1, padding="same")(conv3)
# Add another 1D convolutional layer with 256 filters, kernel size 5, 'same' padding, and ReLU activation
conv4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(pool3)
# Add another max pooling layer with pool size 5, stride 1, and 'same' padding
pool4 = MaxPool1D(pool_size=5, strides=1, padding="same")(conv4)
# Flatten the output of the last convolutional layer
flatten = Flatten()(pool4)
# Add a dense layer with sigmoid activation to output a binary classification
outputs = Dense(1, activation='sigmoid')(flatten)
# Create the model
model_cnn = Model(inputs=inputs, outputs=outputs)
#--------------------------------------------------------------------------------------------
# cnn_lstm_res
input_layer = Input(shape=(24,))
embed = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer)
# First convolutional block
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(embed)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
# Second convolutional block with residual connection from the first block
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn2_res = Add()([cnn1, cnn2])
# Third convolutional block with residual connection from the second block
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2_res)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn3_res = Add()([cnn2_res, cnn3])
# Fourth convolutional block with residual connection from the third block
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3_res)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn4)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn4_res = Add()([cnn3_res, cnn4])
cnn = Flatten()(cnn4_res)
cnn = Dense(16, activation='sigmoid')(cnn)
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(embed)
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(lstm)
lstm = Bidirectional(LSTM(state_dim, return_sequences=False, dropout=0.2))(lstm)
lstm = Dense(16, activation='sigmoid')(lstm)
merge = concatenate([cnn, lstm], axis=1)
preds = Dense(1, activation='sigmoid')(merge)
cnn_lstm_res = Model(input_layer, preds)
#--------------------------------------------------------------------------------------------
# cnn_lstm_res
# Define the input layer for text data
input_layer_1 = Input(shape=(24,))
# Define the embedding layer for text data
embed_1 = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer_1)
# First convolutional block
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(embed_1)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
# Second convolutional block with residual connection from the first block
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn2_res = Add()([cnn1, cnn2])
# Third convolutional block with residual connection from the second block
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2_res)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn3_res = Add()([cnn2_res, cnn3])
# Fourth convolutional block with residual connection from the third block
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3_res)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn4)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn4_res = Add()([cnn3_res, cnn4])
cnn = Flatten()(cnn4_res)
cnn = Dense(16, activation='sigmoid')(cnn)
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(embed_1)
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(lstm)
lstm = Bidirectional(LSTM(state_dim, return_sequences=False, dropout=0.2))(lstm)
lstm = Dense(16, activation='sigmoid')(lstm)
# Define the input layer for gene feature
input_layer_2 = Input(shape=(1,))
# Define the embedding layer for gene feature
embed_2 = Embedding(100, 20, input_length=1)(input_layer_2)
# Flatten the output of the embedding layer for gene feature
flatten = Flatten()(embed_2)
# Concatenate the output of the CNN, LSTM and the flattened gene feature
merge = concatenate([cnn, lstm, flatten], axis=1)
preds = Dense(1, activation='sigmoid')(merge)
cnn_lstm_res_gene = Model([input_layer_1, input_layer_2], preds)
#--------------------------------------------------------------------------------------------
# cnn_lstm
input_layer = Input(shape=(24,))
embed = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer)
# CNN layers
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(embed)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn4)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn = Flatten()(cnn4)
cnn = Dense(16, activation='sigmoid')(cnn)
# LSTM layers
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(embed)
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(lstm)
lstm = Bidirectional(LSTM(state_dim, return_sequences=False, dropout=0.2))(lstm)
lstm = Dense(16, activation='sigmoid')(lstm)
# Concatenate CNN and LSTM outputs
merge = concatenate([cnn, lstm], axis=1)
# Output layer
preds = Dense(1, activation='sigmoid')(merge)
# Create the model
cnn_lstm = Model(input_layer, preds)
#--------------------------------------------------------------------------------------------
# cnn_lstm
# Define the input layer for text data
input_layer_1 = Input(shape=(24,))
# Define the embedding layer for text data
embed_1 = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer_1)
# CNN layers
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(embed_1)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
cnn1 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn1 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn1)
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn1)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn2 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2)
cnn2 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn2)
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn2)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn3 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3)
cnn3 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn3)
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn3)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn4 = Conv1D(filters=256, kernel_size=5, padding='same', activation='relu')(cnn4)
cnn4 = MaxPool1D(pool_size=5, strides=1, padding="same")(cnn4)
cnn = Flatten()(cnn4)
cnn = Dense(16, activation='sigmoid')(cnn)
# LSTM layers
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(embed_1)
lstm = Bidirectional(LSTM(state_dim, return_sequences=True, dropout=0.2))(lstm)
lstm = Bidirectional(LSTM(state_dim, return_sequences=False, dropout=0.2))(lstm)
lstm = Dense(16, activation='sigmoid')(lstm)
# Define the input layer for gene feature
input_layer_2 = Input(shape=(1,))
# Define the embedding layer for gene feature
embed_2 = Embedding(100, 20, input_length=1)(input_layer_2)
# Flatten the output of the embedding layer for gene feature
flatten = Flatten()(embed_2)
# Concatenate the output of the CNN, LSTM and the flattened gene feature
merge = concatenate([cnn, lstm, flatten], axis=1)
preds = Dense(1, activation='sigmoid')(merge)
cnn_lstm_gene = Model([input_layer_1, input_layer_2], preds)
#--------------------------------------------------------------------------------------------
# cnn_AA_Gene/GeneFam
# Define the input layer for text data
input_layer_1 = Input(shape=(24,))
# Add an embedding layer for text data
embed_1 = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer_1)
# Add four 1D convolutional layers with max pooling
cnn = Conv1D(filters=256,kernel_size=5,padding = 'same', activation = 'relu')(embed_1)
cnn = MaxPool1D(pool_size=5,strides=1, padding="same")(cnn)
cnn = Conv1D(filters=256,kernel_size=5,padding = 'same', activation = 'relu')(cnn)
cnn = MaxPool1D(pool_size=5,strides=1, padding="same")(cnn)
cnn = Conv1D(filters=256,kernel_size=5,padding = 'same', activation = 'relu')(cnn)
cnn = MaxPool1D(pool_size=5,strides=1, padding="same")(cnn)
cnn = Conv1D(filters=256,kernel_size=5,padding = 'same', activation = 'relu')(cnn)
cnn = MaxPool1D(pool_size=5,strides=1, padding="same")(cnn)
# Flatten the output of the last convolutional layer
cnn = Flatten()(cnn)
# Add a dense layer with sigmoid activation for binary classification
cnn = Dense(1,activation='sigmoid')(cnn)
# Define the input layer for categorical data
input_layer_2 = Input(shape=(1,))
# Add an embedding layer for categorical data
embed_2 = Embedding(100, 20, input_length=1)(input_layer_2)
# Flatten the output of the embedding layer
flatten = Flatten()(embed_2)
# Concatenate the output of the last convolutional layer and the flattened categorical data
merge = concatenate([cnn,flatten],axis = 1)
# Add a dense layer with sigmoid activation for binary classification
preds = Dense(1,activation='sigmoid')(merge)
# Define the model with two inputs and one output
cnn_gene = Model([input_layer_1,input_layer_2],preds)
#--------------------------------------------------------------------------------------------
# logist
# Create a sequential model
model_logist = Sequential()
# Add an embedding layer with specified vocabulary, embedding dimension, and input length
model_logist.add(Embedding(vocabulary, embedding_dim, input_length=word_num,))
# Flatten the output of the embedding layer
model_logist.add(Flatten())
# Add a dense layer with 1 neuron and relu activation function
model_logist.add(Dense(1, activation='relu'))
#--------------------------------------------------------------------------------------------
# logist
# Logistic Regression
# Define the input layer for text data
input_layer_1 = Input(shape=(word_num,))
# Define the embedding layer for text data
embed_1 = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer_1)
# Flatten the output of the embedding layer for text data
flatten_1 = Flatten()(embed_1)
# Define the input layer for gene feature
input_layer_2 = Input(shape=(1,))
# Define the embedding layer for gene feature
embed_2 = Embedding(100, 20, input_length=1)(input_layer_2)
# Flatten the output of the embedding layer for gene feature
flatten_2 = Flatten()(embed_2)
# Concatenate the flattened text data and the flattened gene feature
merge = concatenate([flatten_1,flatten_2],axis = 1)
# Add a dense output layer with relu activation
output_layer = Dense(1, activation='relu')(merge)
# Define the model with two inputs and one output
logist_gene = Model(inputs=[input_layer_1,input_layer_2], outputs=output_layer)
#--------------------------------------------------------------------------------------------
# SimpleRNN
# Define the input layer
input_layer = Input(shape=(word_num,))
# Add the embedding layer
embed = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer)
# Add a SimpleRNN layer, with dropout and return sequences set to False
rnn = SimpleRNN(state_dim, return_sequences=False, dropout=0.2)(embed)
# Add a dense output layer with relu activation
output_layer = Dense(1, activation='relu')(rnn)
# Create the model with input and output layers
model_SimpleRNN = Model(inputs=input_layer, outputs=output_layer)
#--------------------------------------------------------------------------------------------
# SimpleRNN
# Define the input layer for text data
input_layer_1 = Input(shape=(word_num,))
# Add the embedding layer for text data
embed_1 = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer_1)
# Add a SimpleRNN layer, with dropout and return sequences set to False
rnn = SimpleRNN(state_dim, return_sequences=False, dropout=0.2)(embed_1)
# Define the input layer for gene feature
input_layer_2 = Input(shape=(1,))
# Add an embedding layer for gene feature
embed_2 = Embedding(100, 20, input_length=1)(input_layer_2)
# Flatten the output of the embedding layer
flatten = Flatten()(embed_2)
# Concatenate the output of the last SimpleRNN layer and the flattened gene feature
merge = concatenate([rnn,flatten],axis = 1)
# Add a dense output layer with relu activation
output_layer = Dense(1, activation='relu')(merge)
# Create the model with two inputs and one output
SimpleRNN_gene = Model(inputs=[input_layer_1,input_layer_2], outputs=output_layer)
#--------------------------------------------------------------------------------------------
# Define input layer
input_layer = Input(shape=(word_num,))
# Define embedding layer
embed = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer)
# Define LSTM layer
lstm = LSTM(state_dim, return_sequences=False, dropout=0.2)(embed)
# Define output layer
output_layer = Dense(1, activation='relu')(lstm)
# Define the model
model_lstm = Model(inputs=input_layer, outputs=output_layer)
#--------------------------------------------------------------------------------------------
# LSTM_gene
# Define the input layer for text data
input_layer_1 = Input(shape=(word_num,))
# Define the embedding layer for text data
embed_1 = Embedding(vocabulary, embedding_dim, input_length=word_num)(input_layer_1)
# Define LSTM layer for text data
lstm = LSTM(state_dim, return_sequences=False, dropout=0.2)(embed_1)
# Define the input layer for gene feature
input_layer_2 = Input(shape=(1,))
# Define the embedding layer for gene feature
embed_2 = Embedding(100, 20, input_length=1)(input_layer_2)
# Flatten the output of the embedding layer
flatten = Flatten()(embed_2)
# Concatenate the output of the LSTM layer and the flattened gene feature
merge = concatenate([lstm, flatten], axis=1)
# Define the output layer with relu activation
output_layer = Dense(1, activation='relu')(merge)
# Define the model with two inputs and one output
lstm_gene = Model(inputs=[input_layer_1, input_layer_2], outputs=output_layer)