0

I am writing a CNN for text classification. The max pooling2D layer seems does not work as the output shape is same as conv2D. I have attached my code and output shape below. Thanks for helping me!

    from keras.layers import Dense, Input, Flatten
    from keras.layers import Conv2D, MaxPooling2D, Embedding, Reshape, Concatenate, Dropout
    from keras import optimizers
    from keras.models import Model

    convs = []
    filter_sizes = [2,4,8]
    BATCH_SIZE = 10

    sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
    embedded_sequences = embedding_layer(sequence_input)
    reshape = Reshape((MAX_SEQUENCE_LENGTH, EMBEDDING_DIM,1))(embedded_sequences)

    conv_0 = Conv2D(filters = 128, kernel_size=(MAX_SEQUENCE_LENGTH, filter_sizes[0]), activation='relu')(reshape)
    conv_1 = Conv2D(filters = 128, kernel_size=(MAX_SEQUENCE_LENGTH, filter_sizes[1]), activation='relu')(reshape)
    conv_2 = Conv2D(filters = 128, kernel_size=(MAX_SEQUENCE_LENGTH, filter_sizes[2]), activation='relu')(reshape)

    maxpool_0 = MaxPooling2D(pool_size=(MAX_SEQUENCE_LENGTH - filter_sizes[0] + 1,1), strides=(1,1), padding='same')(conv_0)
    maxpool_1 = MaxPooling2D(pool_size=(MAX_SEQUENCE_LENGTH - filter_sizes[1] + 1, 1), strides=(1,1), padding='same')(conv_1)
    maxpool_2 = MaxPooling2D(pool_size=(MAX_SEQUENCE_LENGTH - filter_sizes[2] + 1, 1), strides=(1,1), padding='same')(conv_2)

    concatenated_tensor = Concatenate(axis = 2)([maxpool_0, maxpool_1, maxpool_2])
    flatten = Flatten()(concatenated_tensor)
    dense = Dense(2048, activation='relu')(flatten)
    dense_out = Dropout(0.5)(dense)
    preds = Dense(label_dim, activation='sigmoid')(dense_out)

    model = Model(sequence_input, preds)
    opt = optimizers.Adam(lr=0.0001)
    model.compile(loss='binary_crossentropy',
          optimizer=opt,
          metrics=['acc'])

output shape for each layer

Cindy
  • 43
  • 1
  • 7
  • Why are you putting padding=same in your MaxPooling layers? – Dr. Snoopy Sep 09 '18 at 06:32
  • @ Matias Valdenegro Because I want the outputs for three different filters size to be the same. – Cindy Sep 09 '18 at 07:09
  • I don't understand then, you say pooling doesn't work because shapes are the same, but you forced this with the use of padding. – Dr. Snoopy Sep 09 '18 at 07:10
  • @ Matias Valdenegro I made a mistake on stride, it should be the same as kernel_size in pooling. If you can see from the output shape, I got (None, 1, 199, 128) for first filter size after conv2D, I should got (None, 1, 1, 128) for first filter size after maxpooling2D, however, it returns (None, 1, 199, 128), which is exactly the same as conv2D. I am not sure what happened, why max pooling does not work. Thanks for your help! – Cindy Sep 09 '18 at 07:34
  • 1
    MaxPooling is working as intended, the problem is your understanding of what the parameters do. I wouldn't use padding=same with pooling as the whole purpose of using pooling is to reduce dimensions (the shapes). – Dr. Snoopy Sep 09 '18 at 11:21
  • I don't quite understand. The parameter padding is just whether or not add paddings while pooling. In my situation, no matter what type of padding I selected, it should return (None, 1, 1, 128) after pooling. But the program returns (None, 1, 199, 128), which is wired. By the way, I tried padding = same and padding = valid, both of them does not work. – Cindy Sep 09 '18 at 22:58
  • Seems what you want is GlobalMaxPooling2D, that one will collapse spatial dims to one. – Dr. Snoopy Sep 09 '18 at 23:00

0 Answers0