2017-06-30 10:12:20 +02:00
|
|
|
import numpy as np
|
|
|
|
from keras.utils import np_utils
|
|
|
|
|
|
|
|
import dataset
|
|
|
|
import models
|
|
|
|
|
|
|
|
|
2017-06-30 10:42:21 +02:00
|
|
|
# config = tf.ConfigProto(log_device_placement=True)
|
|
|
|
# config.gpu_options.per_process_gpu_memory_fraction = 0.5
|
|
|
|
# config.gpu_options.allow_growth = True
|
|
|
|
# session = tf.Session(config=config)
|
|
|
|
|
|
|
|
|
2017-06-30 10:12:20 +02:00
|
|
|
def main():
|
|
|
|
# parameter
|
|
|
|
innerCNNFilters = 512
|
|
|
|
innerCNNKernelSize = 2
|
|
|
|
cnnDropout = 0.5
|
|
|
|
cnnHiddenDims = 1024
|
|
|
|
domainFeatures = 512
|
|
|
|
flowFeatures = 3
|
|
|
|
numCiscoFeatures = 30
|
|
|
|
windowSize = 10
|
|
|
|
maxLen = 40
|
|
|
|
embeddingSize = 100
|
|
|
|
kernel_size = 2
|
|
|
|
drop_out = 0.5
|
|
|
|
filters = 2
|
|
|
|
hidden_dims = 100
|
|
|
|
vocabSize = 40
|
|
|
|
threshold = 3
|
|
|
|
minFlowsPerUser = 10
|
|
|
|
numEpochs = 100
|
|
|
|
timesNeg = -1
|
|
|
|
|
|
|
|
char_dict = dataset.get_character_dict()
|
|
|
|
user_flow_df = dataset.get_user_flow_data()
|
|
|
|
|
|
|
|
print("create training dataset")
|
2017-06-30 18:43:50 +02:00
|
|
|
(X_tr, hits_tr, names_tr, server_tr, trusted_hits_tr) = dataset.create_dataset_from_flows(
|
2017-06-30 10:12:20 +02:00
|
|
|
user_flow_df, char_dict,
|
2017-06-30 18:43:50 +02:00
|
|
|
maxLen=maxLen, windowSize=windowSize)
|
|
|
|
# make client labels discrete with 4 different values
|
|
|
|
# TODO: use trusted_hits_tr for client classification too
|
|
|
|
client_labels = np.apply_along_axis(lambda x: dataset.discretize_label(x, 3), 0, np.atleast_2d(hits_tr))
|
|
|
|
# select only 1.0 and 0.0 from training data
|
|
|
|
pos_idx = np.where(client_labels == 1.0)[0]
|
|
|
|
neg_idx = np.where(client_labels == 0.0)[0]
|
2017-06-30 17:19:04 +02:00
|
|
|
idx = np.concatenate((pos_idx, neg_idx))
|
2017-06-30 18:43:50 +02:00
|
|
|
# select labels for prediction
|
|
|
|
client_labels = client_labels[idx]
|
|
|
|
server_labels = server_tr[idx]
|
2017-06-30 10:12:20 +02:00
|
|
|
|
2017-06-30 18:43:50 +02:00
|
|
|
# TODO: remove when features are flattened
|
2017-06-30 10:12:20 +02:00
|
|
|
for i in range(len(X_tr)):
|
2017-06-30 17:19:04 +02:00
|
|
|
X_tr[i] = X_tr[i][idx]
|
2017-06-30 10:12:20 +02:00
|
|
|
|
|
|
|
shared_cnn = models.get_shared_cnn(len(char_dict) + 1, embeddingSize, maxLen,
|
|
|
|
domainFeatures, kernel_size, domainFeatures, 0.5)
|
|
|
|
|
|
|
|
model = models.get_top_cnn(shared_cnn, flowFeatures, maxLen, windowSize, domainFeatures, filters, kernel_size,
|
|
|
|
cnnHiddenDims, cnnDropout)
|
|
|
|
|
|
|
|
model.compile(optimizer='adam',
|
|
|
|
loss='binary_crossentropy',
|
|
|
|
metrics=['accuracy'])
|
|
|
|
|
|
|
|
epochNumber = 0
|
2017-06-30 18:43:50 +02:00
|
|
|
client_labels = np_utils.to_categorical(client_labels, 2)
|
|
|
|
server_labels = np_utils.to_categorical(server_labels, 2)
|
|
|
|
model.fit(X_tr, [client_labels, server_labels], batch_size=128,
|
2017-06-30 10:12:20 +02:00
|
|
|
epochs=epochNumber + 1, shuffle=True, initial_epoch=epochNumber) # ,
|
|
|
|
# validation_data=(testData,testLabel))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|