refactor network to use new input format
This commit is contained in:
parent
5743127b7f
commit
59c1176e85
48
models.py
48
models.py
@ -1,12 +1,12 @@
|
|||||||
import keras
|
import keras
|
||||||
from keras.engine import Input, Model
|
from keras.engine import Input, Model
|
||||||
from keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense, Dropout, Activation, Reshape
|
from keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense, Dropout, Activation, TimeDistributed
|
||||||
|
|
||||||
|
|
||||||
def get_shared_cnn(vocabSize, embeddingSize, input_length, filters, kernel_size,
|
def get_shared_cnn(vocab_size, embedding_size, input_length, filters, kernel_size,
|
||||||
hidden_dims, drop_out):
|
hidden_dims, drop_out):
|
||||||
x = y = Input(shape=(input_length,))
|
x = y = Input(shape=(input_length,))
|
||||||
y = Embedding(input_dim=vocabSize, output_dim=embeddingSize)(y)
|
y = Embedding(input_dim=vocab_size, output_dim=embedding_size)(y)
|
||||||
y = Conv1D(filters, kernel_size, activation='relu')(y)
|
y = Conv1D(filters, kernel_size, activation='relu')(y)
|
||||||
y = GlobalMaxPooling1D()(y)
|
y = GlobalMaxPooling1D()(y)
|
||||||
y = Dense(hidden_dims)(y)
|
y = Dense(hidden_dims)(y)
|
||||||
@ -21,33 +21,21 @@ def get_full_model(vocabSize, embeddingSize, maxLen, domainFeatures, flowFeature
|
|||||||
|
|
||||||
|
|
||||||
def get_top_cnn(cnn, numFeatures, maxLen, windowSize, domainFeatures, filters, kernel_size, cnnHiddenDims, cnnDropout):
|
def get_top_cnn(cnn, numFeatures, maxLen, windowSize, domainFeatures, filters, kernel_size, cnnHiddenDims, cnnDropout):
|
||||||
inputList = []
|
ipt_domains = Input(shape=(windowSize, maxLen), name="ipt_domains")
|
||||||
encodedList = []
|
encoded = TimeDistributed(cnn)(ipt_domains)
|
||||||
# TODO: ???
|
ipt_flows = Input(shape=(windowSize, numFeatures), name="ipt_flows")
|
||||||
for i in range(windowSize):
|
merged = keras.layers.concatenate([encoded, ipt_flows], -1)
|
||||||
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
|
# add second cnn
|
||||||
cnn = Conv1D(filters,
|
y = Conv1D(filters,
|
||||||
kernel_size,
|
kernel_size,
|
||||||
activation='relu',
|
activation='relu',
|
||||||
input_shape=(windowSize, domainFeatures + numFeatures))(reshape)
|
input_shape=(windowSize, domainFeatures + numFeatures))(merged)
|
||||||
|
# TODO: why global pooling? -> 3D to 2D
|
||||||
# we use max pooling:
|
# we use max pooling:
|
||||||
maxPool = GlobalMaxPooling1D()(cnn)
|
y = GlobalMaxPooling1D()(y)
|
||||||
cnnDropout = Dropout(cnnDropout)(maxPool)
|
y = Dropout(cnnDropout)(y)
|
||||||
cnnDense = Dense(cnnHiddenDims, activation='relu')(cnnDropout)
|
y = Dense(cnnHiddenDims, activation='relu')(y)
|
||||||
cnnOutput1 = Dense(2, activation='softmax', name="client")(cnnDense)
|
y1 = Dense(2, activation='softmax', name="client")(y)
|
||||||
cnnOutput2 = Dense(2, activation='softmax', name="server")(cnnDense)
|
y2 = Dense(2, activation='softmax', name="server")(y)
|
||||||
|
|
||||||
# We define a trainable model linking the
|
return Model(inputs=[ipt_domains, ipt_flows], outputs=(y1, y2))
|
||||||
# tweet inputs to the predictions
|
|
||||||
return Model(inputs=inputList, outputs=(cnnOutput1, cnnOutput2))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user