From 267a486fb78a985cbfdc60ef8549b3128f716713 Mon Sep 17 00:00:00 2001 From: txh18 Date: Mon, 16 Nov 2015 11:44:43 +0800 Subject: fixed direct update, did not know the result --- nerv/layer/affine.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'nerv/layer/affine.lua') 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) -- cgit v1.2.3