2017-09-17 17:26:09 +02:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2017-07-05 18:10:22 +02:00
|
|
|
import keras
|
2017-09-17 17:26:09 +02:00
|
|
|
from keras.activations import elu
|
2017-09-07 14:24:55 +02:00
|
|
|
from keras.engine import Input, Model as KerasModel
|
2017-09-17 17:26:09 +02:00
|
|
|
from keras.layers import Conv1D, Dense, Dropout, Embedding, GlobalAveragePooling1D, GlobalMaxPooling1D, MaxPool1D, \
|
|
|
|
TimeDistributed
|
2017-07-05 18:10:22 +02:00
|
|
|
|
2017-07-30 13:47:11 +02:00
|
|
|
import dataset
|
2017-07-05 18:10:22 +02:00
|
|
|
|
2017-09-17 17:26:09 +02:00
|
|
|
|
|
|
|
def selu(x):
|
|
|
|
"""Scaled Exponential Linear Unit. (Klambauer et al., 2017)
|
|
|
|
# Arguments
|
|
|
|
x: A tensor or variable to compute the activation function for.
|
|
|
|
# References
|
|
|
|
- [Self-Normalizing Neural Networks](https://arxiv.org/abs/1706.02515)
|
|
|
|
# copied from keras.io
|
|
|
|
"""
|
|
|
|
alpha = 1.6732632423543772848170429916717
|
|
|
|
scale = 1.0507009873554804934193349852946
|
|
|
|
return scale * elu(x, alpha)
|
|
|
|
|
2017-09-07 14:24:55 +02:00
|
|
|
|
|
|
|
Model = namedtuple("Model", ["in_domains", "in_flows", "out_client", "out_server"])
|
|
|
|
|
2017-07-30 13:47:11 +02:00
|
|
|
|
|
|
|
def get_embedding(embedding_size, input_length, filter_size, kernel_size, hidden_dims, drop_out=0.5):
|
2017-07-05 18:10:22 +02:00
|
|
|
x = y = Input(shape=(input_length,))
|
2017-07-30 13:47:11 +02:00
|
|
|
y = Embedding(input_dim=dataset.get_vocab_size(), output_dim=embedding_size)(y)
|
2017-09-17 17:26:09 +02:00
|
|
|
y = Conv1D(filter_size, kernel_size=5, activation=selu)(y)
|
|
|
|
y = Conv1D(filter_size, kernel_size=3, activation=selu)(y)
|
|
|
|
y = Conv1D(filter_size, kernel_size=3, activation=selu)(y)
|
2017-07-28 17:25:08 +02:00
|
|
|
y = GlobalAveragePooling1D()(y)
|
2017-09-22 10:01:12 +02:00
|
|
|
y = Dense(hidden_dims, activation=selu)(y)
|
2017-09-07 14:24:55 +02:00
|
|
|
return KerasModel(x, y)
|
2017-07-05 18:10:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_model(cnnDropout, flow_features, domain_features, window_size, domain_length, cnn_dims, kernel_size,
|
2017-08-02 12:58:09 +02:00
|
|
|
dense_dim, cnn, model_output="both"):
|
2017-07-05 18:10:22 +02:00
|
|
|
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-09-17 17:26:09 +02:00
|
|
|
y = Conv1D(filters=cnn_dims, kernel_size=kernel_size, activation=selu, 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-09-17 17:26:09 +02:00
|
|
|
y = Conv1D(filters=cnn_dims, kernel_size=kernel_size, activation=selu, padding="same")(y)
|
2017-07-06 16:27:47 +02:00
|
|
|
y = MaxPool1D(pool_size=3, strides=1)(y)
|
2017-09-17 17:26:09 +02:00
|
|
|
y = Conv1D(filters=cnn_dims, kernel_size=kernel_size, activation=selu, 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)
|
2017-09-17 17:26:09 +02:00
|
|
|
y = Dense(dense_dim, activation=selu)(y)
|
2017-09-22 10:01:12 +02:00
|
|
|
y = Dense(dense_dim, activation=selu)(y)
|
2017-09-07 14:24:55 +02:00
|
|
|
out_client = Dense(1, activation='sigmoid', name="client")(y)
|
|
|
|
out_server = Dense(1, activation='sigmoid', name="server")(y)
|
2017-07-05 18:10:22 +02:00
|
|
|
|
2017-09-07 14:24:55 +02:00
|
|
|
return Model(ipt_domains, ipt_flows, out_client, out_server)
|
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,
|
2017-08-02 12:58:09 +02:00
|
|
|
dense_dim, cnn, model_output="both"):
|
2017-07-29 19:42:36 +02:00
|
|
|
ipt_domains = Input(shape=(window_size, domain_length), name="ipt_domains")
|
|
|
|
ipt_flows = Input(shape=(window_size, flow_features), name="ipt_flows")
|
2017-09-17 17:26:09 +02:00
|
|
|
encoded = TimeDistributed(cnn, name="domain_cnn")(ipt_domains)
|
2017-08-05 09:33:07 +02:00
|
|
|
merged = keras.layers.concatenate([encoded, ipt_flows], -1)
|
2017-09-20 14:43:28 +02:00
|
|
|
y = Dense(dense_dim, activation=selu)(merged)
|
2017-09-22 10:01:12 +02:00
|
|
|
y = Dense(dense_dim,
|
|
|
|
activation="relu",
|
|
|
|
name="dense_server")(y)
|
2017-09-07 14:24:55 +02:00
|
|
|
out_server = Dense(1, activation="sigmoid", name="server")(y)
|
2017-09-17 17:26:09 +02:00
|
|
|
merged = keras.layers.concatenate([merged, y], -1)
|
2017-08-05 09:33:07 +02:00
|
|
|
# CNN processing a small slides of flow windows
|
2017-09-17 17:26:09 +02:00
|
|
|
y = Conv1D(filters=cnn_dims,
|
|
|
|
kernel_size=kernel_size,
|
|
|
|
activation=selu,
|
|
|
|
padding="same",
|
|
|
|
input_shape=(window_size, domain_features + flow_features))(merged)
|
|
|
|
y = MaxPool1D(pool_size=3,
|
|
|
|
strides=1)(y)
|
|
|
|
y = Conv1D(filters=cnn_dims,
|
|
|
|
kernel_size=kernel_size,
|
|
|
|
activation=selu,
|
|
|
|
padding="same")(y)
|
|
|
|
y = MaxPool1D(pool_size=3,
|
|
|
|
strides=1)(y)
|
|
|
|
y = Conv1D(filters=cnn_dims,
|
|
|
|
kernel_size=kernel_size,
|
|
|
|
activation=selu,
|
|
|
|
padding="same")(y)
|
2017-07-29 19:42:36 +02:00
|
|
|
# remove temporal dimension by global max pooling
|
|
|
|
y = GlobalMaxPooling1D()(y)
|
|
|
|
y = Dropout(dropout)(y)
|
2017-09-22 10:01:12 +02:00
|
|
|
y = Dense(dense_dim, activation=selu)(y)
|
2017-09-17 17:26:09 +02:00
|
|
|
y = Dense(dense_dim,
|
|
|
|
activation=selu,
|
|
|
|
name="dense_client")(y)
|
2017-09-07 14:24:55 +02:00
|
|
|
out_client = Dense(1, activation='sigmoid', name="client")(y)
|
|
|
|
|
|
|
|
return Model(ipt_domains, ipt_flows, out_client, out_server)
|
2017-07-29 19:42:36 +02:00
|
|
|
|