Reputation: 115
I am working with sequences of vectors as input data to a NN in tensorflow and I would like to perform an average pooling over the depth of the input.
I tried to use the following lambda layer:
depth_pool = keras.layers.Lambda(
lambda X: tf.nn.avg_pool1d(X,
ksize=(1, 1, 3),
strides=(1, 1, 3),
padding="VALID"))
However, I receive the error message:
UnimplementedError: Non-spatial pooling is not yet supported.
Is there a way to achieve the desired result?
Thank you very much for your help
Upvotes: 1
Views: 142
Reputation: 22031
If your input has these dimensionalities: (None, timestamps, features)
you can simply permute the depth with other dimensionalities, apply the standard pooling and then permute back to the original dimensionalities.
To give an example... If your network accepts input whit shape (None, 20, 99)
you can simply do what follows to obtain a depth pooling:
inp = Input((20,99))
depth_pool = Permute((2,1))(inp)
depth_pool = AveragePooling1D(3)(depth_pool)
depth_pool = Permute((2,1))(depth_pool)
m = Model(inp, depth_pool)
m.summary()
The summary:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 20, 99)] 0
_________________________________________________________________
permute_4 (Permute) (None, 99, 20) 0
_________________________________________________________________
average_pooling1d_3 (Average (None, 33, 20) 0
_________________________________________________________________
permute_5 (Permute) (None, 20, 33) 0
=================================================================
The output has shape (None, 20, 33)
If your input has these dimensionalities: (None, features, timestamps)
you can simply set data_format='channels_first'
in your layers
inp = Input((20,99))
depth_pool = AveragePooling1D(3, data_format='channels_first')(inp)
m = Model(inp, depth_pool)
m.summary()
The summary:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_9 (InputLayer) [(None, 20, 99)] 0
_________________________________________________________________
average_pooling1d_7 (Average (None, 20, 33) 0
=================================================================
The output has shape (None, 20, 33)
Upvotes: 2