当输入和conv内核具有相同的大小时,theano conv3d2d在gpu上给出错误的结果

时间:2016-09-24 08:40:30

标签: python theano

当我在GPU上使用theano进行3D卷积计算时,我遇到了一些问题。

我的发展环境是:

theano 0.8.2

python 2.7.12

cuda 7.5没有cuDNN

当我使用theano.tensor.nnet.conv3d2d.conv3d在GPU(Nvidia Geforce GTX970)上进行3D卷积时,我得到了错误的计算结果。   我的脚本如下:

import numpy as np
import theano
T = theano.tensor
floatX = theano.config.floatX
import theano.tensor.nnet.conv3d2d

batchsize = 1
in_channels = 1
in_time = 3
in_width = 2
in_height = 2
flt_channels = 1
flt_time = 3
flt_width = 2
flt_height = 2
input_shape = (batchsize, in_time, in_channels, in_height, in_width)
x = np.arange(np.prod(input_shape)).astype(np.float32)
x = np.reshape(x,input_shape)
inputs = theano.shared(x, borrow = True, name='inputs')
for i in range(3):
    for j in range(2):
        for k in range(2):
            flt_shape = (flt_channels, flt_time, in_channels, flt_height, flt_width)
            W = np.zeros(flt_shape,dtype=np.float32)
            W[0, i, 0, j, k] = 1
            filters = theano.shared(W, borrow=True, name='filters')
            bias =    theano.shared(np.zeros(flt_channels,np.float32),name='bias')
            convA = T.nnet.conv3d2d.conv3d(
            signals=inputs,  # Ns, Ts, C, Hs, Ws
            filters=filters, # Nf, Tf, C, Hf, Wf
            signals_shape=input_shape,
            filters_shape=flt_shape,
            border_mode='valid')
        convA = convA + bias.dimshuffle('x','x',0,'x','x')
        funcA = theano.function([], convA)
        A = funcA()
        print "[", i, j, k, "] is 1, other is 0.", "    A = ", A

在GPU上计算后,我得到的结果是

[ 0 0 0 ] is 1, other is 0.     A =  [[[[[ 11.]]]]]
[ 0 0 1 ] is 1, other is 0.     A =  [[[[[ 10.]]]]]
[ 0 1 0 ] is 1, other is 0.     A =  [[[[[ 9.]]]]]
[ 0 1 1 ] is 1, other is 0.     A =  [[[[[ 8.]]]]]
[ 1 0 0 ] is 1, other is 0.     A =  [[[[[ 11.]]]]]
[ 1 0 1 ] is 1, other is 0.     A =  [[[[[ 10.]]]]]
[ 1 1 0 ] is 1, other is 0.     A =  [[[[[ 9.]]]]]
[ 1 1 1 ] is 1, other is 0.     A =  [[[[[ 8.]]]]]
[ 2 0 0 ] is 1, other is 0.     A =  [[[[[ 11.]]]]]
[ 2 0 1 ] is 1, other is 0.     A =  [[[[[ 10.]]]]]
[ 2 1 0 ] is 1, other is 0.     A =  [[[[[ 9.]]]]]
[ 2 1 1 ] is 1, other is 0.     A =  [[[[[ 8.]]]]]

但是当我在CPU上运行SAME脚本时,结果返回是正确的。

[ 0 0 0 ] is 1, other is 0.     A =  [[[[[ 11.]]]]]
[ 0 0 1 ] is 1, other is 0.     A =  [[[[[ 10.]]]]]
[ 0 1 0 ] is 1, other is 0.     A =  [[[[[ 9.]]]]]
[ 0 1 1 ] is 1, other is 0.     A =  [[[[[ 8.]]]]]
[ 1 0 0 ] is 1, other is 0.     A =  [[[[[ 7.]]]]]
[ 1 0 1 ] is 1, other is 0.     A =  [[[[[ 6.]]]]]
[ 1 1 0 ] is 1, other is 0.     A =  [[[[[ 5.]]]]]
[ 1 1 1 ] is 1, other is 0.     A =  [[[[[ 4.]]]]]
[ 2 0 0 ] is 1, other is 0.     A =  [[[[[ 3.]]]]]
[ 2 0 1 ] is 1, other is 0.     A =  [[[[[ 2.]]]]]
[ 2 1 0 ] is 1, other is 0.     A =  [[[[[ 1.]]]]]
[ 2 1 1 ] is 1, other is 0.     A =  [[[[[ 0.]]]]]

我对我得到的不同结果感到困惑。这就是theano的错误吗?

为什么GPU下的计算结果是错误的?

如果有人在这里帮助我,我真的很感激。

此致

0 个答案:

没有答案