5 changed files with 132 additions and 107 deletions
@ -0,0 +1,68 @@
|
||||
import numpy as np |
||||
from keras.utils import np_utils |
||||
|
||||
import dataset |
||||
import models |
||||
|
||||
|
||||
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") |
||||
(X_tr, y_tr, hits_tr, names_tr) = dataset.create_dataset_from_flows( |
||||
user_flow_df, char_dict, |
||||
maxLen=maxLen, threshold=threshold, windowSize=windowSize) |
||||
|
||||
pos_idx = np.where(y_tr == 1.0)[0] |
||||
neg_idx = np.where(y_tr == 0.0)[0] |
||||
|
||||
use_idx = np.concatenate((pos_idx, neg_idx)) |
||||
|
||||
y_tr = y_tr[use_idx] |
||||
# hits_tr = hits_tr[use_idx] |
||||
# names_tr = names_tr[use_idx] |
||||
for i in range(len(X_tr)): |
||||
X_tr[i] = X_tr[i][use_idx] |
||||
|
||||
# TODO: WTF? I don't get it... |
||||
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 |
||||
y_tr = np_utils.to_categorical(y_tr, 2) |
||||
model.fit(x=X_tr, y=y_tr, batch_size=128, |
||||
epochs=epochNumber + 1, shuffle=True, initial_epoch=epochNumber) # , |
||||
# validation_data=(testData,testLabel)) |
||||
|
||||
|
||||
if __name__ == "__main__": |
||||
main() |
@ -0,0 +1,53 @@
|
||||
import keras |
||||
from keras.engine import Input, Model |
||||
from keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense, Dropout, Activation, Reshape |
||||
|
||||
|
||||
def get_shared_cnn(vocabSize, embeddingSize, input_length, filters, kernel_size, |
||||
hidden_dims, drop_out): |
||||
x = y = Input(shape=(input_length,)) |
||||
y = Embedding(input_dim=vocabSize, output_dim=embeddingSize)(y) |
||||
y = Conv1D(filters, kernel_size, activation='relu')(y) |
||||
y = GlobalMaxPooling1D()(y) |
||||
y = Dense(hidden_dims)(y) |
||||
y = Dropout(drop_out)(y) |
||||
y = Activation('relu')(y) |
||||
return Model(x, y) |
||||
|
||||
|
||||
def get_full_model(vocabSize, embeddingSize, maxLen, domainFeatures, flowFeatures, |
||||
filters, h1, h2, dropout, dense): |
||||
pass |
||||
|
||||
|
||||
def get_top_cnn(cnn, numFeatures, maxLen, windowSize, domainFeatures, filters, kernel_size, cnnHiddenDims, cnnDropout): |
||||
inputList = [] |
||||
encodedList = [] |
||||
# TODO: ??? |
||||
for i in range(windowSize): |
||||
inputList.append(Input(shape=(maxLen,))) |
||||
encodedList.append(cnn(inputList[-1])) # add shared domain model |
||||
inputList.append(Input(shape=(numFeatures,))) |
||||
# TODO: ??? |
||||
merge_layer_input = [] |
||||
for i in range(windowSize): |
||||
merge_layer_input.append(encodedList[i]) |
||||
merge_layer_input.append(inputList[(2 * i) + 1]) |
||||
# We can then concatenate the two vectors: |
||||
merged_vector = keras.layers.concatenate(merge_layer_input, axis=-1) |
||||
reshape = Reshape((windowSize, domainFeatures + numFeatures))(merged_vector) |
||||
# add second cnn |
||||
cnn = Conv1D(filters, |
||||
kernel_size, |
||||
activation='relu', |
||||
input_shape=(windowSize, domainFeatures + numFeatures))(reshape) |
||||
# we use max pooling: |
||||
maxPool = GlobalMaxPooling1D()(cnn) |
||||
cnnDropout = Dropout(cnnDropout)(maxPool) |
||||
cnnDense = Dense(cnnHiddenDims, activation='relu')(cnnDropout) |
||||
cnnOutput = Dense(2, activation='softmax')(cnnDense) |
||||
|
||||
# We define a trainable model linking the |
||||
# tweet inputs to the predictions |
||||
model = Model(inputs=inputList, outputs=cnnOutput) |
||||
return model |
Loading…
Reference in new issue