0
$\begingroup$

I'm trying to understand the convolution process better by applying conv2d to different inputs. However I get unexpected result by transforming 3x3 matrix from 1 to 2 channels using two 2x2 filters:

input = tf.constant([1., 2., 3.,
                     4., 3., 2.,
                     1., 2., 4.],
                    shape=[1, 3, 3, 1], dtype=tf.float32)

filters = tf.constant([[1., 2.,
                        3., 1.],
                       [0., 0.,
                        0., 0.]],
                      shape=[2, 2, 1, 2], dtype=tf.float32)

output = tf.nn.conv2d(input, filters, strides=[1, 1, 1, 1], padding="VALID")

with tf.Session() as sess:
    print(sess.run(output))

The result is:

[[[[ 7.  4.]
   [11.  7.]]

  [[13. 11.]
   [ 9.  8.]]]]

What I expected to see is something like the first output feature map to be the result of convolution (1->1 channel), defined by the first filter, and the second feature map to be zero.

Is this dissonance a result of dimensions' meaning misunderstanding or something else?

Thanks!

$\endgroup$
1
$\begingroup$

When you specify filters as

[[1., 2., 3., 1.], [0., 0., 0., 0.]]

(which has a shape of 2, 4)

and then setting the shape as (2, 2, 1, 2), you've implicitly reshaped them as

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

If you transpose the out channel axis to the front, so that you can read off both filters, you get

array([[[1., 3.], [0., 0.]], [[2., 1.], [0., 0.]]], dtype=float32)

so you can see neither of the filters is actually zero.

$\endgroup$
  • $\begingroup$ Ah, right, mixed up the dimensions order! The innermost pairs in my initialization contain numbers for both filters, so tensor had to be [[[1., 0.], [2., 0.]], [[3., 0.], [1., 0.]]]. It's hard to think in n-dimensions, especially if n>3 :) Thank you for helping! $\endgroup$ – Patison Jan 13 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.