Browse Source

refactor network to use new input format

master
René Knaebel 1 year ago
parent
commit
59c1176e85
2 changed files with 21 additions and 30 deletions
  1. 3
    0
      Makefile
  2. 18
    30
      models.py

+ 3
- 0
Makefile View File

@@ -0,0 +1,3 @@
1
+test:
2
+	python3 main.py --epochs 1 --batch 64
3
+

+ 18
- 30
models.py View File

@@ -1,12 +1,12 @@
1 1
 import keras
2 2
 from keras.engine import Input, Model
3
-from keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense, Dropout, Activation, Reshape
3
+from keras.layers import Embedding, Conv1D, GlobalMaxPooling1D, Dense, Dropout, Activation, TimeDistributed
4 4
 
5 5
 
6
-def get_shared_cnn(vocabSize, embeddingSize, input_length, filters, kernel_size,
6
+def get_shared_cnn(vocab_size, embedding_size, input_length, filters, kernel_size,
7 7
                    hidden_dims, drop_out):
8 8
     x = y = Input(shape=(input_length,))
9
-    y = Embedding(input_dim=vocabSize, output_dim=embeddingSize)(y)
9
+    y = Embedding(input_dim=vocab_size, output_dim=embedding_size)(y)
10 10
     y = Conv1D(filters, kernel_size, activation='relu')(y)
11 11
     y = GlobalMaxPooling1D()(y)
12 12
     y = Dense(hidden_dims)(y)
@@ -21,33 +21,21 @@ def get_full_model(vocabSize, embeddingSize, maxLen, domainFeatures, flowFeature
21 21
 
22 22
 
23 23
 def get_top_cnn(cnn, numFeatures, maxLen, windowSize, domainFeatures, filters, kernel_size, cnnHiddenDims, cnnDropout):
24
-    inputList = []
25
-    encodedList = []
26
-    # TODO: ???
27
-    for i in range(windowSize):
28
-        inputList.append(Input(shape=(maxLen,)))
29
-        encodedList.append(cnn(inputList[-1]))  # add shared domain model
30
-        inputList.append(Input(shape=(numFeatures,)))
31
-    # TODO: ???
32
-    merge_layer_input = []
33
-    for i in range(windowSize):
34
-        merge_layer_input.append(encodedList[i])
35
-        merge_layer_input.append(inputList[(2 * i) + 1])
36
-    # We can then concatenate the two vectors:
37
-    merged_vector = keras.layers.concatenate(merge_layer_input, axis=-1)
38
-    reshape = Reshape((windowSize, domainFeatures + numFeatures))(merged_vector)
24
+    ipt_domains = Input(shape=(windowSize, maxLen), name="ipt_domains")
25
+    encoded = TimeDistributed(cnn)(ipt_domains)
26
+    ipt_flows = Input(shape=(windowSize, numFeatures), name="ipt_flows")
27
+    merged = keras.layers.concatenate([encoded, ipt_flows], -1)
39 28
     # add second cnn
40
-    cnn = Conv1D(filters,
41
-                 kernel_size,
42
-                 activation='relu',
43
-                 input_shape=(windowSize, domainFeatures + numFeatures))(reshape)
29
+    y = Conv1D(filters,
30
+               kernel_size,
31
+               activation='relu',
32
+               input_shape=(windowSize, domainFeatures + numFeatures))(merged)
33
+    # TODO: why global pooling? -> 3D to 2D
44 34
     # we use max pooling:
45
-    maxPool = GlobalMaxPooling1D()(cnn)
46
-    cnnDropout = Dropout(cnnDropout)(maxPool)
47
-    cnnDense = Dense(cnnHiddenDims, activation='relu')(cnnDropout)
48
-    cnnOutput1 = Dense(2, activation='softmax', name="client")(cnnDense)
49
-    cnnOutput2 = Dense(2, activation='softmax', name="server")(cnnDense)
35
+    y = GlobalMaxPooling1D()(y)
36
+    y = Dropout(cnnDropout)(y)
37
+    y = Dense(cnnHiddenDims, activation='relu')(y)
38
+    y1 = Dense(2, activation='softmax', name="client")(y)
39
+    y2 = Dense(2, activation='softmax', name="server")(y)
50 40
 
51
-    # We define a trainable model linking the
52
-    # tweet inputs to the predictions
53
-    return Model(inputs=inputList, outputs=(cnnOutput1, cnnOutput2))
41
+    return Model(inputs=[ipt_domains, ipt_flows], outputs=(y1, y2))

Loading…
Cancel
Save