2017-07-14 14:58:17 +02:00
|
|
|
import keras.backend as K
|
|
|
|
|
2017-10-09 14:19:01 +02:00
|
|
|
from models import deep1
|
2017-09-22 10:01:12 +02:00
|
|
|
from models.renes_networks import selu
|
2017-09-20 14:43:28 +02:00
|
|
|
from . import flat_2, pauls_networks, renes_networks
|
2017-07-07 08:43:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_models_by_params(params: dict):
|
|
|
|
# decomposing param section
|
|
|
|
# mainly embedding model
|
2017-10-05 15:26:53 +02:00
|
|
|
# network_type = params.get("type")
|
2017-09-01 10:42:26 +02:00
|
|
|
network_depth = params.get("depth")
|
2017-10-03 18:58:54 +02:00
|
|
|
embedding_size = params.get("embedding")
|
2017-07-07 08:43:16 +02:00
|
|
|
filter_embedding = params.get("filter_embedding")
|
|
|
|
kernel_embedding = params.get("kernel_embedding")
|
2017-09-07 15:53:58 +02:00
|
|
|
hidden_embedding = params.get("dense_embedding")
|
2017-10-05 15:26:53 +02:00
|
|
|
# dropout = params.get("dropout")
|
2017-07-07 08:43:16 +02:00
|
|
|
# mainly prediction model
|
|
|
|
flow_features = params.get("flow_features")
|
|
|
|
window_size = params.get("window_size")
|
|
|
|
domain_length = params.get("domain_length")
|
|
|
|
filter_main = params.get("filter_main")
|
2017-09-07 15:53:58 +02:00
|
|
|
kernel_main = params.get("kernel_main")
|
2017-07-07 08:43:16 +02:00
|
|
|
dense_dim = params.get("dense_main")
|
2017-08-02 12:58:09 +02:00
|
|
|
model_output = params.get("model_output", "both")
|
2017-07-07 08:43:16 +02:00
|
|
|
# create models
|
2017-09-22 10:01:12 +02:00
|
|
|
if network_depth == "flat1":
|
2017-09-01 10:42:26 +02:00
|
|
|
networks = pauls_networks
|
2017-09-22 10:01:12 +02:00
|
|
|
elif network_depth == "flat2":
|
2017-09-20 14:43:28 +02:00
|
|
|
networks = flat_2
|
2017-09-22 10:01:12 +02:00
|
|
|
elif network_depth == "deep1":
|
2017-10-09 15:10:15 +02:00
|
|
|
networks = deep1
|
|
|
|
elif network_depth == "deep2":
|
2017-09-01 10:42:26 +02:00
|
|
|
networks = renes_networks
|
|
|
|
else:
|
|
|
|
raise Exception("network not found")
|
2017-10-09 14:19:01 +02:00
|
|
|
embedding_model = networks.get_embedding(embedding_size, domain_length, filter_embedding, kernel_embedding,
|
2017-09-07 15:53:58 +02:00
|
|
|
hidden_embedding, 0.5)
|
2017-07-07 08:43:16 +02:00
|
|
|
|
2017-11-05 22:52:50 +01:00
|
|
|
final = networks.get_model(0.25, flow_features, hidden_embedding, window_size, domain_length,
|
|
|
|
filter_main, kernel_main, dense_dim, embedding_model, model_output)
|
|
|
|
|
|
|
|
inter = networks.get_new_model(0.25, flow_features, hidden_embedding, window_size, domain_length,
|
2017-09-07 14:24:55 +02:00
|
|
|
filter_main, kernel_main, dense_dim, embedding_model, model_output)
|
2017-07-07 08:43:16 +02:00
|
|
|
|
2017-11-05 22:52:50 +01:00
|
|
|
long = networks.get_new_model2(0.25, flow_features, hidden_embedding, window_size, domain_length,
|
|
|
|
filter_main, kernel_main, dense_dim, embedding_model, model_output)
|
2017-07-29 19:42:36 +02:00
|
|
|
|
2017-11-06 21:51:49 +01:00
|
|
|
soft = networks.get_new_soft(0.25, flow_features, hidden_embedding, window_size, domain_length,
|
|
|
|
filter_main, kernel_main, dense_dim, embedding_model, model_output)
|
|
|
|
|
|
|
|
return embedding_model, final, inter, long, soft
|
2017-07-14 14:58:17 +02:00
|
|
|
|
|
|
|
|
2017-10-05 15:26:53 +02:00
|
|
|
def get_server_model_by_params(params: dict):
|
|
|
|
# decomposing param section
|
|
|
|
# mainly embedding model
|
|
|
|
network_depth = params.get("depth")
|
|
|
|
embedding_size = params.get("embedding")
|
|
|
|
input_length = params.get("input_length")
|
|
|
|
filter_embedding = params.get("filter_embedding")
|
|
|
|
kernel_embedding = params.get("kernel_embedding")
|
|
|
|
hidden_embedding = params.get("dense_embedding")
|
|
|
|
# mainly prediction model
|
|
|
|
flow_features = params.get("flow_features")
|
|
|
|
domain_length = params.get("domain_length")
|
|
|
|
dense_dim = params.get("dense_main")
|
|
|
|
# create models
|
|
|
|
if network_depth == "flat1":
|
|
|
|
networks = pauls_networks
|
|
|
|
elif network_depth == "flat2":
|
|
|
|
networks = flat_2
|
|
|
|
elif network_depth == "deep1":
|
2017-10-09 14:19:01 +02:00
|
|
|
networks = deep1
|
|
|
|
elif network_depth == "deep2":
|
2017-10-05 15:26:53 +02:00
|
|
|
networks = renes_networks
|
|
|
|
else:
|
|
|
|
raise Exception("network not found")
|
|
|
|
|
|
|
|
embedding_model = networks.get_embedding(embedding_size, input_length, filter_embedding, kernel_embedding,
|
|
|
|
hidden_embedding, 0.5)
|
|
|
|
|
|
|
|
return networks.get_server_model(flow_features, domain_length, dense_dim, embedding_model)
|
|
|
|
|
|
|
|
|
|
|
|
def get_custom_objects():
|
2017-07-14 14:58:17 +02:00
|
|
|
return dict([
|
|
|
|
("precision", precision),
|
|
|
|
("recall", recall),
|
|
|
|
("f1_score", f1_score),
|
2017-09-22 10:01:12 +02:00
|
|
|
("selu", selu)
|
2017-07-14 14:58:17 +02:00
|
|
|
])
|
|
|
|
|
|
|
|
|
|
|
|
def get_metric_functions():
|
|
|
|
return [precision, recall, f1_score]
|
|
|
|
|
|
|
|
|
|
|
|
def precision(y_true, y_pred):
|
|
|
|
# Count positive samples.
|
|
|
|
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
|
|
|
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
|
|
|
|
return true_positives / (predicted_positives + K.epsilon())
|
|
|
|
|
|
|
|
|
|
|
|
def recall(y_true, y_pred):
|
|
|
|
# Count positive samples.
|
|
|
|
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
|
|
|
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
|
|
|
|
return true_positives / (possible_positives + K.epsilon())
|
|
|
|
|
|
|
|
|
|
|
|
def f1_score(y_true, y_pred):
|
|
|
|
return f_score(1)(y_true, y_pred)
|
|
|
|
|
|
|
|
|
|
|
|
def f05_score(y_true, y_pred):
|
|
|
|
return f_score(0.5)(y_true, y_pred)
|
|
|
|
|
|
|
|
|
|
|
|
def f_score(beta):
|
|
|
|
def _f(y_true, y_pred):
|
|
|
|
p = precision(y_true, y_pred)
|
|
|
|
r = recall(y_true, y_pred)
|
|
|
|
|
|
|
|
bb = beta ** 2
|
|
|
|
|
|
|
|
fbeta_score = (1 + bb) * (p * r) / (bb * p + r + K.epsilon())
|
|
|
|
|
|
|
|
return fbeta_score
|
|
|
|
|
|
|
|
return _f
|