简体   繁体   English

为什么用theano和numpy计算的卷积输出不同?

[英]Why are the convolution outputs calculated with theano and numpy not the same?

I made a simple example ipython notebook to calculate convolution with theano and with numpy , however the results are different. 我制作了一个简单的示例ipython Notebook以计算theano和numpy的卷积 ,但是结果不同。 Does anybody know where is the mistake? 有人知道错误在哪里吗?

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

Define the input image x0: 定义输入图像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)

Define the convolution kernel: 定义卷积内核:

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)

Calculate the convolution with theano and cudnn: 用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)

Calculate convolution with numpy (note the result is different): 用numpy计算卷积(注意结果是不同的):

numpy.sum(x0 * w0)
# -0.097668208

I'm not exactly sure what kind of convolution you are trying to compute, but it seems to me that numpy.sum(x0*w0) might not be the way to do it. 我不确定要尝试计算哪种卷积,但在我看来numpy.sum(x0*w0)可能不是这样做的方法。 Does this help? 这有帮助吗?

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))

The last element of the resulting array, ie np_convolution[-1,-1,-1,-1] is -0.047490807560833327 , which seems to be the answer you're looking for in your notebook. 结果数组的最后一个元素,即np_convolution[-1,-1,-1,-1]-0.047490807560833327 ,这似乎是您在笔记本中寻找的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM