local lookup = nn.LookupTable(opt.vocab_size, opt.vec_size)
if opt.model_type == 'static' or opt.model_type == 'nonstatic' then
lookup.weight:copy(w2v)
else
-- rand
lookup.weight:uniform(-0.25, 0.25)
end
-- padding should always be 0
lookup.weight[1]:zero()
-- build simple recurrent neural network
local r
if opt.lstm == 0 then
r = nn.Recurrent(
opt.hidden_size, nn.Identity(),
nn.Linear(opt.vec_size, opt.hidden_size), nn.Sigmoid(),
max_seq_len
)
else
require 'nngraph'
nn.FastLSTM.usenngraph = true -- faster
nn.FastLSTM.bn = opt.bn -- batch normolization
r = nn.FastLSTM(opt.vec_size, opt.hidden_size)
end
rnn = nn.Sequential()
:add(lookup)
:add(nn.SplitTable(1, 2)) -- batch * seq * vec
:add(nn.Sequencer(r)) -- seq * batch * vec
:add(nn.SelectTable(-1)) -- this selects the last time-step of the rnn output sequence
:add(nn.Linear(opt.hidden_size, opt.num_classes))
:add(cudnn.LogSoftMax()) -- opt.cudnn = 1
The code of my model is as followed:
local lookup = nn.LookupTable(opt.vocab_size, opt.vec_size) if opt.model_type == 'static' or opt.model_type == 'nonstatic' then lookup.weight:copy(w2v) else -- rand lookup.weight:uniform(-0.25, 0.25) end -- padding should always be 0 lookup.weight[1]:zero()
-- build simple recurrent neural network local r if opt.lstm == 0 then r = nn.Recurrent( opt.hidden_size, nn.Identity(), nn.Linear(opt.vec_size, opt.hidden_size), nn.Sigmoid(),
max_seq_len ) else require 'nngraph' nn.FastLSTM.usenngraph = true -- faster nn.FastLSTM.bn = opt.bn -- batch normolization r = nn.FastLSTM(opt.vec_size, opt.hidden_size) end
rnn = nn.Sequential() :add(lookup) :add(nn.SplitTable(1, 2)) -- batch * seq * vec :add(nn.Sequencer(r)) -- seq * batch * vec :add(nn.SelectTable(-1)) -- this selects the last time-step of the rnn output sequence :add(nn.Linear(opt.hidden_size, opt.num_classes)) :add(cudnn.LogSoftMax()) -- opt.cudnn = 1