aboutsummaryrefslogtreecommitdiff
path: root/nerv/layer
diff options
context:
space:
mode:
Diffstat (limited to 'nerv/layer')
-rw-r--r--nerv/layer/affine_recurrent.lua80
-rw-r--r--nerv/layer/gate_fff.lua73
-rw-r--r--nerv/layer/gru.lua2
-rw-r--r--nerv/layer/init.lua9
4 files changed, 8 insertions, 156 deletions
diff --git a/nerv/layer/affine_recurrent.lua b/nerv/layer/affine_recurrent.lua
deleted file mode 100644
index fd6f38f..0000000
--- a/nerv/layer/affine_recurrent.lua
+++ /dev/null
@@ -1,80 +0,0 @@
-local Recurrent = nerv.class('nerv.AffineRecurrentLayer', 'nerv.Layer')
-
---id: string
---global_conf: table
---layer_conf: table
---Get Parameters
-function Recurrent:__init(id, global_conf, layer_conf)
- self.id = id
- self.dim_in = layer_conf.dim_in
- self.dim_out = layer_conf.dim_out
- self.gconf = global_conf
- self.log_pre = self.id .. "[LOG]"
-
- self.bp = self:find_param("bp", layer_conf, global_conf, nerv.BiasParam, {1, self.dim_out[1]}) --layer_conf.bp
- self.ltp_hh = self:find_param("ltphh", layer_conf, global_conf, nerv.LinearTransParam, {self.dim_in[2], self.dim_out[1]}) --layer_conf.ltp_hh --from hidden to hidden
- self.ltp_ih = self:find_param("ltpih", layer_conf, global_conf, nerv.LinearTransParam, {self.dim_in[1], self.dim_out[1]}) --layer_conf.ltp_hh --from hidden to hidden
-
- self:check_dim_len(2, 1)
- self.direct_update = layer_conf.direct_update
-
- self.clip = layer_conf.clip --clip error in back_propagate
- if self.clip ~= nil then
- nerv.info("%s creating, will clip the error by %f", self.log_pre, self.clip)
- end
-end
-
---Check parameter
-function Recurrent:init(batch_size)
- if self.ltp_hh.trans:ncol() ~= self.bp.trans:ncol() or
- self.ltp_ih.trans:ncol() ~= self.bp.trans:ncol() then
- nerv.error("mismatching dimensions of ltp and bp")
- end
- if self.dim_in[1] ~= self.ltp_ih.trans:nrow() or
- self.dim_in[2] ~= self.ltp_hh.trans:nrow() then
- nerv.error("mismatching dimensions of ltp and input")
- end
- if (self.dim_out[1] ~= self.bp.trans:ncol()) then
- nerv.error("mismatching dimensions of bp and output")
- end
-
- self.ltp_hh:train_init()
- self.ltp_ih:train_init()
- self.bp:train_init()
-end
-
-function Recurrent:batch_resize(batch_size)
- -- do nothing
-end
-
-function Recurrent:update(bp_err, input, output)
- self.ltp_ih:update_by_err_input(bp_err[1], input[1])
- self.ltp_hh:update_by_err_input(bp_err[1], input[2])
- self.bp:update_by_gradient(bp_err[1]:colsum())
-end
-
-function Recurrent:propagate(input, output)
- output[1]:mul(input[1], self.ltp_ih.trans, 1.0, 0.0, 'N', 'N')
- output[1]:mul(input[2], self.ltp_hh.trans, 1.0, 1.0, 'N', 'N')
- output[1]:add_row(self.bp.trans, 1.0)
-end
-
-function Recurrent:back_propagate(bp_err, next_bp_err, input, output)
- next_bp_err[1]:mul(bp_err[1], self.ltp_ih.trans, 1.0, 0.0, 'N', 'T')
- next_bp_err[2]:mul(bp_err[1], self.ltp_hh.trans, 1.0, 0.0, 'N', 'T')
- --[[
- for i = 0, next_bp_err[2]:nrow() - 1 do
- for j = 0, next_bp_err[2]:ncol() - 1 do
- if (next_bp_err[2][i][j] > 10) then next_bp_err[2][i][j] = 10 end
- if (next_bp_err[2][i][j] < -10) then next_bp_err[2][i][j] = -10 end
- end
- end
- ]]--
- if self.clip ~= nil then
- next_bp_err[2]:clip(-self.clip, self.clip)
- end
-end
-
-function Recurrent:get_params()
- return nerv.ParamRepo({self.ltp_ih, self.ltp_hh, self.bp})
-end
diff --git a/nerv/layer/gate_fff.lua b/nerv/layer/gate_fff.lua
deleted file mode 100644
index 6082e27..0000000
--- a/nerv/layer/gate_fff.lua
+++ /dev/null
@@ -1,73 +0,0 @@
-local GateFFFLayer = nerv.class('nerv.GateFLayer', 'nerv.Layer') --Full matrix gate
-
-function GateFFFLayer:__init(id, global_conf, layer_conf)
- self.id = id
- self.dim_in = layer_conf.dim_in
- self.dim_out = layer_conf.dim_out
- self.gconf = global_conf
-
- for i = 1, #self.dim_in do
- self["ltp" .. i] = self:find_param("ltp" .. i, layer_conf, global_conf, nerv.LinearTransParam, {self.dim_in[i], self.dim_out[1]}) --layer_conf.ltp
- end
- self.bp = self:find_param("bp", layer_conf, global_conf, nerv.BiasParam, {1, self.dim_out[1]})--layer_conf.bp
-
- self:check_dim_len(-1, 1) --accept multiple inputs
-end
-
-function GateFFFLayer:init(batch_size)
- for i = 1, #self.dim_in do
- if self["ltp" .. i].trans:ncol() ~= self.bp.trans:ncol() then
- nerv.error("mismatching dimensions of linear transform and bias paramter")
- end
- if self.dim_in[i] ~= self["ltp" .. i].trans:nrow() then
- nerv.error("mismatching dimensions of linear transform parameter and input")
- end
- self["ltp"..i]:train_init()
- end
-
- if self.dim_out[1] ~= self.ltp1.trans:ncol() then
- nerv.error("mismatching dimensions of linear transform parameter and output")
- end
- self.bp:train_init()
- self.err_bakm = self.gconf.cumat_type(batch_size, self.dim_out[1])
-end
-
-function GateFFFLayer:batch_resize(batch_size)
- if self.err_m:nrow() ~= batch_size then
- self.err_bakm = self.gconf.cumat_type(batch_size, self.dim_out[1])
- end
-end
-
-function GateFFFLayer:propagate(input, output)
- -- apply linear transform
- output[1]:mul(input[1], self.ltp1.trans, 1.0, 0.0, 'N', 'N')
- for i = 2, #self.dim_in do
- output[1]:mul(input[i], self["ltp" .. i].trans, 1.0, 1.0, 'N', 'N')
- end
- -- add bias
- output[1]:add_row(self.bp.trans, 1.0)
- output[1]:sigmoid(output[1])
-end
-
-function GateFFFLayer:back_propagate(bp_err, next_bp_err, input, output)
- self.err_bakm:sigmoid_grad(bp_err[1], output[1])
- for i = 1, #self.dim_in do
- next_bp_err[i]:mul(self.err_bakm, self["ltp" .. i].trans, 1.0, 0.0, 'N', 'T')
- end
-end
-
-function GateFFFLayer:update(bp_err, input, output)
- self.err_bakm:sigmoid_grad(bp_err[1], output[1])
- for i = 1, #self.dim_in do
- self["ltp" .. i]:update_by_err_input(self.err_bakm, input[i])
- end
- self.bp:update_by_gradient(self.err_bakm:colsum())
-end
-
-function GateFFFLayer:get_params()
- local pr = nerv.ParamRepo({self.bp})
- for i = 1, #self.dim_in do
- pr:add(self["ltp" .. i].id, self["ltp" .. i])
- end
- return pr
-end
diff --git a/nerv/layer/gru.lua b/nerv/layer/gru.lua
index 2162e28..e81d21a 100644
--- a/nerv/layer/gru.lua
+++ b/nerv/layer/gru.lua
@@ -48,7 +48,7 @@ function GRULayer:__init(id, global_conf, layer_conf)
["nerv.TanhLayer"] = {
[ap("mainTanhL")] = {{}, {dim_in = {dout1}, dim_out = {dout1}}},
},
- ["nerv.GateFLayer"] = {
+ ["nerv.LSTMGateLayer"] = {
[ap("resetGateL")] = {{}, {dim_in = {din1, din2},
dim_out = {din2},
pr = pr}},
diff --git a/nerv/layer/init.lua b/nerv/layer/init.lua
index 6b7a1d7..54f33ae 100644
--- a/nerv/layer/init.lua
+++ b/nerv/layer/init.lua
@@ -109,11 +109,16 @@ nerv.include('bias.lua')
nerv.include('window.lua')
nerv.include('mse.lua')
nerv.include('combiner.lua')
-nerv.include('affine_recurrent.lua')
nerv.include('softmax.lua')
nerv.include('elem_mul.lua')
-nerv.include('gate_fff.lua')
nerv.include('lstm.lua')
nerv.include('lstm_gate.lua')
nerv.include('dropout.lua')
nerv.include('gru.lua')
+
+-- The following lines are for backward compatibility, and will be removed in
+-- the future. The use of these names are deprecated.
+nerv.DropoutLayerT = nerv.DropoutLayer
+nerv.GRULayerT = nerv.GRULayer
+nerv.LSTMLayerT = nerv.LSTMLayer
+nerv.SoftmaxCELayerT = nerv.SoftmaxCELayer