【问题标题】:Why are the convolution outputs calculated with theano and numpy not the same?为什么用theano和numpy计算的卷积输出不一样?
【发布时间】:2015-09-18 18:30:56
【问题描述】:

我做了一个simple example ipython notebook to calculate convolution with theano and with numpy,但是结果不同。有谁知道错在哪里?

import theano
import numpy
from theano.sandbox.cuda import dnn
import theano.tensor as T

定义输入图像x0:

x0 = numpy.array([[[[  7.61323881,   0.        ,   0.        ,   0.        ,
            0.        ,   0.        ],
         [ 25.58142853,   0.        ,   0.        ,   0.        ,
            0.        ,   0.        ],
         [  7.51445341,   0.        ,   0.        ,   0.        ,
            0.        ,   0.        ],
         [  0.        ,  12.74498367,   4.96315479,   0.        ,
            0.        ,   0.        ],
         [  0.        ,   0.        ,   0.        ,   0.        ,
            0.        ,   0.        ],
         [  0.        ,   0.        ,   0.        ,   0.        ,
            0.        ,   0.        ]]]], dtype='float32')

x0.shape
# (1, 1, 6, 6)

定义卷积核:

w0 = numpy.array([[[[-0.0015835 , -0.00088091,  0.00226375,  0.00378434,  0.00032208,
          -0.00396959],
         [-0.000179  ,  0.00030951,  0.00113849,  0.00012536, -0.00017198,
          -0.00318825],
         [-0.00263921, -0.00383847, -0.00225416, -0.00250589, -0.00149073,
          -0.00287099],
         [-0.00149283, -0.00312137, -0.00431571, -0.00394508, -0.00165113,
          -0.0012118 ],
         [-0.00167376, -0.00169753, -0.00373235, -0.00337372, -0.00025546,
           0.00072154],
         [-0.00141197, -0.00099017, -0.00091934, -0.00226817, -0.0024105 ,
          -0.00333713]]]], dtype='float32')

w0.shape
# (1, 1, 6, 6)

用theano和cudnn计算卷积:

X = T.tensor4('input')
W = T.tensor4('W')
conv_out = dnn.dnn_conv(img=X, kerns=W)
convolution = theano.function([X, W], conv_out)
numpy.array(convolution(x0, w0))
# array([[[[-0.04749081]]]], dtype=float32)

用numpy计算卷积(注意结果不同):

numpy.sum(x0 * w0)
# -0.097668208

【问题讨论】:

标签: numpy cuda theano convolution


【解决方案1】:

我不确定您要计算哪种卷积,但在我看来numpy.sum(x0*w0) 可能不是这样做的方法。这有帮助吗?

import numpy as np
# ... define x0 and w0 like in your example ...
np_convolution = np.fft.irfftn(np.fft.rfftn(x0) * np.fft.rfftn(w0))

结果数组的最后一个元素,即np_convolution[-1,-1,-1,-1]-0.047490807560833327,这似乎是您在笔记本中寻找的答案。

【讨论】:

  • 感谢您指出numpy.sum(x0*w0) 的问题。事实上,这不是卷积,而是互相关。正确的计算方法是将内核翻转为numpy.sum(x0*w0[:,:, ::-1, ::-1])。这将给出相同的结果。有一个相关的问题:perform the exact same convolution as in theano's conv2d
  • numpy.sum(x0*w0) 实际上只是一个数字 - 两个信号之间的点积,对应于在 0 处评估的互相关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 2019-05-03
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
  • 2013-01-13
相关资源
最近更新 更多