aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nerv/examples/lmptb/tnn_ptb_main.lua4
-rw-r--r--nerv/layer/affine.lua37
-rw-r--r--nerv/layer/affine_recurrent.lua4
3 files changed, 32 insertions, 13 deletions
diff --git a/nerv/examples/lmptb/tnn_ptb_main.lua b/nerv/examples/lmptb/tnn_ptb_main.lua
index 491d4b7..f68311c 100644
--- a/nerv/examples/lmptb/tnn_ptb_main.lua
+++ b/nerv/examples/lmptb/tnn_ptb_main.lua
@@ -64,7 +64,7 @@ end
function prepare_layers(global_conf, paramRepo)
printf("%s preparing layers...\n", global_conf.sche_log_pre)
- local du = true
+ local du = false
--local recurrentLconfig = {{["bp"] = "bp_h", ["ltp_hh"] = "ltp_hh"}, {["dim_in"] = {global_conf.hidden_size, global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}, ["break_id"] = global_conf.vocab:get_sen_entry().id, ["independent"] = global_conf.independent, ["clip"] = 10}}
local recurrentLconfig = {{["bp"] = "bp_h", ["ltp_hh"] = "ltp_hh"}, {["dim_in"] = {global_conf.hidden_size, global_conf.hidden_size}, ["dim_out"] = {global_conf.hidden_size}, ["clip"] = 10, ["direct_update"] = du}}
@@ -165,7 +165,7 @@ test_fn = data_dir .. '/ptb.test.txt.adds'
vocab_fn = data_dir .. '/vocab'
global_conf = {
- lrate = 1, wcost = 1e-5, momentum = 0.9,
+ lrate = 1, wcost = 1e-5, momentum = 0,
cumat_type = nerv.CuMatrixFloat,
mmat_type = nerv.MMatrixFloat,
nn_act_default = 0,
diff --git a/nerv/layer/affine.lua b/nerv/layer/affine.lua
index 0fcff36..c24af16 100644
--- a/nerv/layer/affine.lua
+++ b/nerv/layer/affine.lua
@@ -19,14 +19,33 @@ end
function MatrixParam:update(gradient)
local gconf = self.gconf
- self.correction:add(self.correction, gradient, gconf.momentum, 1.0)
- -- momentum gain
- local mmt_gain = 1.0 / (1.0 - gconf.momentum);
- local n = self.gconf.batch_size * mmt_gain
- -- perform update
- self.trans:add(self.trans, self.correction, 1.0 - gconf.lrate*gconf.wcost/gconf.batch_size, -gconf.lrate / n)
+ if (gconf.momentum > 0) then
+ self.correction:add(self.correction, gradient, gconf.momentum, 1.0)
+ -- momentum gain
+ local mmt_gain = 1.0 / (1.0 - gconf.momentum);
+ local n = self.gconf.batch_size * mmt_gain
+ -- perform update
+ self.trans:add(self.trans, self.correction, 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, -gconf.lrate/n)
+ else
+ self.trans:add(self.trans, gradient, 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, -gconf.lrate/gconf.batch_size)
+ end
+end
+
+function MatrixParam:updateEI(err, input)
+ local gconf = self.gconf
+ if (gconf.momentum > 0) then
+ self.correction:mul(input, err, 1.0, gconf.momentum, 'T', 'N')
+ -- momentum gain
+ local mmt_gain = 1.0 / (1.0 - gconf.momentum);
+ local n = self.gconf.batch_size * mmt_gain
+ -- perform update
+ self.trans:add(self.trans, self.correction, 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, -gconf.lrate/n)
+ else
+ self.trans:mul(input, err, -gconf.lrate/gconf.batch_size, 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, 'T', 'N')
+ end
end
+--[[ --these updates are the same
function LinearTransParam:update(gradient)
MatrixParam.update(self, gradient)
-- local gconf = self.gconf
@@ -36,10 +55,11 @@ end
function BiasParam:update(gradient)
MatrixParam.update(self, gradient)
- -- local gconf = self.gconf
+ --local gconf = self.gconf
-- weight decay
-- self.trans:add(self.trans, self.trans, 1.0, -gconf.lrate * gconf.wcost / gconf.batch_size)
end
+]]--
function AffineLayer:__init(id, global_conf, layer_conf)
self.id = id
@@ -88,8 +108,7 @@ function AffineLayer:update(bp_err, input, output)
self.bp.trans:add(self.bp.trans, bp_err[1]:colsum(), 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, -gconf.lrate / gconf.batch_size)
end
else
- self.ltp_grad:mul(input[1], bp_err[1], 1.0, 0.0, 'T', 'N')
- self.ltp:update(self.ltp_grad)
+ self.ltp:updateEI(bp_err[1], input[1])
self.bp:update(bp_err[1]:colsum())
end
end
diff --git a/nerv/layer/affine_recurrent.lua b/nerv/layer/affine_recurrent.lua
index c8cd382..b465e95 100644
--- a/nerv/layer/affine_recurrent.lua
+++ b/nerv/layer/affine_recurrent.lua
@@ -61,8 +61,8 @@ function Recurrent:update(bp_err, input, output)
bp:add(bp, bp_err[1]:colsum(), 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, -gconf.lrate/gconf.batch_size)
end
else
- self.ltp_hh_grad:mul(input[2], bp_err[1], 1.0, 0.0, 'T', 'N')
- self.ltp_hh:update(self.ltp_hh_grad)
+ --self.ltp_hh_grad:mul(input[2], bp_err[1], 1.0, 0.0, 'T', 'N')
+ self.ltp_hh:updateEI(bp_err[1], input[2])
self.bp:update(bp_err[1]:colsum())
end
end