Skip to content

Commit 0353483

Browse files
committed
Filters: create 2 branches of them. Use functional style in Keras
1 parent bf0a1c0 commit 0353483

File tree

1 file changed

+47
-20
lines changed

1 file changed

+47
-20
lines changed

keras_cnn.py

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import numpy as np
22
import keras
33
from keras.models import Sequential
4-
from keras.layers import Dense, Dropout, Flatten
4+
from keras.layers import Dense, Dropout, Flatten, Merge
55
from keras.layers import Conv2D, MaxPooling2D
66
from keras.optimizers import SGD
7-
import tensorflow as tf
7+
from keras.layers import merge, add
88

99

1010
class SentimentAnalysisAnalyzer(object):
@@ -20,32 +20,59 @@ def fit(self, x_train, y_train, batch_size, epochs):
2020
def evaluate(self, x, y, batch_size):
2121
raise NotImplementedError("'evaluate' has to be implemented")
2222

23+
24+
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout
25+
from keras.models import Model
26+
2327
class WithKeras(SentimentAnalysisAnalyzer):
2428

2529
def __init__(self, features_in_words, words_in_review):
2630

2731
SentimentAnalysisAnalyzer.__init__(self, features_in_words, words_in_review)
28-
29-
# # n_features_in_word = mw.model.vector_size
30-
# n_features_in_word = 300 # TODO: replace this by previous line
31-
# n_words_in_review = 10 # number of words to take from each review
32-
33-
self.model = Sequential()
34-
# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.
35-
# this applies 1 convolution filter(s) of size 3x3 each.
36-
filter_depth = 5
37-
self.model.add(Conv2D(filter_depth, (3, self.n_features_in_word), activation='relu', input_shape=(self.n_words_in_review, self.n_features_in_word, self.input_channels), strides=(1, 1), padding='valid'))
38-
self.model.add(MaxPooling2D(pool_size=(2, 1), strides = (1,1)))
39-
self.model.add(Dropout(0.25))
40-
41-
self.model.add(Flatten())
42-
self.model.add(Dense(256, activation='relu'))
43-
self.model.add(Dropout(0.5))
44-
self.model.add(Dense(2, activation='softmax'))
45-
32+
# inputs
33+
inputs = Input(shape=(self.n_words_in_review, self.n_features_in_word, self.input_channels))
34+
# intermediate results
35+
# 2D 3x3 convolution followed by a maxpool
36+
branch1 =Conv2D(
37+
filters=5,
38+
kernel_size=(3, self.n_features_in_word),
39+
activation='relu',
40+
input_shape=(self.n_words_in_review, self.n_features_in_word, self.input_channels),
41+
strides=(1, 1),
42+
padding='valid')(inputs)
43+
branch1 = MaxPooling2D(pool_size=(2, 1), strides = (1,1))(branch1)
44+
branch1 = Dropout(0.25)(branch1)
45+
46+
# 2D 4x4 convolution followed by a maxpool
47+
branch2 =Conv2D(
48+
filters=5,
49+
kernel_size=(4, self.n_features_in_word),
50+
activation='relu',
51+
input_shape=(self.n_words_in_review, self.n_features_in_word, self.input_channels),
52+
strides=(1, 1),
53+
padding='valid')(inputs)
54+
branch2 = MaxPooling2D(pool_size=(2, 1), strides = (1,1))(branch2)
55+
branch2 = Dropout(0.25)(branch2)
56+
57+
# now let's go fully-connected to a 2-way classification:
58+
merged_branches = keras.layers.concatenate([branch1, branch2], axis = 1) # , axis=-1)
59+
60+
# fully-connected
61+
fully_connected = Flatten()(merged_branches)
62+
fully_connected = Dense(256, activation='relu')(fully_connected)
63+
fully_connected = Dropout(0.5)(fully_connected)
64+
65+
# ok. done
66+
categorization = Dense(2, activation='softmax')(fully_connected)
67+
68+
# all good. Let's build the model, then
69+
self.model = Model(inputs=inputs, outputs=categorization)
4670
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
4771
self.model.compile(loss='categorical_crossentropy', metrics = ['accuracy'], optimizer=sgd)
4872

73+
def summary(self):
74+
return self.model.summary()
75+
4976
def fit(self, x_train, y_train, batch_size, epochs):
5077
self.model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs)
5178

0 commit comments

Comments
 (0)