Create model.py
Browse files
model.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gc
|
| 3 |
+
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import seaborn as sns
|
| 8 |
+
|
| 9 |
+
from collections import Counter
|
| 10 |
+
from prettytable import PrettyTable
|
| 11 |
+
from IPython.display import Image
|
| 12 |
+
|
| 13 |
+
from sklearn.preprocessing import LabelEncoder
|
| 14 |
+
|
| 15 |
+
from keras.models import Model
|
| 16 |
+
from keras.regularizers import l2
|
| 17 |
+
from keras.constraints import max_norm
|
| 18 |
+
from keras.utils import to_categorical
|
| 19 |
+
from keras.preprocessing.text import Tokenizer
|
| 20 |
+
from keras.utils import pad_sequences
|
| 21 |
+
from keras.callbacks import EarlyStopping
|
| 22 |
+
from keras.layers import Input, Dense, Dropout, Flatten, Activation
|
| 23 |
+
from keras.layers import Conv1D, Add, MaxPooling1D, BatchNormalization
|
| 24 |
+
from keras.layers import Embedding, Bidirectional, LSTM, CuDNNLSTM, GlobalMaxPooling1D
|
| 25 |
+
|
| 26 |
+
import tensorflow as tf
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def residual_block(data, filters, d_rate):
|
| 30 |
+
"""
|
| 31 |
+
_data: input
|
| 32 |
+
_filters: convolution filters
|
| 33 |
+
_d_rate: dilation rate
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
shortcut = data
|
| 37 |
+
|
| 38 |
+
bn1 = BatchNormalization()(data)
|
| 39 |
+
act1 = Activation('relu')(bn1)
|
| 40 |
+
conv1 = Conv1D(filters, 1, dilation_rate=d_rate, padding='same', kernel_regularizer=l2(0.001))(act1)
|
| 41 |
+
|
| 42 |
+
#bottleneck convolution
|
| 43 |
+
bn2 = BatchNormalization()(conv1)
|
| 44 |
+
act2 = Activation('relu')(bn2)
|
| 45 |
+
conv2 = Conv1D(filters, 3, padding='same', kernel_regularizer=l2(0.001))(act2)
|
| 46 |
+
|
| 47 |
+
#skip connection
|
| 48 |
+
x = Add()([conv2, shortcut])
|
| 49 |
+
|
| 50 |
+
return x
|
| 51 |
+
|
| 52 |
+
def get_model():
|
| 53 |
+
# model
|
| 54 |
+
x_input = Input(shape=(100, 21))
|
| 55 |
+
|
| 56 |
+
#initial conv
|
| 57 |
+
conv = Conv1D(128, 1, padding='same')(x_input)
|
| 58 |
+
|
| 59 |
+
# per-residue representation
|
| 60 |
+
res1 = residual_block(conv, 128, 2)
|
| 61 |
+
res2 = residual_block(res1, 128, 3)
|
| 62 |
+
|
| 63 |
+
x = MaxPooling1D(3)(res2)
|
| 64 |
+
x = Dropout(0.5)(x)
|
| 65 |
+
|
| 66 |
+
# softmax classifier
|
| 67 |
+
x = Flatten()(x)
|
| 68 |
+
x_output = Dense(1000, activation='softmax', kernel_regularizer=l2(0.0001))(x)
|
| 69 |
+
|
| 70 |
+
model2 = Model(inputs=x_input, outputs=x_output)
|
| 71 |
+
model2.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 72 |
+
|
| 73 |
+
return model2
|