diff options
author | Qi Liu <[email protected]> | 2016-05-12 17:41:21 +0800 |
---|---|---|
committer | Qi Liu <[email protected]> | 2016-05-12 17:41:21 +0800 |
commit | c0fdb7ee2966546023410bb03e62dee0cf64e0e1 (patch) | |
tree | cd90688b8aee2991a78f971e1bc4e0e9596e357b /nerv/layer/lstm.lua | |
parent | d88a57f4852c50a2678de950ee650ed9b6a895f0 (diff) | |
parent | 10916f721a945a5edd052ab93027413fd3c01f65 (diff) |
Merge branch 'master' into 'master'
beta-1.2
multiple change
1. Merge affine & lstm_gate & projection layer
2. Change clip behavior
3. Seq_buffer support sequence level shuffle
4. LSTM & LSTMP layer support multiple input
See merge request !4
Diffstat (limited to 'nerv/layer/lstm.lua')
-rw-r--r-- | nerv/layer/lstm.lua | 55 |
1 files changed, 34 insertions, 21 deletions
diff --git a/nerv/layer/lstm.lua b/nerv/layer/lstm.lua index 3de3453..5d73ad2 100644 --- a/nerv/layer/lstm.lua +++ b/nerv/layer/lstm.lua @@ -2,9 +2,12 @@ local LSTMLayer = nerv.class('nerv.LSTMLayer', 'nerv.GraphLayer') function LSTMLayer:__init(id, global_conf, layer_conf) nerv.Layer.__init(self, id, global_conf, layer_conf) - self:check_dim_len(1, 1) + self:check_dim_len(-1, 1) + if #self.dim_in == 0 then + nerv.error('LSTM layer %s has no input', self.id) + end - local din = layer_conf.dim_in[1] + local din = layer_conf.dim_in local dout = layer_conf.dim_out[1] local pr = layer_conf.pr @@ -17,48 +20,51 @@ function LSTMLayer:__init(id, global_conf, layer_conf) mainCombine = {dim_in = {dout, dout}, dim_out = {dout}, lambda = {1, 1}}, }, ['nerv.DuplicateLayer'] = { - inputDup = {dim_in = {din}, dim_out = {din, din, din, din}}, outputDup = {dim_in = {dout}, dim_out = {dout, dout, dout, dout, dout}}, cellDup = {dim_in = {dout}, dim_out = {dout, dout, dout, dout, dout}}, }, ['nerv.AffineLayer'] = { - mainAffine = {dim_in = {din, dout}, dim_out = {dout}, pr = pr}, + mainAffine = {dim_in = table.connect({dout}, din), dim_out = {dout}, pr = pr}, + forgetGate = {dim_in = table.connect({dout, dout}, din), dim_out = {dout}, + param_type = table.connect({'N', 'D'}, table.vector(#din, 'N')), pr = pr, activation = nerv.SigmoidLayer}, + inputGate = {dim_in = table.connect({dout, dout}, din), dim_out = {dout}, + param_type = table.connect({'N', 'D'}, table.vector(#din, 'N')), pr = pr, activation = nerv.SigmoidLayer}, + outputGate = {dim_in = table.connect({dout, dout}, din), dim_out = {dout}, + param_type = table.connect({'N', 'D'}, table.vector(#din, 'N')), pr = pr, activation = nerv.SigmoidLayer}, }, ['nerv.TanhLayer'] = { mainTanh = {dim_in = {dout}, dim_out = {dout}}, outputTanh = {dim_in = {dout}, dim_out = {dout}}, }, - ['nerv.LSTMGateLayer'] = { - forgetGate = {dim_in = {din, dout, dout}, dim_out = {dout}, param_type = {'N', 'N', 'D'}, pr = pr}, - inputGate = {dim_in = {din, dout, dout}, dim_out = {dout}, param_type = {'N', 'N', 'D'}, pr = pr}, - outputGate = {dim_in = {din, dout, dout}, dim_out = {dout}, param_type = {'N', 'N', 'D'}, pr = pr}, - }, ['nerv.ElemMulLayer'] = { inputGateMul = {dim_in = {dout, dout}, dim_out = {dout}}, forgetGateMul = {dim_in = {dout, dout}, dim_out = {dout}}, outputGateMul = {dim_in = {dout, dout}, dim_out = {dout}}, }, } + for i = 1, #din do + layers['nerv.DuplicateLayer']['inputDup' .. i] = {dim_in = {din[i]}, dim_out = {din[i], din[i], din[i], din[i]}} + end local connections = { -- lstm input - {'<input>[1]', 'inputDup[1]', 0}, + --{'<input>[1 .. n]', 'inputDup(1 .. n)[1]', 0}, -- input gate - {'inputDup[1]', 'inputGate[1]', 0}, - {'outputDup[1]', 'inputGate[2]', 1}, - {'cellDup[1]', 'inputGate[3]', 1}, + {'outputDup[1]', 'inputGate[1]', 1}, + {'cellDup[1]', 'inputGate[2]', 1}, + --{'inputDup(1 .. n)[1]', 'inputGate[3 .. n + 2]', 0}, -- forget gate - {'inputDup[2]', 'forgetGate[1]', 0}, - {'outputDup[2]', 'forgetGate[2]', 1}, - {'cellDup[2]', 'forgetGate[3]', 1}, + {'outputDup[2]', 'forgetGate[1]', 1}, + {'cellDup[2]', 'forgetGate[2]', 1}, + --{'inputDup(1 .. n)[2]', 'forgetGate[3 .. n + 2]', 0}, -- lstm cell {'forgetGate[1]', 'forgetGateMul[1]', 0}, {'cellDup[3]', 'forgetGateMul[2]', 1}, - {'inputDup[3]', 'mainAffine[1]', 0}, - {'outputDup[3]', 'mainAffine[2]', 1}, + {'outputDup[3]', 'mainAffine[1]', 1}, + --{'inputDup(1 .. n)[3]', 'mainAffine[2 .. n + 1]', 0}, {'mainAffine[1]', 'mainTanh[1]', 0}, {'inputGate[1]', 'inputGateMul[1]', 0}, {'mainTanh[1]', 'inputGateMul[2]', 0}, @@ -67,9 +73,9 @@ function LSTMLayer:__init(id, global_conf, layer_conf) {'mainCombine[1]', 'cellDup[1]', 0}, -- forget gate - {'inputDup[4]', 'outputGate[1]', 0}, - {'outputDup[4]', 'outputGate[2]', 1}, - {'cellDup[4]', 'outputGate[3]', 0}, + {'outputDup[4]', 'outputGate[1]', 1}, + {'cellDup[4]', 'outputGate[2]', 0}, + --{'inputDup(1 .. n)[4]', 'outputGate[2 .. n + 1]', 0}, -- lstm output {'cellDup[5]', 'outputTanh[1]', 0}, @@ -78,6 +84,13 @@ function LSTMLayer:__init(id, global_conf, layer_conf) {'outputGateMul[1]', 'outputDup[1]', 0}, {'outputDup[5]', '<output>[1]', 0}, } + for i = 1, #din do + table.insert(connections, {'<input>[' .. i .. ']', 'inputDup' .. i .. '[1]', 0}) + table.insert(connections, {'inputDup' .. i .. '[1]', 'inputGate[' .. (i + 2) .. ']', 0}) + table.insert(connections, {'inputDup' .. i .. '[2]', 'forgetGate[' .. (i + 2) .. ']', 0}) + table.insert(connections, {'inputDup' .. i .. '[3]', 'mainAffine[' .. (i + 1) .. ']', 0}) + table.insert(connections, {'inputDup' .. i .. '[4]', 'outputGate[' .. (i + 2) .. ']', 0}) + end self:add_prefix(layers, connections) local layer_repo = nerv.LayerRepo(layers, pr, global_conf) |