2017-07-05 18:10:22 +02:00
|
|
|
import keras
|
|
|
|
from keras.engine import Input, Model
|
2017-07-28 17:25:08 +02:00
|
|
|
from keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense, Dropout, TimeDistributed, MaxPool1D, \
|
|
|
|
GlobalAveragePooling1D
|
2017-07-05 18:10:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_embedding(vocab_size, embedding_size, input_length,
|
2017-07-07 08:43:16 +02:00
|
|
|
filter_size, kernel_size, hidden_dims, drop_out=0.5):
|
2017-07-05 18:10:22 +02:00
|
|
|
x = y = Input(shape=(input_length,))
|
|
|
|
y = Embedding(input_dim=vocab_size, output_dim=embedding_size)(y)
|
2017-07-07 08:43:16 +02:00
|
|
|
y = Conv1D(filter_size, kernel_size=5, activation='relu')(y)
|
|
|
|
y = Conv1D(filter_size, kernel_size=3, activation='relu')(y)
|
|
|
|
y = Conv1D(filter_size, kernel_size=3, activation='relu')(y)
|
2017-07-28 17:25:08 +02:00
|
|
|
y = GlobalAveragePooling1D()(y)
|
2017-07-05 18:10:22 +02:00
|
|
|
y = Dense(hidden_dims, activation="relu")(y)
|
|
|
|
return Model(x, y)
|
|
|
|
|
|
|
|
|
|
|
|
def get_model(cnnDropout, flow_features, domain_features, window_size, domain_length, cnn_dims, kernel_size,
|
|
|
|
dense_dim, cnn):
|
|
|
|
ipt_domains = Input(shape=(window_size, domain_length), name="ipt_domains")
|
|
|
|
encoded = TimeDistributed(cnn)(ipt_domains)
|
|
|
|
ipt_flows = Input(shape=(window_size, flow_features), name="ipt_flows")
|
|
|
|
merged = keras.layers.concatenate([encoded, ipt_flows], -1)
|
|
|
|
# CNN processing a small slides of flow windows
|
2017-07-14 21:01:08 +02:00
|
|
|
y = Conv1D(filters=cnn_dims, kernel_size=kernel_size, activation='relu', padding="same",
|
2017-07-05 18:10:22 +02:00
|
|
|
input_shape=(window_size, domain_features + flow_features))(merged)
|
|
|
|
y = MaxPool1D(pool_size=3, strides=1)(y)
|
2017-07-14 21:01:08 +02:00
|
|
|
y = Conv1D(filters=cnn_dims, kernel_size=kernel_size, activation='relu', padding="same")(y)
|
2017-07-06 16:27:47 +02:00
|
|
|
y = MaxPool1D(pool_size=3, strides=1)(y)
|
2017-07-14 21:01:08 +02:00
|
|
|
y = Conv1D(filters=cnn_dims, kernel_size=kernel_size, activation='relu', padding="same")(y)
|
2017-07-05 18:10:22 +02:00
|
|
|
# remove temporal dimension by global max pooling
|
|
|
|
y = GlobalMaxPooling1D()(y)
|
|
|
|
y = Dropout(cnnDropout)(y)
|
|
|
|
y = Dense(dense_dim, activation='relu')(y)
|
2017-07-07 08:43:16 +02:00
|
|
|
y = Dense(dense_dim // 2, activation='relu')(y)
|
2017-07-30 12:50:26 +02:00
|
|
|
y1 = Dense(1, activation='sigmoid', name="client")(y)
|
|
|
|
y2 = Dense(1, activation='sigmoid', name="server")(y)
|
2017-07-05 18:10:22 +02:00
|
|
|
|
|
|
|
return Model(inputs=[ipt_domains, ipt_flows], outputs=(y1, y2))
|
2017-07-29 19:42:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_new_model(dropout, flow_features, domain_features, window_size, domain_length, cnn_dims, kernel_size,
|
|
|
|
dense_dim, cnn):
|
|
|
|
ipt_domains = Input(shape=(window_size, domain_length), name="ipt_domains")
|
|
|
|
ipt_flows = Input(shape=(window_size, flow_features), name="ipt_flows")
|
|
|
|
encoded = TimeDistributed(cnn)(ipt_domains)
|
|
|
|
|
2017-07-30 12:50:26 +02:00
|
|
|
y2 = Dense(1, activation="sigmoid", name="server")(encoded)
|
2017-07-29 19:42:36 +02:00
|
|
|
merged = keras.layers.concatenate([encoded, ipt_flows, y2], -1)
|
|
|
|
|
|
|
|
y = Conv1D(cnn_dims,
|
|
|
|
kernel_size,
|
|
|
|
activation='relu',
|
|
|
|
input_shape=(window_size, domain_features + flow_features))(merged)
|
|
|
|
# remove temporal dimension by global max pooling
|
|
|
|
y = GlobalMaxPooling1D()(y)
|
|
|
|
y = Dropout(dropout)(y)
|
|
|
|
y = Dense(dense_dim, activation='relu')(y)
|
|
|
|
|
2017-07-30 12:50:26 +02:00
|
|
|
y1 = Dense(1, activation='sigmoid', name="client")(y)
|
2017-07-29 19:42:36 +02:00
|
|
|
model = Model(inputs=[ipt_domains, ipt_flows], outputs=(y1, y2))
|
|
|
|
|
|
|
|
return model
|