正如这篇文章所述,在max-pooling层中使用stride 1不应该减少维度。然而,在进行一些实验时,我观察到了一些不同的东西。
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
可以看到,使用max-pool确实降低了数据的维数。我误解了什么吗?
您忘记添加padding='same'
参数了。默认情况下,填充设置为valid.
,这很棘手,因为只有在进行卷积/其他操作之前才会更改padding
参数。
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