how to convert pytorch adaptive_avg_pool2d method to keras or tensorflow

7.3k Views Asked by At

I don't know how to convert the PyTorch method adaptive_avg_pool2d to Keras or TensorFlow. Anyone can help? PyTorch mehod is

adaptive_avg_pool2d(14,[14])

I tried to use the average pooling, the reshape the tensor in Keras, but got the error:

ValueError: total size of new array must be unchanged

1

There are 1 best solutions below

6
Fábio Perez On

I'm not sure if I understood your question, but in PyTorch, you pass the spatial dimensions to AdaptiveAvgPool2d. For instance, if you want to have an output sized 5x7, you can use nn.AdaptiveAvgPool2d((5,7)).

If you want a global average pooling layer, you can use nn.AdaptiveAvgPool2d(1). In Keras you can just use GlobalAveragePooling2D.

For other output sizes in Keras, you need to use AveragePooling2D, but you can't specify the output shape directly. You need to calculate/define the pool_size, stride, and padding parameters depending on how you want the output shape. If you need help with the calculations, check this page of CS231n course.