0

I'm having a lot of problems to understand numpy array shape.

If have a numpy array with shape (1, 12, 12, 512), does it means?

  1. I have 512 arrays of shape (1, 12, 12).
  2. I have one array of shape (12, 12, 512)

What does it means?

By the way, this numpy array is a feature map from an encoder CNN model.

If I use tf.keras.layers.GlobalAveragePooling2D with that feature map, I get a Tensor with shape (1, 512) that is what it makes me doubt.

To show a simplest array and try to understand it, I have used this code:

x = tf.constant([[1., 2., 3.],
                 [4., 5., 6.]])
y = tf.reshape(x, [1, 2, 3, 1])

x array is:

array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)

y array is:

array([[[[1.],
         [2.],
         [3.]],

        [[4.],
         [5.],
         [6.]]]], dtype=float32)

So I think is the second option, but I'm not sure.

1 Answer 1

2

Both those two statements are correct. However, ultimately it comes down to what your data object represents. If the first axis is the batch, it's more likely to be 1 batch containing 512 12x12-feature maps.


Edit: Which is the case, since you mention Tensorflow. The library works this with format: (batch, height, width, channels). Where the channels are last, contrary to how PyTorch operates.

tf.keras.layers.GlobalAveragePooling2D will, for each batch, average each one of your 12x12 feature maps into a single point. Since you have 512 feature maps, this will leave you with a shape of (1, 512). "Global" means it will average the whole feature map.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you are right, because the output_channels for the last Conv2D layer is 512, so I have 512 feature maps of size 12x12 but, if you check the code that I have added to my question, it seems that the second option is the right one. Maybe because I don't understand every thing completely. Thanks a lot for your answer.
In your example, y is essentially a single batch of 1 2x3 feature map, and in this case would be the correct format. Glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.