From 0d3d8f4afdc38726b8ed933dbfcb85e759145c43 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 2 Jun 2015 12:51:18 +0800 Subject: add preprocessing layers and change layer constructor interface --- layer/softmax_ce.lua | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'layer/softmax_ce.lua') 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 -- cgit v1.2.3 From 08a52c03a77ce13ae4f6a4deb06ab0ae274d399a Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 2 Jun 2015 23:07:15 +0800 Subject: fix a bug: input[1] should be input[2] (since Lua arrays are 1-based) --- layer/softmax_ce.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'layer/softmax_ce.lua') diff --git a/layer/softmax_ce.lua b/layer/softmax_ce.lua index 3dfebc5..09eb3a9 100644 --- a/layer/softmax_ce.lua +++ b/layer/softmax_ce.lua @@ -27,6 +27,8 @@ function SoftmaxCELayer:propagate(input, output) local ce = soutput:create() ce:log_elem(soutput) 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() @@ -34,5 +36,5 @@ end function SoftmaxCELayer:back_propagate(next_bp_err, bp_err, input, output) -- softmax output - label - next_bp_err[1]:add(self.soutput, input[1], 1.0, -1.0) + next_bp_err[1]:add(self.soutput, input[2], 1.0, -1.0) end -- cgit v1.2.3