Difference between Tensorflow convolution and numpy convolution Difference between Tensorflow convolution and numpy convolution numpy numpy

Difference between Tensorflow convolution and numpy convolution


The problem that you see is because TF does not really calculate the convolution. If you will take a look at the explanation of what convolution actually does (check for Visual explanations of convolution), you will see that the second function is flipped:

  1. Express each function in terms of a dummy variable
  2. Reflect one of the functions (this is the flip)
  3. ..... Some other stuff which I will not copy here.

TF does everything except of that flip. So all you need to do is to flip the kernel either in TF or in numpy. Flipping for 1d case is just kernel in a reverse order, for 2d you will need to flip both axis (rotate the kernel 2 times).

import tensorflow as tfimport numpy as npI = [1, 0, 2, 3, 0, 1, 1]K = [2, 1, 3]i = tf.constant(I, dtype=tf.float32, name='i')k = tf.constant(K, dtype=tf.float32, name='k')data   = tf.reshape(i, [1, int(i.shape[0]), 1], name='data')kernel = tf.reshape(k, [int(k.shape[0]), 1, 1], name='kernel')res = tf.squeeze(tf.nn.conv1d(data, kernel, 1, 'VALID'))with tf.Session() as sess:    print sess.run(res)    print np.convolve(I, K[::-1], 'VALID')


The order of filter is reversed. TensorFlow convolution is actually correlation. Numpy gets notation from math, TF gets notation from machine learning papers and somewhere the order got reversed.

This prints True

filter_np2=filter_np[::-1,0,0]np.allclose(np.convolve(X[0,:,0],filter_np2,'SAME'),  Xconv_tf.flatten())    np.convolve(X[0,:,0],filter_np2,'SAME')