Element-Research / rnn

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

LSTM extension using Recurrence and Sequencer #373

Closed hashbangCoder closed 7 years ago

hashbangCoder commented 7 years ago

Hi,

I'm trying to implement an LSTM extension that outputs a sentinel vector alongside the cell state and hidden. I did not want to mess with the existing LSTM.lua structure i.e. forward and backward calls, so I decided to re-implement it using Recurrence (for just a single time-step input). I ran into problems when my recurrent module has two outputs. Consider a simple example :

nIndex = 1000
hiddenSize = 10
rm = nn.Sequential() -- input is {x[t], h[t-1]}
   :add(nn.ParallelTable()
      :add(nn.LookupTable(nIndex, hiddenSize))
      :add(nn.Sequential():add(nn.ParallelTable()
          :add(nn.Linear(hiddenSize, hiddenSize)) -- recurrent layera
          :add(nn.Linear(hiddenSize,hiddenSize)))
          :add(nn.CAddTable())))

inp = {torch.Tensor({100}),{torch.randn(10),torch.randn(10)}}
print(rm:forward(inp))
rn = nn.Recurrence(rm,10,2)
print(rn)

My rn module expects an input {inp,h[t-1]} == {inp,{cell,hidden}} but I cannot do rn:forward(torch.Tensor{12}) since the rm output is a table of length 2. I believe table output is not supported in Recurrence.lua. Here for the first time step, internally output is set to zero vector of 10x1.

For my project, I was considering modifying the nngraph of FastLSTM and passing that to the recurrence module, but this throws a spanner in my plan. Any ideas on how to proceed? Or any re-implementation of LSTM using Recurence and Sequencer? I couldn't find any online