2017-11-07 20:09:20 +01:00
|
|
|
from keras.models import Model
|
2017-07-14 14:58:17 +02:00
|
|
|
|
2017-11-07 20:32:08 +01:00
|
|
|
from . import networks
|
|
|
|
from .metrics import *
|
2017-07-07 08:43:16 +02:00
|
|
|
|
|
|
|
|
2017-11-07 20:09:20 +01:00
|
|
|
def create_model(model, output_type):
|
|
|
|
if output_type == "both":
|
|
|
|
return Model(inputs=[model.in_domains, model.in_flows], outputs=(model.out_client, model.out_server))
|
|
|
|
elif output_type == "client":
|
|
|
|
return Model(inputs=[model.in_domains, model.in_flows], outputs=(model.out_client,))
|
|
|
|
else:
|
|
|
|
raise Exception("unknown model output")
|
2017-11-07 20:32:08 +01:00
|
|
|
|
2017-11-07 20:09:20 +01:00
|
|
|
|
2017-07-07 08:43:16 +02:00
|
|
|
def get_models_by_params(params: dict):
|
|
|
|
# decomposing param section
|
|
|
|
# mainly embedding model
|
2017-11-07 20:09:20 +01:00
|
|
|
network_type = params.get("type")
|
2017-11-07 20:47:41 +01: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-29 19:42:36 +02:00
|
|
|
|
2017-11-07 20:32:08 +01:00
|
|
|
domain_cnn = networks.get_domain_embedding_model(embedding_size, domain_length, filter_embedding, kernel_embedding,
|
|
|
|
hidden_embedding, 0.5)
|
|
|
|
|
2017-11-07 20:09:20 +01:00
|
|
|
if network_type == "final":
|
2017-11-07 20:32:08 +01:00
|
|
|
model = networks.get_final_model(0.25, flow_features, window_size, domain_length,
|
|
|
|
filter_main, kernel_main, dense_dim, domain_cnn)
|
2017-11-07 20:09:20 +01:00
|
|
|
model = create_model(model, model_output)
|
|
|
|
elif network_type in ("inter", "staggered"):
|
2017-11-07 20:32:08 +01:00
|
|
|
model = networks.get_inter_model(0.25, flow_features, window_size, domain_length,
|
|
|
|
filter_main, kernel_main, dense_dim, domain_cnn)
|
2017-11-07 20:09:20 +01:00
|
|
|
model = create_model(model, model_output)
|
|
|
|
elif network_type == "long":
|
2017-11-07 20:32:08 +01:00
|
|
|
model = networks.get_long_model(0.25, flow_features, window_size, domain_length,
|
2017-11-07 20:09:20 +01:00
|
|
|
filter_main, kernel_main, dense_dim, domain_cnn)
|
|
|
|
model = create_model(model, model_output)
|
|
|
|
elif network_type == "soft":
|
2017-11-07 20:32:08 +01:00
|
|
|
model = networks.get_long_model(0.25, flow_features, window_size, domain_length,
|
|
|
|
filter_main, kernel_main, dense_dim, domain_cnn)
|
2017-11-07 20:09:20 +01:00
|
|
|
model = create_model(model, model_output)
|
|
|
|
conv_server = model.get_layer("conv_server").trainable_weights
|
|
|
|
conv_client = model.get_layer("conv_client").trainable_weights
|
|
|
|
l1 = [0.001 * K.sum(K.abs(x - y)) for (x, y) in zip(conv_server, conv_client)]
|
|
|
|
model.add_loss(l1)
|
|
|
|
|
|
|
|
dense_server = model.get_layer("dense_server").trainable_weights
|
|
|
|
dense_client = model.get_layer("dense_client").trainable_weights
|
|
|
|
l2 = [0.001 * K.sum(K.abs(x - y)) for (x, y) in zip(dense_server, dense_client)]
|
|
|
|
model.add_loss(l2)
|
|
|
|
else:
|
|
|
|
raise ValueError("network type not found")
|
2017-11-06 21:51:49 +01:00
|
|
|
|
2017-11-07 20:09:20 +01:00
|
|
|
return model
|
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")
|
2017-11-07 20:32:08 +01:00
|
|
|
|
|
|
|
embedding_model = networks.get_domain_embedding_model(embedding_size, input_length, filter_embedding,
|
|
|
|
kernel_embedding,
|
|
|
|
hidden_embedding, 0.5)
|
2017-10-05 15:26:53 +02:00
|
|
|
|
|
|
|
return networks.get_server_model(flow_features, domain_length, dense_dim, embedding_model)
|