As stated in this post, using stride one in a max-pooling layer should not decrease the dimension. However, while running some experiments, I have observed something different.
my_model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(10,))])
my_model.add(tf.keras.layers.Dense(16,activation=tf.identity))
my_model.add(tf.keras.layers.Reshape([16,1]))
my_model.add(tf.keras.layers.MaxPooling1D(pool_size=(4), strides=(1)))
my_model.add(tf.keras.layers.Flatten())
my_model.add(tf.keras.layers.Dense(16,activation=tf.identity))
my_model.summary()
Model: "sequential_24"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_35 (Flatten) (None, 10) 0
_________________________________________________________________
dense_41 (Dense) (None, 16) 176
_________________________________________________________________
reshape_18 (Reshape) (None, 16, 1) 0
_________________________________________________________________
max_pooling1d_18 (MaxPooling (None, 13, 1) 0
_________________________________________________________________
flatten_36 (Flatten) (None, 13) 0
_________________________________________________________________
dense_42 (Dense) (None, 16) 224
=================================================================
Total params: 400
Trainable params: 400
Non-trainable params: 0
As one can see, using the max-pool did decrease the dimension of the data. Am I misinterpreting something?
CodePudding user response:
You forgot to add the padding='same'
parameter. By default the padding is set to valid.
It is tricky as one would expect to alter the padding
parameter only in case of convolutions/other operations prior to the pooling one.
import tensorflow as tf
my_model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(10,))])
my_model.add(tf.keras.layers.Dense(16,activation=tf.identity))
my_model.add(tf.keras.layers.Reshape([16,1]))
my_model.add(tf.keras.layers.MaxPooling1D(pool_size=(4), strides=(1),padding='same'))
my_model.add(tf.keras.layers.Flatten())
my_model.add(tf.keras.layers.Dense(16,activation=tf.identity))
my_model.summary()
Model: "sequential_4"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten_3 (Flatten) (None, 10) 0
_________________________________________________________________
dense_8 (Dense) (None, 16) 176
_________________________________________________________________
reshape_2 (Reshape) (None, 16, 1) 0
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 16, 1) 0
_________________________________________________________________
flatten_4 (Flatten) (None, 16) 0
_________________________________________________________________
dense_9 (Dense) (None, 16) 272
=================================================================
Total params: 448
Trainable params: 448
Non-trainable params: 0