aboutsummaryrefslogtreecommitdiff
path: root/layer
diff options
context:
space:
mode:
Diffstat (limited to 'layer')
-rw-r--r--layer/affine.lua31
-rw-r--r--layer/bias.lua24
-rw-r--r--layer/init.lua23
-rw-r--r--layer/sigmoid.lua12
-rw-r--r--layer/softmax_ce.lua18
-rw-r--r--layer/window.lua24
6 files changed, 115 insertions, 17 deletions
diff --git a/layer/affine.lua b/layer/affine.lua
index 573b98d..90a1d16 100644
--- a/layer/affine.lua
+++ b/layer/affine.lua
@@ -12,14 +12,27 @@ function MatrixParam:write(pfhandle)
self.trans:new_to_host():save(pfhandle)
end
-function AffineLayer:__init(id, global_conf, ltp, bp)
+function AffineLayer:__init(id, global_conf, layer_conf)
self.id = id
- self.ltp = ltp
- self.bp = bp
+ self.ltp = layer_conf.ltp
+ self.bp = layer_conf.bp
+ self.dim_in = layer_conf.dim_in
+ self.dim_out = layer_conf.dim_out
self.gconf = global_conf
+ self:check_dim_len(1, 1) -- exactly one input and one output
end
function AffineLayer:init()
+ if self.ltp.trans:ncol() ~= self.bp.trans:ncol() then
+ nerv.error("mismatching dimensions of linear transform and bias paramter")
+ end
+ if self.dim_in[1] ~= self.ltp.trans:nrow() then
+ nerv.error("mismatching dimensions of linear transform parameter and input")
+ end
+ if self.dim_out[1] ~= self.ltp.trans:ncol() then
+ nerv.error("mismatching dimensions of linear transform parameter and output")
+ end
+
-- linear transform correction
self.ltc = self.ltp.trans:create()
self.ltc:fill(0)
@@ -36,10 +49,10 @@ function nerv.AffineLayer:update(bp_err, input, output)
local gconf = self.gconf
-- momentum gain
local mmt_gain = 1.0 / (1.0 - gconf.momentum);
- local n = input[0]:nrow() * mmt_gain
+ local n = input[1]:nrow() * mmt_gain
-- update corrections (accumulated errors)
- ltc:mul(input[0], bp_err[0], 1.0, gconf.momentum, 'T', 'N')
- bc:add(bc, bp_err[0]:colsum(), gconf.momentum, 1.0)
+ ltc:mul(input[1], bp_err[1], 1.0, gconf.momentum, 'T', 'N')
+ bc:add(bc, bp_err[1]:colsum(), gconf.momentum, 1.0)
-- perform update
ltp:add(ltp, ltc, 1.0, -gconf.lrate / n)
bp:add(bp, bc, 1.0, -gconf.lrate / n)
@@ -49,11 +62,11 @@ end
function nerv.AffineLayer:propagate(input, output)
-- apply linear transform
- output[0]:mul(input[0], self.ltp.trans, 1.0, 0.0, 'N', 'N')
+ output[1]:mul(input[1], self.ltp.trans, 1.0, 0.0, 'N', 'N')
-- add bias
- output[0]:add_row(self.bp.trans, 1.0)
+ output[1]:add_row(self.bp.trans, 1.0)
end
function nerv.AffineLayer:back_propagate(next_bp_err, bp_err, input, output)
- next_bp_err[0]:mul(bp_err[0], self.ltp.trans, 1.0, 0.0, 'N', 'T')
+ next_bp_err[1]:mul(bp_err[1], self.ltp.trans, 1.0, 0.0, 'N', 'T')
end
diff --git a/layer/bias.lua b/layer/bias.lua
new file mode 100644
index 0000000..6ddfe11
--- /dev/null
+++ b/layer/bias.lua
@@ -0,0 +1,24 @@
+local BiasLayer = nerv.class("nerv.BiasLayer", "nerv.Layer")
+
+function BiasLayer:__init(id, global_conf, layer_conf)
+ self.id = id
+ self.gconf = global_conf
+ self.bias = layer_conf.bias
+ self.dim_in = layer_conf.dim_in
+ self.dim_out = layer_conf.dim_out
+ self:check_dim_len(1, 1)
+end
+
+function BiasLayer:init()
+ if self.dim_in[1] ~= self.bias.trans:ncol() then
+ nerv.error("mismatching dimensions of input and bias parameter")
+ end
+ if self.dim_out[1] ~= self.bias.trans:ncol() then
+ nerv.error("mismatching dimensions of output and bias parameter")
+ end
+end
+
+function BiasLayer:propagate(input, output)
+ output[1]:copy_fromd(input[1])
+ output[1]:add_row(self.bias.trans, 1.0)
+end
diff --git a/layer/init.lua b/layer/init.lua
index a98621d..c8c691b 100644
--- a/layer/init.lua
+++ b/layer/init.lua
@@ -44,3 +44,26 @@ end
function nerv.Layer:back_propagate(next_bp_err, bp_err, input, output)
nerv.error_method_not_implemented()
end
+
+function nerv.Layer:check_dim_len(len_in, len_out)
+ local expected_in = #self.dim_in
+ local expected_out = #self.dim_out
+ if len_in > 0 and expected_in ~= len_in then
+ nerv.error("layer %s expects %d inputs, %d given",
+ self.id, len_in, expected_in)
+ end
+ if len_out > 0 and expected_out ~= len_out then
+ nerv.error("layer %s expects %d outputs, %d given",
+ self.id, len_out, expected_out)
+ end
+end
+
+function nerv.Layer:get_dim()
+ return self.dim_in, self.dim_out
+end
+
+require 'layer.affine'
+require 'layer.sigmoid'
+require 'layer.softmax_ce'
+require 'layer.bias'
+require 'layer.window'
diff --git a/layer/sigmoid.lua b/layer/sigmoid.lua
index ca34419..220b7af 100644
--- a/layer/sigmoid.lua
+++ b/layer/sigmoid.lua
@@ -1,11 +1,17 @@
local SigmoidLayer = nerv.class("nerv.SigmoidLayer", "nerv.Layer")
-function SigmoidLayer:__init(id, global_conf)
+function SigmoidLayer:__init(id, global_conf, layer_conf)
self.id = id
self.gconf = global_conf
+ self.dim_in = layer_conf.dim_in
+ self.dim_out = layer_conf.dim_out
+ self:check_dim_len(1, 1)
end
function SigmoidLayer:init()
+ if self.dim_in[1] ~= self.dim_out[1] then
+ nerv.error("mismatching dimensions of input and output")
+ end
end
function SigmoidLayer:update(bp_err, input, output)
@@ -13,9 +19,9 @@ function SigmoidLayer:update(bp_err, input, output)
end
function SigmoidLayer:propagate(input, output)
- output[0]:sigmoid(input[0])
+ output[1]:sigmoid(input[1])
end
function SigmoidLayer:back_propagate(next_bp_err, bp_err, input, output)
- next_bp_err[0]:sigmoid_grad(bp_err[0], output[0])
+ next_bp_err[1]:sigmoid_grad(bp_err[1], output[1])
end
diff --git a/layer/softmax_ce.lua b/layer/softmax_ce.lua
index 37d2864..09eb3a9 100644
--- a/layer/softmax_ce.lua
+++ b/layer/softmax_ce.lua
@@ -1,11 +1,17 @@
local SoftmaxCELayer = nerv.class("nerv.SoftmaxCELayer", "nerv.Layer")
-function SoftmaxCELayer:__init(id, global_conf)
+function SoftmaxCELayer:__init(id, global_conf, layer_conf)
self.id = id
self.gconf = global_conf
+ self.dim_in = layer_conf.dim_in
+ self.dim_out = layer_conf.dim_out
+ self:check_dim_len(2, -1) -- two inputs: nn output and label
end
function SoftmaxCELayer:init()
+ if self.dim_in[1] ~= self.dim_in[1] then
+ nerv.error("mismatching dimensions of previous network output and labels")
+ end
self.total_ce = 0.0
self.total_frames = 0
end
@@ -15,12 +21,14 @@ function SoftmaxCELayer:update(bp_err, input, output)
end
function SoftmaxCELayer:propagate(input, output)
- local soutput = input[0]:create() -- temporary value for calc softmax
+ local soutput = input[1]:create() -- temporary value for calc softmax
self.soutput = soutput
- soutput:softmax(input[0])
+ soutput:softmax(input[1])
local ce = soutput:create()
ce:log_elem(soutput)
- ce:mul_elem(ce, input[1])
+ ce:mul_elem(ce, input[2])
+-- print(input[1][0])
+-- print(soutput[1][0])
-- add total ce
self.total_ce = self.total_ce - ce:rowsum():colsum()[0]
self.total_frames = self.total_frames + soutput:nrow()
@@ -28,5 +36,5 @@ end
function SoftmaxCELayer:back_propagate(next_bp_err, bp_err, input, output)
-- softmax output - label
- next_bp_err[0]:add(self.soutput, input[1], 1.0, -1.0)
+ next_bp_err[1]:add(self.soutput, input[2], 1.0, -1.0)
end
diff --git a/layer/window.lua b/layer/window.lua
new file mode 100644
index 0000000..8e9e761
--- /dev/null
+++ b/layer/window.lua
@@ -0,0 +1,24 @@
+local WindowLayer = nerv.class("nerv.WindowLayer", "nerv.Layer")
+
+function WindowLayer:__init(id, global_conf, layer_conf)
+ self.id = id
+ self.gconf = global_conf
+ self.window = layer_conf.window
+ self.dim_in = layer_conf.dim_in
+ self.dim_out = layer_conf.dim_out
+ self:check_dim_len(1, 1)
+end
+
+function WindowLayer:init()
+ if self.dim_in[1] ~= self.window.trans:ncol() then
+ nerv.error("mismatching dimensions of input and window parameter")
+ end
+ if self.dim_out[1] ~= self.window.trans:ncol() then
+ nerv.error("mismatching dimensions of output and window parameter")
+ end
+end
+
+function WindowLayer:propagate(input, output)
+ output[1]:copy_fromd(input[1])
+ output[1]:scale_row(self.window.trans)
+end