From eba6049a82455499c68ee875843b6f44d6164fa5 Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 5 Jun 2015 16:56:33 +0800 Subject: add close method for ChunkFile, fix #18 --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 5c6fa7b..5b6e081 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ LUA_LIBS := matrix/init.lua io/init.lua nerv.lua \ nn/init.lua nn/layer_repo.lua nn/param_repo.lua nn/layer_dag.lua \ io/sgd_buffer.lua INCLUDE := -I build/luajit-2.0/include/luajit-2.0/ -DLUA_USE_APICHECK -CUDA_BASE := /usr/local/cuda-6.5 +# CUDA_BASE := /usr/local/cuda-6.5 +CUDA_BASE := /usr/local/cuda-5.0 CUDA_INCLUDE := -I $(CUDA_BASE)/include/ INCLUDE += $(CUDA_INCLUDE) LDFLAGS := -L$(CUDA_BASE)/lib64/ -Wl,-rpath=$(CUDA_BASE)/lib64/ -lcudart -lcublas -- cgit v1.2.3-70-g09d2 From 37af4bed9c3680fdb9db569605f15013e9b6b64d Mon Sep 17 00:00:00 2001 From: Determinant Date: Fri, 5 Jun 2015 17:53:05 +0800 Subject: add get_params to all layers --- Makefile | 2 +- examples/test_nn_lib.lua | 29 +++++++++++++++++++++-------- layer/affine.lua | 10 +++++++--- layer/bias.lua | 4 ++++ layer/init.lua | 4 ++++ layer/sigmoid.lua | 4 ++++ layer/softmax_ce.lua | 4 ++++ layer/window.lua | 4 ++++ nn/layer_dag.lua | 28 +++++++++++++++++++--------- 9 files changed, 68 insertions(+), 21 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 5b6e081..0468f57 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ LUA_LIBS := matrix/init.lua io/init.lua nerv.lua \ nn/init.lua nn/layer_repo.lua nn/param_repo.lua nn/layer_dag.lua \ io/sgd_buffer.lua INCLUDE := -I build/luajit-2.0/include/luajit-2.0/ -DLUA_USE_APICHECK -# CUDA_BASE := /usr/local/cuda-6.5 +#CUDA_BASE := /usr/local/cuda-6.5 CUDA_BASE := /usr/local/cuda-5.0 CUDA_INCLUDE := -I $(CUDA_BASE)/include/ INCLUDE += $(CUDA_INCLUDE) diff --git a/examples/test_nn_lib.lua b/examples/test_nn_lib.lua index 04fd7d6..6fdbd67 100644 --- a/examples/test_nn_lib.lua +++ b/examples/test_nn_lib.lua @@ -117,7 +117,7 @@ tnet_reader = nerv.TNetReader(gconf, buffer = nerv.SGDBuffer(gconf, { buffer_size = 81920, - -- randomize = true, + randomize = true, readers = { { reader = tnet_reader, data = {main_scp = 429, ref = 1}} @@ -128,9 +128,12 @@ sm = sublayer_repo:get_layer("softmax_ce0") main = layer_repo:get_layer("main") main:init(gconf.batch_size) gconf.cnt = 0 +-- data = buffer:get_data() +-- input = {data.main_scp, data.ref} +-- while true do for data in buffer.get_data, buffer do - if gconf.cnt == 1000 then break end - gconf.cnt = gconf.cnt + 1 +-- if gconf.cnt == 100 then break end +-- gconf.cnt = gconf.cnt + 1 input = {data.main_scp, data.ref} output = {} @@ -141,11 +144,21 @@ for data in buffer.get_data, buffer do main:back_propagate(err_output, err_input, input, output) main:update(err_input, input, output) - nerv.utils.printf("cross entropy: %.8f\n", sm.total_ce) - nerv.utils.printf("correct: %d\n", sm.total_correct) - nerv.utils.printf("frames: %d\n", sm.total_frames) - nerv.utils.printf("err/frm: %.8f\n", sm.total_ce / sm.total_frames) - nerv.utils.printf("accuracy: %.8f\n", sm.total_correct / sm.total_frames) +-- nerv.utils.printf("cross entropy: %.8f\n", sm.total_ce) +-- nerv.utils.printf("correct: %d\n", sm.total_correct) +-- nerv.utils.printf("frames: %d\n", sm.total_frames) +-- nerv.utils.printf("err/frm: %.8f\n", sm.total_ce / sm.total_frames) +-- nerv.utils.printf("accuracy: %.8f\n", sm.total_correct / sm.total_frames) collectgarbage("collect") end +nerv.utils.printf("cross entropy: %.8f\n", sm.total_ce) +nerv.utils.printf("correct: %d\n", sm.total_correct) +nerv.utils.printf("accuracy: %.3f%%\n", sm.total_correct / sm.total_frames * 100) +nerv.utils.printf("writing back...\n") +cf = nerv.ChunkFile("output.nerv", "w") +for i, p in ipairs(main:get_params()) do + print(p) + cf:write_chunk(p) +end +cf:close() nerv.Matrix.print_profile() diff --git a/layer/affine.lua b/layer/affine.lua index 59a0e91..2cd7acb 100644 --- a/layer/affine.lua +++ b/layer/affine.lua @@ -41,7 +41,7 @@ function AffineLayer:init() self.bc:fill(0) end -function nerv.AffineLayer:update(bp_err, input, output) +function AffineLayer:update(bp_err, input, output) local ltp = self.ltp.trans local bp = self.bp.trans local ltc = self.ltc @@ -60,13 +60,17 @@ function nerv.AffineLayer:update(bp_err, input, output) ltp:add(ltp, ltp, 1.0, -gconf.lrate * gconf.wcost) end -function nerv.AffineLayer:propagate(input, output) +function AffineLayer:propagate(input, output) -- apply linear transform output[1]:mul(input[1], self.ltp.trans, 1.0, 0.0, 'N', 'N') -- add bias output[1]:add_row(self.bp.trans, 1.0) end -function nerv.AffineLayer:back_propagate(next_bp_err, bp_err, input, output) +function AffineLayer:back_propagate(next_bp_err, bp_err, input, output) next_bp_err[1]:mul(bp_err[1], self.ltp.trans, 1.0, 0.0, 'N', 'T') end + +function AffineLayer:get_params() + return {self.ltp, self.bp} +end diff --git a/layer/bias.lua b/layer/bias.lua index 6ddfe11..8cd326b 100644 --- a/layer/bias.lua +++ b/layer/bias.lua @@ -22,3 +22,7 @@ function BiasLayer:propagate(input, output) output[1]:copy_fromd(input[1]) output[1]:add_row(self.bias.trans, 1.0) end + +function BiasLayer:get_params() + return {self.bias} +end diff --git a/layer/init.lua b/layer/init.lua index 38bcd7f..3011f8e 100644 --- a/layer/init.lua +++ b/layer/init.lua @@ -58,6 +58,10 @@ function Layer:check_dim_len(len_in, len_out) end end +function Layer:get_params() + nerv.error_method_not_implemented() +end + function Layer:get_dim() return self.dim_in, self.dim_out end diff --git a/layer/sigmoid.lua b/layer/sigmoid.lua index 220b7af..dd10fb9 100644 --- a/layer/sigmoid.lua +++ b/layer/sigmoid.lua @@ -25,3 +25,7 @@ end function SigmoidLayer:back_propagate(next_bp_err, bp_err, input, output) next_bp_err[1]:sigmoid_grad(bp_err[1], output[1]) end + +function SigmoidLayer:get_params() + return {} +end diff --git a/layer/softmax_ce.lua b/layer/softmax_ce.lua index cd57010..79e859e 100644 --- a/layer/softmax_ce.lua +++ b/layer/softmax_ce.lua @@ -50,3 +50,7 @@ function SoftmaxCELayer:back_propagate(next_bp_err, bp_err, input, output) end next_bp_err[1]:add(self.soutput, label, 1.0, -1.0) end + +function SoftmaxCELayer:get_params() + return {} +end diff --git a/layer/window.lua b/layer/window.lua index 8e9e761..b381c9b 100644 --- a/layer/window.lua +++ b/layer/window.lua @@ -22,3 +22,7 @@ function WindowLayer:propagate(input, output) output[1]:copy_fromd(input[1]) output[1]:scale_row(self.window.trans) end + +function WindowLayer:get_params() + return {self.window} +end diff --git a/nn/layer_dag.lua b/nn/layer_dag.lua index 3951bfa..2dda7c9 100644 --- a/nn/layer_dag.lua +++ b/nn/layer_dag.lua @@ -38,7 +38,7 @@ local function discover(id, layers, layer_repo) return ref end -function nerv.DAGLayer:__init(id, global_conf, layer_conf) +function DAGLayer:__init(id, global_conf, layer_conf) local layers = {} local inputs = {} local outputs = {} @@ -131,7 +131,7 @@ function nerv.DAGLayer:__init(id, global_conf, layer_conf) self.gconf = global_conf end -function nerv.DAGLayer:init(batch_size) -- topology sort +function DAGLayer:init(batch_size) -- topology sort for i, conn in ipairs(self.parsed_conn) do local _, output_dim local ref_from, port_from, ref_to, port_to @@ -174,7 +174,7 @@ function nerv.DAGLayer:init(batch_size) -- topology sort end end -function nerv.DAGLayer:set_inputs(input) +function DAGLayer:set_inputs(input) for i = 1, #self.dim_in do local layer = self.inputs[i][1] local port = self.inputs[i][2] @@ -182,7 +182,7 @@ function nerv.DAGLayer:set_inputs(input) end end -function nerv.DAGLayer:set_outputs(output) +function DAGLayer:set_outputs(output) for i = 1, #self.dim_out do local layer = self.outputs[i][1] local port = self.outputs[i][2] @@ -190,7 +190,7 @@ function nerv.DAGLayer:set_outputs(output) end end -function nerv.DAGLayer:set_err_inputs(bp_err) +function DAGLayer:set_err_inputs(bp_err) for i = 1, #self.dim_out do local layer = self.outputs[i][1] local port = self.outputs[i][2] @@ -198,7 +198,7 @@ function nerv.DAGLayer:set_err_inputs(bp_err) end end -function nerv.DAGLayer:set_err_outputs(next_bp_err) +function DAGLayer:set_err_outputs(next_bp_err) for i = 1, #self.dim_in do local layer = self.inputs[i][1] local port = self.inputs[i][2] @@ -206,7 +206,7 @@ function nerv.DAGLayer:set_err_outputs(next_bp_err) end end -function nerv.DAGLayer:update(bp_err, input, output) +function DAGLayer:update(bp_err, input, output) self:set_err_inputs(bp_err) self:set_inputs(input) self:set_outputs(output) @@ -217,7 +217,7 @@ function nerv.DAGLayer:update(bp_err, input, output) end end -function nerv.DAGLayer:propagate(input, output) +function DAGLayer:propagate(input, output) self:set_inputs(input) self:set_outputs(output) for i = 1, #self.queue do @@ -227,7 +227,7 @@ function nerv.DAGLayer:propagate(input, output) end end -function nerv.DAGLayer:back_propagate(next_bp_err, bp_err, input, output) +function DAGLayer:back_propagate(next_bp_err, bp_err, input, output) self:set_err_outputs(next_bp_err) self:set_err_inputs(bp_err) self:set_inputs(input) @@ -238,3 +238,13 @@ function nerv.DAGLayer:back_propagate(next_bp_err, bp_err, input, output) ref.layer:back_propagate(ref.err_outputs, ref.err_inputs, ref.inputs, ref.outputs) end end + +function DAGLayer:get_params() + local res = {} + for id, ref in pairs(self.queue) do + for i, p in ipairs(ref.layer:get_params()) do + table.insert(res, p) + end + end + return res +end -- cgit v1.2.3-70-g09d2