aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile3
-rw-r--r--examples/test_dnn_layers.lua34
-rw-r--r--layer/affine.lua31
-rw-r--r--layer/bias.lua24
-rw-r--r--layer/init.lua13
-rw-r--r--layer/sigmoid.lua12
-rw-r--r--layer/softmax_ce.lua16
-rw-r--r--layer/window.lua24
m---------speech0
9 files changed, 124 insertions, 33 deletions
diff --git a/Makefile b/Makefile
index 69fb739..3325b4d 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,8 @@ OBJS := nerv.o luaT.o common.o \
LIBS := libnerv.so
LUA_LIBS := matrix/init.lua io/init.lua nerv.lua \
pl/utils.lua pl/compat.lua \
- layer/init.lua layer/affine.lua layer/sigmoid.lua layer/softmax_ce.lua
+ layer/init.lua layer/affine.lua layer/sigmoid.lua layer/softmax_ce.lua \
+ layer/window.lua layer/bias.lua
INCLUDE := -I build/luajit-2.0/include/luajit-2.0/ -DLUA_USE_APICHECK
CUDA_BASE := /usr/local/cuda-6.5
CUDA_INCLUDE := -I $(CUDA_BASE)/include/
diff --git a/examples/test_dnn_layers.lua b/examples/test_dnn_layers.lua
index 866e685..9be9d71 100644
--- a/examples/test_dnn_layers.lua
+++ b/examples/test_dnn_layers.lua
@@ -11,10 +11,14 @@ bp = pf:read_chunk("b", global_conf)
-- print(bp.trans)
-af = nerv.AffineLayer("test", global_conf, ltp, bp)
-sg = nerv.SigmoidLayer("test2", global_conf)
-sm = nerv.SoftmaxCELayer("test3", global_conf)
-
+af = nerv.AffineLayer("test", global_conf, {["ltp"] = ltp,
+ ["bp"] = bp,
+ dim_in = {429},
+ dim_out = {2048}})
+sg = nerv.SigmoidLayer("test2", global_conf, {dim_in = {2048},
+ dim_out = {2048}})
+sm = nerv.SoftmaxCELayer("test3", global_conf, {dim_in = {2048, 2048},
+ dim_out = {}})
af:init()
sg:init()
sm:init()
@@ -27,18 +31,18 @@ for i = 0, 9 do
label[i][i] = 1.0
end
-input1 = {[0] = df:read_chunk("input", global_conf).trans}
-output1 = {[0] = nerv.CuMatrixFloat(10, 2048)}
+input1 = {df:read_chunk("input", global_conf).trans}
+output1 = {nerv.CuMatrixFloat(10, 2048)}
input2 = output1
-output2 = {[0] = nerv.CuMatrixFloat(10, 2048)}
-input3 = {[0] = output2[0], [1] = label}
+output2 = {nerv.CuMatrixFloat(10, 2048)}
+input3 = {output2[1], label}
output3 = nil
err_input1 = nil
-err_output1 = {[0] = nerv.CuMatrixFloat(10, 2048)}
+err_output1 = {nerv.CuMatrixFloat(10, 2048)}
err_input2 = err_output1
-err_output2 = {[0] = nerv.CuMatrixFloat(10, 2048)}
+err_output2 = {nerv.CuMatrixFloat(10, 2048)}
err_input3 = err_output2
-err_output3 = {[0] = input1[0]:create()}
+err_output3 = {input1[1]:create()}
for i = 0, 3 do
-- propagate
@@ -59,13 +63,13 @@ for i = 0, 3 do
print("output1")
- print(output1[0])
+ print(output1[1])
print("output2")
- print(output2[0])
+ print(output2[1])
print("err_output1")
- print(err_output1[0])
+ print(err_output1[1])
print("err_output2")
- print(err_output2[0])
+ print(err_output2[1])
nerv.utils.printf("cross entropy: %.8f\n", sm.total_ce)
nerv.utils.printf("frames: %.8f\n", sm.total_frames)
end
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..4881cb7 100644
--- a/layer/init.lua
+++ b/layer/init.lua
@@ -44,3 +44,16 @@ 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 = table.getn(self.dim_in)
+ local expected_out = table.getn(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
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..3dfebc5 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,12 @@ 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])
-- add total ce
self.total_ce = self.total_ce - ce:rowsum():colsum()[0]
self.total_frames = self.total_frames + soutput:nrow()
@@ -28,5 +34,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[1], 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
diff --git a/speech b/speech
-Subproject d8ea67ee420c2fc73085da04de86df023acd98d
+Subproject 821aec314824b89e9fe9c3ee467793a05ed89ee