tensorflow conv2d diffrent start index between eve

2020-05-03 10:34发布

问题:

To my understanding from tf.nn.conv2d doc for SAME convolution (no matter the stride) The first dot product should be centered around (0,0) though as you can see bellow when the stride is odd the first dot product seems to be centered around (1,1): in this toy example

input shape is [5,5,1]

filer shape is [3,3,1,1]

res = tf.nn.conv2d(X, F, strides=[1,x,x,1], padding='SAME')

stride 1 result:

array([[ 1.49573362,  2.65084887,  2.96818447,  3.04787111,  1.89275599],
   [ 3.1941781 ,  4.47312069,  4.10260868,  4.13415051,  2.85520792],
   [ 2.65490007,  3.41439581,  2.93415952,  3.65811515,  2.89861989],
   [ 2.22547054,  2.98453856,  2.89428496,  3.29111433,  2.53204632],
   [ 0.52702606,  1.16226625,  1.75986075,  2.20483446,  1.56959426]], dtype=float32)

stride 2 result:

array([[ 1.49573362,  2.96818447,  1.89275599],
   [ 2.65490007,  2.93415952,  2.89861989],
   [ 0.52702606,  1.75986075,  1.56959426]], dtype=float32)

stride 3 result:

array([[ 4.47312069,  2.85520792],
   [ 1.16226625,  1.56959426]], dtype=float32)

Is this a bug or am I missing something?

回答1:

What is happening is that tensorflow will add the columns at the end if the number of extra zero columns (from padding) are odd.

In your example with stride = 1 it needs to add two columns, so it adds a column at the beginning and one at the end (meaning beginning, end of each side: left, right, top, bottom). Stride = 2 will do the same.

However, for stride = 3 it just needs to add one column and it does it at the end (right and bottom). If it needed to add 5 columns it will add 2 at the beginning (left, top) and 3 at the end (right, bottom)