aboutsummaryrefslogtreecommitdiff
path: root/nerv/layer
diff options
context:
space:
mode:
authortxh18 <cloudygooseg@gmail.com>2015-11-23 15:54:20 +0800
committertxh18 <cloudygooseg@gmail.com>2015-11-23 15:54:20 +0800
commite76ae9b12651ed8497537edf357f4cf90421ea0d (patch)
tree6a305444219a041ce953a3323e4d5449e335b218 /nerv/layer
parent979473dcc890a92fb90b470b924d1e1e70f6dbc0 (diff)
parentec6bde79a5817409bb8a77075b411974c1d8f856 (diff)
merge in recent changes about param updates
Merge branch 'master' into txh18/rnnlm
Diffstat (limited to 'nerv/layer')
-rw-r--r--nerv/layer/affine.lua72
1 files changed, 26 insertions, 46 deletions
diff --git a/nerv/layer/affine.lua b/nerv/layer/affine.lua
index 3e84ec0..ed58d38 100644
--- a/nerv/layer/affine.lua
+++ b/nerv/layer/affine.lua
@@ -17,49 +17,46 @@ function MatrixParam:train_init()
self.correction:fill(0)
end
-function MatrixParam:update_by_gradient(gradient)
+function MatrixParam:_update_by_gradient(gradient, alpha, beta)
local gconf = self.gconf
+ -- momentum gain
+ local mmt_gain = 1.0 / (1.0 - gconf.momentum)
+ local n = self.gconf.batch_size * mmt_gain
+ -- perform update
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)
+ self.trans:add(self.trans, self.correction, alpha, -gconf.lrate / n * beta)
else
- self.trans:add(self.trans, gradient, 1.0 - gconf.lrate * gconf.wcost / gconf.batch_size, - gconf.lrate / gconf.batch_size)
+ self.trans:add(self.trans, gradient, alpha, -gconf.lrate / n * beta)
end
end
-function MatrixParam:update_by_err_input(err, input)
+function MatrixParam:_update_by_err_input(err, input, alpha, beta)
local gconf = self.gconf
+ -- momentum gain
+ local mmt_gain = 1.0 / (1.0 - gconf.momentum)
+ local n = self.gconf.batch_size * mmt_gain
+ -- perform update
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)
+ self.trans:add(self.trans, self.correction, alpha, -gconf.lrate / n * beta)
else
- self.trans:mul(input, err, - gconf.lrate / gconf.batch_size, 1.0 - gconf.lrate * gconf.wcost / gconf.batch_size, 'T', 'N')
+ self.trans:mul(input, err, -gconf.lrate / n * beta, alpha, 'T', 'N')
end
end
---[[ --these updates are the same
-function LinearTransParam:update(gradient)
- MatrixParam.update(self, gradient)
- -- local gconf = self.gconf
- -- weight decay(put into MatrixParam:update)
- -- self.trans:add(self.trans, self.trans, 1.0, -gconf.lrate * gconf.wcost / gconf.batch_size)
+function MatrixParam:update_by_gradient(gradient)
+ self:_update_by_gradient(gradient, 1.0, 1.0)
+end
+
+function MatrixParam:update_by_err_input(err, input)
+ self:_update_by_err_input(err, input, 1.0, 1.0)
end
-function BiasParam:update(gradient)
- MatrixParam.update(self, gradient)
- --local gconf = self.gconf
- -- weight decay
- -- self.trans:add(self.trans, self.trans, 1.0, -gconf.lrate * gconf.wcost / gconf.batch_size)
+function LinearTransParam:update_by_err_input(err, input)
+ local l2 = 1 - self.gconf.lrate * self.gconf.wcost
+ self:_update_by_err_input(err, input, l2, l2)
end
-]]--
function AffineLayer:__init(id, global_conf, layer_conf)
self.id = id
@@ -69,7 +66,7 @@ function AffineLayer:__init(id, global_conf, layer_conf)
self.bp = self:find_param("bp", layer_conf, global_conf, nerv.BiasParam, {1, self.dim_out[1]})--layer_conf.bp
self.gconf = global_conf
self:check_dim_len(1, 1) -- exactly one input and one output
- self.direct_update = layer_conf.direct_update or global_conf.direct_update
+ -- self.direct_update = layer_conf.direct_update or global_conf.direct_update
end
function AffineLayer:init(batch_size)
@@ -92,25 +89,8 @@ function AffineLayer:batch_resize(batch_size)
end
function AffineLayer:update(bp_err, input, output)
- 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')
- self.bp.correction:add(self.bp.correction, bp_err[1]:colsum(), gconf.momentum, 1)
- -- 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 * gconf.wcost / gconf.batch_size, - gconf.lrate / n)
- self.bp.trans:add(self.bp.trans, self.bp.correction, 1.0 - gconf.lrate * gconf.wcost / gconf.batch_size, - gconf.lrate / n)
- 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:update_by_err_input(bp_err[1], input[1])
- self.bp:update_by_gradient(bp_err[1]:colsum())
- end
+ self.ltp:update_by_err_input(bp_err[1], input[1])
+ self.bp:update_by_gradient(bp_err[1]:colsum())
end
function AffineLayer:propagate(input, output)