-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvencoder.py
executable file
·229 lines (194 loc) · 6.95 KB
/
convencoder.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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import pandas as pd
import numpy as np
import click as ck
from aaindex import MAXLEN
import sys
import math
from utils import DataGenerator
from scipy import sparse
from keras.layers import (
Conv1D, MaxPooling1D, UpSampling1D, Reshape, Input,
Dense, Embedding, Masking, LSTM, RepeatVector)
from keras.models import load_model, Model
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras import backend as K
from keras.optimizers import SGD
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
K.set_session(sess)
MAXLEN = 1600
@ck.command()
@ck.option(
'--batch-size',
help='Batch size for training and testing', default=128)
@ck.option(
'--epochs',
help='Number of epochs for training', default=12)
@ck.option(
'--model-file',
help='Batch size for training and testing', default='model.h5')
@ck.option(
'--is-train', help="Training mode", is_flag=True, default=False)
@ck.option(
'--data-file', help="Pandas pickled data file", default='data/data.pkl')
def main(batch_size, epochs, model_file, is_train, data_file):
train_data, test_data = load_data(data_file)
if is_train:
train(train_data, batch_size, epochs, model_file)
test(test_data, batch_size, model_file)
def load_data(data_file, split=0.8):
df = pd.read_pickle(data_file)
# df = df[df['indexes'].map(lambda x: len(x)) <= MAXLEN]
n = len(df)
print('Data size: {}'.format(n))
index = np.arange(n)
np.random.seed(seed=0)
np.random.shuffle(index)
train_n = int(n * split)
train_df = df.iloc[index[:train_n]]
test_df = df.iloc[index[train_n:]]
def reshape(values):
values = np.hstack(values).reshape(
len(values), len(values[0]))
return values
def get_values(df):
n = len(df)
rows = []
cols = []
data = []
for i, row in enumerate(df.itertuples()):
for ind, j in enumerate(row.indexes):
rows.append(i)
cols.append(ind)
data.append(j / 20.0)
data = sparse.csr_matrix((data, (rows, cols)), shape=(n, MAXLEN))
index = np.arange(n)
np.random.seed(seed=0)
np.random.shuffle(index)
print(data[0, :].toarray())
return data[index, :]
def get_values_one_hot(df):
n = len(df)
rows = []
cols = []
data = []
for i, row in enumerate(df.itertuples()):
for ind, j in enumerate(row.indexes):
rows.append(i)
cols.append(ind * 21 + j)
data.append(1)
for ind in range(len(row.indexes), MAXLEN):
rows.append(i)
cols.append(ind * 21)
data.append(1)
data = sparse.csr_matrix((data, (rows, cols)), shape=(n, MAXLEN * 21))
index = np.arange(n)
np.random.seed(seed=0)
np.random.shuffle(index)
print(data[0, :].toarray().reshape(MAXLEN, 21))
return data[index, :]
train_data = get_values(train_df)
test_data = get_values(test_df)
return train_data, test_data
def build_model():
input_seq = Input(shape=(MAXLEN * 21,))
x = Reshape((MAXLEN, 21))(input_seq)
x = Conv1D(320, 7, padding='same')(x)
print(x.get_shape())
x = MaxPooling1D(4, padding='same')(x)
print(x.get_shape())
x = Conv1D(160, 7, padding='same')(x)
print(x.get_shape())
x = Conv1D(320, 7, padding='same')(x)
print(x.get_shape())
x = UpSampling1D(4)(x)
print(x.get_shape())
x = Conv1D(21, 7, activation='sigmoid', padding='same')(x)
print(x.get_shape())
decoded = Reshape((MAXLEN * 21,))(x)
autoencoder = Model(input_seq, decoded)
autoencoder.compile(optimizer='sgd', loss='mean_squared_error')
autoencoder.summary()
return autoencoder
def build_model_lstm():
input_seq = Input(shape=(MAXLEN,))
x = Reshape((MAXLEN, 1))(input_seq)
x = Masking(mask_value=0)(x)
x = LSTM(256)(x)
x = RepeatVector(MAXLEN)(x)
x = LSTM(1, return_sequences=True)(x)
decoded = Reshape((MAXLEN,))(x)
autoencoder = Model(input_seq, decoded)
autoencoder.compile(optimizer='rmsprop', loss='binary_crossentropy')
autoencoder.summary()
return autoencoder
def build_model_dense():
input_seq = Input(shape=(MAXLEN,))
x = Dense(1600)(input_seq)
x = Dense(1200)(x)
encoded = Dense(800)(x)
x = Dense(1200)(encoded)
x = Dense(1600)(x)
decoded = Dense(MAXLEN, activation='sigmoid')(x)
autoencoder = Model(input_seq, decoded)
optimizer = SGD(lr=0.1)
autoencoder.compile(optimizer='sgd', loss='mean_squared_error')
autoencoder.summary()
return autoencoder
def train(data, batch_size, epochs, model_file, validation_split=0.8):
index = np.arange(data.shape[0])
train_n = int(data.shape[0] * validation_split)
train_data, valid_data = data[index[:train_n], :], data[index[train_n:], :]
train_generator = DataGenerator(batch_size)
train_generator.fit(train_data, train_data)
valid_generator = DataGenerator(batch_size)
valid_generator.fit(valid_data, valid_data)
steps = int(math.ceil(train_n / batch_size))
valid_n = data.shape[0] - train_n
valid_steps = int(math.ceil(valid_n / batch_size))
checkpointer = ModelCheckpoint(
filepath=model_file,
verbose=1, save_best_only=True)
earlystopper = EarlyStopping(monitor='val_loss', patience=100, verbose=1)
model = build_model()
model.fit_generator(
train_generator,
steps_per_epoch=steps, epochs=epochs,
validation_data=valid_generator, validation_steps=valid_steps,
callbacks=[earlystopper, checkpointer])
def test(data, batch_size, model_file):
model = load_model(model_file)
print(np.round(model.layers[1].get_weights()[0], 3))
generator = DataGenerator(batch_size)
generator.fit(data, data)
steps = int(math.ceil(data.shape[0] / batch_size))
loss = model.evaluate_generator(generator , steps=steps)
print('Test loss %f' % (loss, ))
preds = model.predict_generator(generator, steps=steps)
# preds = preds.reshape(data.shape[0], MAXLEN, 21)
# preds = np.argmax(preds, axis=2)
real = data.toarray() #.reshape(data.shape[0], MAXLEN)
# real = np.argmax(real, axis=2)
preds = np.round(preds * 20).astype(np.int32)
real = np.round(real * 20).astype(np.int32)
for i in range(10):
c = 0
l = 0
lp = 0
print(preds[i])
print(real[i])
for j in range(len(real[i])):
if real[i, j] != 0 and real[i, j] == preds[i, j]:
c += 1
elif l == 0 and real[i, j] == 0:
l = j
if lp == 0 and preds[i, j] == 0:
lp = j
print('Match %d, Length %d, Length pred %d' % (c, l, lp))
if __name__ == '__main__':
main()