0

I am new to Deep Learning, I think I've got the point of this Understanding NumPy's Convolve .

I tried this in numpy

np.convolve([3, 4], [1, 1, 5, 5], 'valid')

the output is

array([ 7, 19, 35])

According to the link the second element of the output should be 23.

  [3 4]
[1 1 5 5]
= 3 * 1 + 4 * 5 = 23

It seems that the second element (19) is wrong in my case, though I have no idea how and why. Any responses will be grateful.

2 Answers2

0

I think you are confused with convolution implementation in neural networks, which is actually cross-corellation. However if you refer to mathematical definition of the convolution, you will see that the the second function has to be time-reversed (or mirrored). Also, note that numpy swaps araguments if the second element has bigger size (as in your case). So the result your get is obtained as following:

[1*4+3*1,1*4+3*5,5*4+3*5]

In case you want numpy to perform calculations as you did, you should use:

np.correlate([3, 4], [1, 1, 5, 5], 'valid')

Here is useful illustration for convolution and cross-correlation:

enter image description here

asakryukin
  • 2,524
  • 1
  • 13
  • 14
0

the reason is that numpy reverse the shorter array, here [3, 4] becomes [4,3]. It is done because of the definition of the convolution (you can find more informations in the section definition of wikipedia here https://en.wikipedia.org/wiki/Convolution).

So in fact : np.convolve([3, 4], [1, 1, 5, 5], 'valid') makes :

[4 3]

[1 1 5 5]

= 4 * 1 + 3 * 5 = 19

:)