-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
161 lines (135 loc) · 7.47 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from keras.models import Sequential, load_model
from keras.layers import Dense, Activation, LSTM, Bidirectional, TimeDistributed, Masking, GRU
from keras.callbacks import EarlyStopping, ModelCheckpoint
from util import calculate_accuracy
def blstm(x_train, x_val, x_test, y_train, y_val, y_test, out_dir,
name='blstm_model', hidden_units=10, layers=1, max_epochs=1000, batch_size=32, patience=20,
dropout=0.0, recurrent_dropout=0.0):
"""
Bidirectional LSTM model for protein secondary structure prediction.
"""
num_samples = x_train.shape[0]
max_seq_len = x_train.shape[1]
num_features = x_train.shape[2]
num_classes = y_train.shape[2]
# Build Keras model
model = Sequential()
model.add(Masking(mask_value=0, input_shape=(max_seq_len, num_features)))
model.add(Bidirectional(LSTM(hidden_units, return_sequences=True, input_shape=(max_seq_len, num_features),
dropout=dropout, recurrent_dropout=recurrent_dropout)))
if layers > 1:
for _ in range(layers-1):
model.add(Bidirectional(LSTM(hidden_units, return_sequences=True,
dropout=dropout, recurrent_dropout=recurrent_dropout)))
model.add(TimeDistributed(Dense(num_classes)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
# Train model. Use early-stopping on validation data to determine when to stop training.
model_path = os.path.join(out_dir, name + '.h5')
checkpointer = ModelCheckpoint(model_path, save_best_only=True)
model.fit(x_train, y_train, epochs=max_epochs, batch_size=batch_size, verbose=2,
validation_data=(x_val, y_val), callbacks=[EarlyStopping(patience=patience), checkpointer])
model = load_model(model_path) # Best model is not necessarily current model instance b/c patience != 0
y_train_pred = model.predict(x_train)
y_test_pred = model.predict(x_test)
print('Train accuracy: {:.2f}%'.format(calculate_accuracy(y_train, y_train_pred) * 100.0))
print('Test accuracy: {:.2f}%'.format(calculate_accuracy(y_test, y_test_pred) * 100.0))
return model
def bgru(x_train, x_val, x_test, y_train, y_val, y_test, out_dir,
name='bgru_model', hidden_units=10, layers=1, max_epochs=1000, batch_size=32, patience=20,
dropout=0.0, recurrent_dropout=0.0):
"""
Bidirectional GRU model for protein secondary structure prediction.
"""
num_samples = x_train.shape[0]
max_seq_len = x_train.shape[1]
num_features = x_train.shape[2]
num_classes = y_train.shape[2]
# Build Keras model
model = Sequential()
model.add(Masking(mask_value=0, input_shape=(max_seq_len, num_features)))
model.add(Bidirectional(GRU(hidden_units, return_sequences=True, input_shape=(max_seq_len, num_features),
dropout=dropout, recurrent_dropout=recurrent_dropout)))
if layers > 1:
for _ in range(layers-1):
model.add(Bidirectional(GRU(hidden_units, return_sequences=True,
dropout=dropout, recurrent_dropout=recurrent_dropout)))
model.add(TimeDistributed(Dense(num_classes)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
# Train model. Use early-stopping on validation data to determine when to stop training.
model_path = os.path.join(out_dir, name + '.h5')
checkpointer = ModelCheckpoint(model_path, save_best_only=True)
model.fit(x_train, y_train, epochs=max_epochs, batch_size=batch_size, verbose=1,
validation_data=(x_val, y_val), callbacks=[EarlyStopping(patience=patience), checkpointer])
model = load_model(model_path) # Best model is not necessarily current model instance b/c patience != 0
y_train_pred = model.predict(x_train)
print('Train accuracy: {:.2f}%'.format(calculate_accuracy(y_train, y_train_pred) * 100.0))
# Test set accuracy
y_test_pred = []
for i in range(3):
y_test_pred.append(model.predict(x_test[i]))
print('Test accuracy: {:.2f}%'.format(calculate_accuracy(y_test[i], y_test_pred[i]) * 100.0))
return model
def breslstm(x_train, x_val, x_test, y_train, y_val, y_test, out_dir,
name='breslstm_model', hidden_units=10, layers=1, max_epochs=1000, batch_size=32, patience=20,
dropout=0.0, recurrent_dropout=0.0):
"""
Bidirectional Residual LSTM model for protein secondary structure prediction.
"""
num_samples = x_train.shape[0]
max_seq_len = x_train.shape[1]
num_features = x_train.shape[2]
num_classes = y_train.shape[2]
# Build Keras model
inputs = Input(shape=(max_seq_len, num_features))
x = Masking(mask_value=0, input_shape=(max_seq_len, num_features))(inputs)
x = Bidirectional(LSTM(hidden_units, return_sequences=True, input_shape=(max_seq_len, num_features),
dropout=dropout, recurrent_dropout=recurrent_dropout))(x)
if layers > 1:
for _ in range(layers-1):
x_rnn = Bidirectional(LSTM(hidden_units, return_sequences=True,
dropout=dropout, recurrent_dropout=recurrent_dropout))(x)
x = add([x, x_rnn])
x = TimeDistributed(Dense(num_classes))(x)
outputs = Activation('softmax')(x)
model = Model(inputs=inputs, outputs=outputs)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model.summary())
# Train model. Use early-stopping on validation data to determine when to stop training.
model_path = os.path.join(out_dir, name + '.h5')
checkpointer = ModelCheckpoint(model_path, save_best_only=True)
model.fit(x_train, y_train, epochs=max_epochs, batch_size=batch_size, verbose=1,
validation_data=(x_val, y_val), callbacks=[EarlyStopping(patience=patience), checkpointer])
model = load_model(model_path) # Best model is not necessarily current model instance b/c patience != 0
y_train_pred = model.predict(x_train)
print('Train accuracy: {:.2f}%'.format(calculate_accuracy(y_train, y_train_pred) * 100.0))
# Test set accuracy
y_test_pred = []
for i in range(3):
y_test_pred.append(model.predict(x_test[i]))
print('Test accuracy: {:.2f}%'.format(calculate_accuracy(y_test[i], y_test_pred[i]) * 100.0))
return model
def load_and_train(model_path, x_train, x_val, x_test, y_train, y_val, y_test,
max_epochs=1000, batch_size=32, patience=20):
"""
Load model and resume training.
"""
model = load_model(model_path)
checkpointer = ModelCheckpoint(model_path, save_best_only=True)
model.fit(x_train, y_train, epochs=max_epochs, batch_size=batch_size, verbose=1,
validation_data=(x_val, y_val), callbacks=[EarlyStopping(patience=patience), checkpointer])
model = load_model(model_path)
y_train_pred = model.predict(x_train)
print('Train accuracy: {:.2f}%'.format(calculate_accuracy(y_train, y_train_pred) * 100.0))
# print('Test accuracy: {:.2f}%'.format(calculate_accuracy(y_test, y_test_pred) * 100.0))
y_test_pred = []
for i in range(3):
y_test_pred.append(model.predict(x_test[i]))
print('Test accuracy: {:.2f}%'.format(calculate_accuracy(y_test[i], y_test_pred[i]) * 100.0))
return model