tensorflow Math behind 2D convolution with advanced examples in TF Padding and strides (the most general case)


Now we will apply a strided convolution to our previously described padded example and calculate the convolution where p = 1, s = 2

enter image description here

Previously when we used strides = 1, our slided window moved by 1 position, with strides = s it moves by s positions (you need to calculate s^2 elements less. But in our case we can take a shortcut and do not perform any computations at all. Because we already computed the values for s = 1, in our case we can just grab each second element.

So if the solution is case of s = 1 was

enter image description here

in case of s = 2 it will be:

enter image description here

Check the positions of values 14, 2, 12, 6 in the previous matrix. The only change we need to perform in our code is to change the strides from 1 to 2 for width and height dimension (2-nd, 3-rd).

res = tf.squeeze(tf.nn.conv2d(image, kernel, [1, 2, 2, 1], "SAME"))
with tf.Session() as sess:
   print sess.run(res)

By the way, there is nothing that stops us from using different strides for different dimensions.