-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_train.py
185 lines (127 loc) · 5.47 KB
/
sim_train.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
"""
Usage: simulation_train <network_json> [<n_trials>]
Options:
<n_trials> Number of runs of the net [default: 50].
"""
import docopt
import random
import numpy as np
import re
import os
#os.environ["CUDA_VISIBLE_DEVICES"] = '2'
import keras
from keras.models import Sequential
from keras.layers import Conv1D, GlobalMaxPool1D, Dense, Dropout
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.initializers import Constant
import keras.backend as K
from keras.engine.topology import Layer
import keras.backend as K
from core import MotifMirrorGradientBleeding, CustomSumPool, CustomMeanPool, MCRCDropout
import gzip as gz
from data_utils import one_hot, train_test_val_split, reverse_complement
from keras.layers import BatchNormalization
from keras.layers import Lambda
def augment_data(sequences, responses):
# we use this method as we want to augment just the training data
s_new, resp_new = [], []
for seq, resp in zip(sequences, responses):
s_new.append(seq)
s_new.append(seq[::-1,::-1])# this assumes the correct encoding
resp_new.append(resp)
resp_new.append(resp)
return np.array(s_new), np.array(resp_new)
def load_data(aug):
sequences = []
responses = []
with gz.open("additive_training_dat.gz") as training_dat:
for line in training_dat:
line = line.decode("ascii")
seq, resp = line.strip().split(" ")
sequences.append(one_hot(seq))
responses.append(int(resp))
if aug:
sequences.append(one_hot(reverse_complement(seq)))
responses.append(int(resp))
X_train, X_val, X_test, Y_train, Y_val, Y_test = train_test_val_split(np.array(sequences), np.array(responses))
return X_train, X_val, X_test, Y_train, Y_val, Y_test
import types
def predict_mc(self, X_pred, n_preds=100):
return np.mean([self.predict(X_pred) for i in range(n_preds)], axis=0)
def generate_model(nn_params):
K.clear_session()
tf_classifier = Sequential()
tf_classifier.add(Conv1D(input_shape=(1000, 4),
filters=nn_params["input_filters"],
kernel_size = (nn_params["filter_length"]),
padding = "valid",
activation = nn_params["activation"],
kernel_regularizer=l2(nn_params["reg"])))
if nn_params["apply_rc"]:
divisor = 2
tf_classifier.add(CustomMeanPool())
else:
divisor = 1
if nn_params["batch_norm"]:
tf_classifier.add(BatchNormalization())
# batch norm never actually used with dropout or Equivariance
if nn_params["use_dropout"]:
if nn_params["mc_dropout"]:
tf_classifier.add(Lambda(lambda x: K.dropout(x, level=0.1)))
else: #don't need mcrc dropout as it appears after sum pool
tf_classifier.add(Dropout(0.1))
tf_classifier.add(GlobalMaxPool1D())
if nn_params["custom_init"]:
tf_classifier.add(Dense(2,activation="softmax", kernel_initializer=Constant(np.array([[1]*(nn_params["input_filters"]//divisor), [1]*(nn_params["input_filters"]//divisor) ])), bias_initializer=Constant(np.array([1, -1]))))
else:
tf_classifier.add(Dense(2,activation="softmax"))
epochs = 50
lrate = 0.01
decay = lrate/epochs
adam = Adam(lr=lrate, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=decay)
tf_classifier.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
tf_classifier.predict_mc = types.MethodType(predict_mc, tf_classifier)
return tf_classifier
def train(nn_params, n_trials):
batch_size=32
from keras.callbacks import EarlyStopping
import tflearn
results = []
X_train, X_val, X_test, Y_train, Y_val, Y_test = load_data(False)
if nn_params["augment_data"]:
X_train, Y_train = augment_data(X_train, Y_train)
### removing training data optionally to test how this impacts results
if "data_frac" in nn_params:
n_data = int(float(nn_params["data_frac"]) * len(X_train))
X_train = X_train[:n_data]
Y_train = Y_train[:n_data]
for trial in range(n_trials):
tf_classifier = generate_model(nn_params)
es = EarlyStopping(patience=4, monitor='val_acc')
mrc = MotifMirrorGradientBleeding(0,assign_bias=True)
if nn_params["apply_rc"]:
callbacks = [es,mrc]
else:
callbacks=[es]
tf_classifier.fit(X_train, tflearn.data_utils.to_categorical(Y_train,2), validation_data=(X_val, tflearn.data_utils.to_categorical(Y_val,2)),
epochs=50, batch_size=batch_size, callbacks=callbacks,verbose=True)
if nn_params["mc_dropout"] and nn_params["use_dropout"]:
predictions = tf_classifier.predict_mc(X_test)
else:
predictions = tf_classifier.predict_mc(X_test, n_preds=1)
results.append((Y_test.tolist(), predictions.tolist()))
with open(nn_params["output_prefix"]+".json","w") as outfile:
json.dump(results, outfile)
if __name__ == "__main__":
args = docopt.docopt(__doc__)
import json
nn_args = json.load(open(args["<network_json>"]))
if args["<n_trials>"] is None:
args["<n_trials>"] = 50
if "batch_norm" not in nn_args:
nn_args["batch_norm"] = 0
if "augment_data" not in nn_args:
nn_args["augment_data"] = 0
print (nn_args)
train(nn_args, int(args["<n_trials>"]))