Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do global average pooling in TensorFlow?

Tags:

tensorflow

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?

like image 530
Carl Thomé Avatar asked Feb 05 '17 16:02

Carl Thomé


2 Answers

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)

like image 189
rafaelvalle Avatar answered Sep 28 '22 04:09

rafaelvalle


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.

like image 24
etarion Avatar answered Sep 28 '22 05:09

etarion