0
votes

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
have you tried making a toy example with values that you can actually calculate? 'As you can see below ...' I do not know about you, but I can't see anything: lots of random numbers. - Salvador Dali
yes i did but all you have to do is compare the output with stride vs output of stride 1. since the results with stride is a sub-sampled version of the stride 1 result. for example you can see the (0,0) result with stride 3 is the (1,1) result with stride 1. - Hillel

1 Answers

1
votes

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)