3

ネットワークにフィードする 2x16x3x10x10 テンソルがあります。私のネットワークには、並行して機能する 2 つの部分があります。最初の部分は 16x3x10x10 行列を取り、最後の 2 つの次元の合計を計算して、16x3 テンソルを返します。2 番目の部分は、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:forward ではなく model:backward を実行した場合に発生することに気付きました。関連するコードは次のとおりです。

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 です。

4

1 に答える 1

4

これはtorch.nnライブラリの欠陥です。バックワード ステップを実行するには、上位モジュールから受け取ったものをnn.Parallel分割し、それらを並列サブモジュールに送信します。gradOutput分割はメモリをコピーせずに効果的に行われるため、これらの断片は連続していません (1 次元で分割しない限り)。

local first_part = nn.Parallel(1,2)
--                               ^
--                 Merging on the 2nd dimension; 
--       Chunks of splitted gradOutput will not be contiguous

問題は、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
    -- modified code:
    if gradOutput:isContiguous() then
        gradOutput = gradOutput:view(size) -- doesn't work with non-contiguous tensors
    else
        gradOutput = gradOutput:resize(size) -- slower because of memory reallocation and changes gradOutput
        -- gradOutput = gradOutput:clone():resize(size) -- doesn't change gradOutput; safer and even slower
    end
    --
    self.gradInput:resizeAs(input)
    self.gradInput:copy(gradOutput:expandAs(input))
    return self.gradInput
end 

[...]

sums = nn.Sequential()
sums:add(nn.Sum_nc(3)) -- <- will use torch.view
sums:add(nn.Sum_nc(3)) -- <- will use torch.resize
于 2015-08-26T16:07:35.697 に答える