diff options
author | txh18 <[email protected]> | 2015-11-16 11:44:43 +0800 |
---|---|---|
committer | txh18 <[email protected]> | 2015-11-16 11:44:43 +0800 |
commit | 267a486fb78a985cbfdc60ef8549b3128f716713 (patch) | |
tree | c60697e60ef5053203b5148cb3f0bfbf88a81c94 | |
parent | ef40688d5a0a3b7eae18dc364a40ae4e8e7619e7 (diff) |
fixed direct update, did not know the result
-rw-r--r-- | nerv/examples/lmptb/lm_trainer.lua | 1 | ||||
-rw-r--r-- | nerv/examples/lmptb/tnn_ptb_main.lua | 8 | ||||
-rw-r--r-- | nerv/layer/affine.lua | 23 | ||||
-rw-r--r-- | nerv/layer/affine_recurrent.lua | 25 |
4 files changed, 35 insertions, 22 deletions
diff --git a/nerv/examples/lmptb/lm_trainer.lua b/nerv/examples/lmptb/lm_trainer.lua index 226873b..2be97c8 100644 --- a/nerv/examples/lmptb/lm_trainer.lua +++ b/nerv/examples/lmptb/lm_trainer.lua @@ -16,6 +16,7 @@ function LMTrainer.lm_process_file(global_conf, fn, tnn, do_train) local result = nerv.LMResult(global_conf, global_conf.vocab) result:init("rnn") + global_conf.timer:flush() tnn:flush_all() --caution: will also flush the inputs from the reader! local next_log_wcn = global_conf.log_w_num diff --git a/nerv/examples/lmptb/tnn_ptb_main.lua b/nerv/examples/lmptb/tnn_ptb_main.lua index 891487c..19d0f8a 100644 --- a/nerv/examples/lmptb/tnn_ptb_main.lua +++ b/nerv/examples/lmptb/tnn_ptb_main.lua @@ -63,9 +63,11 @@ end --Returns: nerv.LayerRepo function prepare_layers(global_conf, paramRepo) printf("%s preparing layers...\n", global_conf.sche_log_pre) + + local du = true --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}} + 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}} local layers = { ["nerv.AffineRecurrentLayer"] = { @@ -85,7 +87,7 @@ function prepare_layers(global_conf, paramRepo) }, ["nerv.AffineLayer"] = { - ["outputL"] = {{["ltp"] = "ltp_ho", ["bp"] = "bp_o"}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.vocab:size()}}}, + ["outputL"] = {{["ltp"] = "ltp_ho", ["bp"] = "bp_o"}, {["dim_in"] = {global_conf.hidden_size}, ["dim_out"] = {global_conf.vocab:size()}, ["direct_update"] = du}}, }, ["nerv.SoftmaxCELayerT"] = { @@ -168,7 +170,7 @@ global_conf = { mmat_type = nerv.MMatrixFloat, nn_act_default = 0, - hidden_size = 300, --set to 400 for a stable good test PPL + hidden_size = 400, --set to 400 for a stable good test PPL chunk_size = 15, batch_size = 10, max_iter = 35, diff --git a/nerv/layer/affine.lua b/nerv/layer/affine.lua index 0462383..a2809bf 100644 --- a/nerv/layer/affine.lua +++ b/nerv/layer/affine.lua @@ -72,18 +72,25 @@ function AffineLayer:batch_resize(batch_size) end function AffineLayer:update(bp_err, input, output) - if self.direct_update then - self.ltp.correction:mul(input[1], bp_err[1], 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.ltp.trans:add(self.ltp.trans, self.ltp.correction, 1.0, -gconf.lrate / n) + if (self.direct_update == true) then + local gconf = self.gconf + if (gconf.momentum > 0) then + self.ltp.correction:mul(input[1], bp_err[1], 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.ltp.trans:add(self.ltp.trans, self.ltp.correction, 1.0, -gconf.lrate / n) + self.bp.trans:add(self.bp.trans, bp_err[1]:colsum(), 1.0-gconf.lrate*gconf.wcost, -gconf.lrate / gconf.batch_size) + else + self.ltp.trans:mul(input[1], bp_err[1], -gconf.lrate / gconf.batch_size, 1.0-gconf.lrate*gconf.wcost/gconf.batch_size, 'T', 'N') + 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.bp:update(bp_err[1]:colsum()) end - self.bp:update(bp_err[1]:colsum()) end function AffineLayer:propagate(input, output) diff --git a/nerv/layer/affine_recurrent.lua b/nerv/layer/affine_recurrent.lua index 92d98e2..3d448d3 100644 --- a/nerv/layer/affine_recurrent.lua +++ b/nerv/layer/affine_recurrent.lua @@ -46,17 +46,20 @@ function Recurrent:update(bp_err, input, output) local ltp_hh = self.ltp_hh.trans local bp = self.bp.trans local gconf = self.gconf - -- momentum gain - local mmt_gain = 1.0 / (1.0 - gconf.momentum); - local n = input[1]:nrow() * mmt_gain - -- update corrections (accumulated errors) - self.ltp_hh.correction:mul(input[2], bp_err[1], 1.0, gconf.momentum, 'T', 'N') - self.bp.correction:add(bc, bp_err[1]:colsum(), gconf.momentum, 1.0) - -- perform update - ltp_hh:add(ltp_hh, self.ltp_hh.correction, 1.0, -gconf.lrate / n) - bp:add(bp, self.bp.correction, 1.0, -gconf.lrate / n) - -- weight decay - ltp_hh:add(ltp_hh, ltp_hh, 1.0, -gconf.lrate * gconf.wcost) + if (gconf.momentum > 0) then + -- momentum gain + local mmt_gain = 1.0 / (1.0 - gconf.momentum); + local n = input[1]:nrow() * mmt_gain + -- update corrections (accumulated errors) + self.ltp_hh.correction:mul(input[2], bp_err[1], 1.0, gconf.momentum, 'T', 'N') + self.bp.correction:add(self.bp.correction, bp_err[1]:colsum(), gconf.momentum, 1.0) + -- perform update and weight decay + ltp_hh:add(ltp_hh, self.ltp_hh.correction, 1.0 - gconf.lrate.gconf.wcost/n, -gconf.lrate / n) + bp:add(bp, self.bp.correction, 1.0 - gconf.lrate*gconf.wcost/n, -gconf.lrate / n) + else + ltp_hh:mul(input[2], bp_err[1], -gconf.lrate/gconf.batch_size, 1.0-gconf.wcost*gconf.lrate/gconf.batch_size, 'T', 'N') + 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) |