预期得到一个连续的张量错误,nn.Sum。

我有一个 2x16x3x10x10 的张量,我将其馈送到网络中。我的网络有两个同时工作的部分。第一部分接受 16x3x10x10 矩阵,并计算最后两个维度上的总和,返回 16x3 张量。第二部分是一个卷积神经网络,产生一个 16x160 张量。每当我尝试运行这个模型时,我会得到以下错误:

...903/nTorch/Torch7/install/share/lua/5.1/torch/Tensor.lua:457: expecting a contiguous tensor
stack traceback:
[C]: in function 'assert'
...903/nTorch/Torch7/install/share/lua/5.1/torch/Tensor.lua:457: in function 'view'
...8/osu7903/nTorch/Torch7/install/share/lua/5.1/nn/Sum.lua:26: in function 'updateGradInput'
...03/nTorch/Torch7/install/share/lua/5.1/nn/Sequential.lua:40: in function 'updateGradInput'
...7903/nTorch/Torch7/install/share/lua/5.1/nn/Parallel.lua:52: in function 'updateGradInput'
...su7903/nTorch/Torch7/install/share/lua/5.1/nn/Module.lua:30: in function 'backward'
...03/nTorch/Torch7/install/share/lua/5.1/nn/Sequential.lua:73: in function 'backward'
./train_v2_with_batch.lua:144: in function 'opfunc'
...su7903/nTorch/Torch7/install/share/lua/5.1/optim/sgd.lua:43: in function 'sgd'
./train_v2_with_batch.lua:160: in function 'train'
run.lua:93: in main chunk
[C]: in function 'dofile'
...rch/Torch7/install/lib/luarocks/rocks/trepl/scm-1/bin/th:131: in main chunk
[C]: at 0x00405800

这是模型的相关部分:

local first_part = nn.Parallel(1,2)
local CNN = nn.Sequential()

local sums = nn.Sequential()
sums:add(nn.Sum(3))
sums:add(nn.Sum(3))
first_part:add(sums)

-- stage 1: conv+max
CNN:add(nn.SpatialConvolutionMM(nfeats, convDepth_L1,receptiveFieldWidth_L1,receptiveFieldHeight_L1))
-- Since the default stride of the receptive field is 1, then
-- (assuming receptiveFieldWidth_L1 = receptiveFieldHeight_L1 = 3)  the number of receptive fields is (10-3+1)x(10-3+1) or 8x8
-- so the output volume is (convDepth_L1 X 8 X 8) or 10 x 8 x 8

--CNN:add(nn.Threshold())
CNN:add(nn.ReLU())
CNN:add(nn.SpatialMaxPooling(poolsize,poolsize,poolsize,poolsize))
-- if poolsize=2, then the output of this is 10x4x4

CNN:add(nn.Reshape(convDepth_L1*outputWdith_L2*outputWdith_L2,true))
first_part:add(CNN)

当输入张量为 2x1x3x10x10 时,代码可以工作,但当张量为 2x16x3x10x10 时,却不能正常工作。

编辑:我才意识到这是在执行 model:backward 而不是 model:forward 时发生的。这是相关的代码:

local y = model:forward(x)
local E = loss:forward(y,yt)

-- estimate df/dW
local dE_dy = loss:backward(y,yt)
print(dE_dy)
model:backward(x,dE_dy)

其中,x 是 2x16x3x10x10 的张量,dE_dy 是 16x2。

点赞
用户4850610
用户4850610

这是 torch.nn 库中的一个缺陷。要执行反向传播步骤,nn.Parallel 将它从更高的模块接收到的 gradOutput 分成几个部分并发送到其并行子模块。分割是有效的,不需要复制内存,因此这些部分是不连续的(除非你在第一维上进行分割)。

local first_part = nn.Parallel(1,2)
--                               ^
--                 在第2个维度上合并;
--       分割后的 gradOutput 块将不连续

问题是 nn.Sum 不能处理非连续的 gradOutput。我没有比对其进行更改更好的主意:

Sum_nc, _ = torch.class('nn.Sum_nc', 'nn.Sum')
function Sum_nc:updateGradInput(input, gradOutput)
    local size = input:size()
    size[self.dimension] = 1
    -- 修改的代码:
    if gradOutput:isContiguous() then
        gradOutput = gradOutput:view(size) -- 与非连续张量不兼容
    else
        gradOutput = gradOutput:resize(size) -- 慢,因为需要重新分配内存和更改 gradOutput
        -- gradOutput = gradOutput:clone():resize(size) -- 不更改 gradOutput;更安全但更慢
    end
    --
    self.gradInput:resizeAs(input)
    self.gradInput:copy(gradOutput:expandAs(input))
    return self.gradInput
end

[...]

sums = nn.Sequential()
sums:add(nn.Sum_nc(3)) -- <- 将使用 torch.view
sums:add(nn.Sum_nc(3)) -- <- 将使用 torch.resize
2015-08-26 16:07:35