Element-Research / rnn

Recurrent Neural Network library for Torch7's nn
BSD 3-Clause "New" or "Revised" License
941 stars 313 forks source link

SeqLSTM : Only supports batch mode error #302

Closed avs20 closed 8 years ago

avs20 commented 8 years ago

Hi all,

I have 128x14x14 input and I am trying to feed it into the 4 parallel seqLSTM modules for processing.

The code is like this

 rnns = nn.Parallel(1,1)
 seqLSTM = nn.SeqLSTM(196,196)
 seqLSTM.batchfirst=true
 rnns:add(seqLSTM)
 rnns:add(seqLSTM)
 rnns:add(seqLSTM)
 rnns:add(seqLSTM)

I understand that the 196 passed above is only the input and the output size. It was throwing error when I passed it 4x128x196 the error message came to be

done    
/home/ashutosh/torch/install/share/lua/5.1/nn/Container.lua:67: 
In 1 module of nn.Sequential:
In 1 module of nn.Sequential:
In 1 module of nn.Parallel:
/home/ashutosh/torch/install/share/lua/5.1/rnn/SeqLSTM.lua:134: Only supports batch mode
stack traceback:
    [C]: in function 'assert'
    /home/ashutosh/torch/install/share/lua/5.1/rnn/SeqLSTM.lua:134: in function '_prepare_size'
    /home/ashutosh/torch/install/share/lua/5.1/rnn/SeqLSTM.lua:169: in function </home/ashutosh/torch/install/share/lua/5.1/rnn/SeqLSTM.lua:167>
    [C]: in function 'xpcall'
    /home/ashutosh/torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors'
    /home/ashutosh/torch/install/share/lua/5.1/nn/Parallel.lua:18: in function </home/ashutosh/torch/install/share/lua/5.1/nn/Parallel.lua:10>
    [C]: in function 'xpcall'
    /home/ashutosh/torch/install/share/lua/5.1/nn/Container.lua:63: in function 'rethrowErrors'
    ...e/ashutosh/torch/install/share/lua/5.1/nn/Sequential.lua:44: in function <...e/ashutosh/torch/install/share/lua/5.1/nn/Sequential.lua:41>
    [C]: in function 'xpcall'
    ...
    [C]: in function 'xpcall'
    /home/ashutosh/torch/install/share/lua/5.1/itorch/main.lua:209: in function </home/ashutosh/torch/install/share/lua/5.1/itorch/main.lua:173>
    /home/ashutosh/torch/install/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
    .../ashutosh/torch/install/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
    .../ashutosh/torch/install/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
    .../ashutosh/torch/install/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
    /home/ashutosh/torch/install/share/lua/5.1/itorch/main.lua:381: in main chunk
    [C]: in function 'require'
    (command line):1: in main chunk
    [C]: at 0x00406670

So how to pass the data in batch mode to seqLSTM or am I doing something fundamentally wrong?

avs20 commented 8 years ago

I solved the problem. It was my fault of less knowledge and not trying enough. I passed my data in the form of 4x1x128x196 where each of 4 till go in to the parallel and then 1 is the seq length, 128 the batch size 196 the input size.

avs20 commented 8 years ago

I am still at the problem now.

I declare the modules as

seqLSTM = nn.SeqLSTM(128,128)

but when I whenever I Pass a tensor of size 128. It throws error of

Only Support batch mode

Then I try to pass the input by reshaping it with nn.Reshape as

RNNModel = nn.Sequential()
RNNModel:add(nn.Reshape(1,1,128))
seqLSTM = nn.SeqLSTM(128,128)

but it is still throwing the same error of only supports batch mode.

The full code to reproduce it is

mData = torch.randn(2,128)
print(mData:size())

require 'rnn'
require 'torch'

mData = torch.randn(2,128)
print(mData:size())

RNNModel = nn.Sequential()
RNNModel:add(nn.Reshape(2,1,128))

rnns = nn.Parallel(1,1)
seqLSTM = nn.SeqLSTM(128,128)
seqLSTM.batchfirst=true
rnns:add(seqLSTM)
rnns:add(seqLSTM)

RNNModel:add(rnns)

print("RNN model done")

mdl = nn.Sequential()
mdl:add(RNNModel)

y = mdl:forward(mData)
print(y:size())

thanks

JoostvDoorn commented 8 years ago

Parallel transforms the input to 1x128 instead of 1x1x128 a simple solution would be to input 2x1x1x128.

nicholas-leonard commented 8 years ago

@JoostvDoorn This fixes it : RNNModel:add(nn.Reshape(2,1,1,128)). The issue is that, with the former RNNModel:add(nn.Reshape(2,1,128)) the output of the Parallel is two tensors of size 1 x 28.

avs20 commented 8 years ago

@JoostvDoorn @nicholas-leonard Yeah it works now. Thanks. I should have caught it myself.