How do I do global average pooling in TensorFlow? If I have a tensor of shape batch_size, height, width, channels = 32, 11, 40, 100
, is it enough to just use tf.layers.average_pooling2d(x, [11, 40], [11, 40])
as long as channels = classes?
You could also do tf.reduce_mean(x, axis=[1,2]), specially if your height and width are not defined.
Typically, in a CNN, tensors have a shape of b, h, w, c
where b
is the batch size, w
and h
correspond to the width and height dimensions, and c
is the number of channels/filters.
When you reduce along the axis [1,2]
, you reduce on the first and second dimensions of the tensor (keeping the batch size, and the number of channels/filters)
You don't need to pass a stride. With padding='valid' (the default), if the spatial extent of the pooling filter is the same as the spatial extent of the image, you will get a 1x1 image out. Other than that, that is how you do it, yes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With