From 462d1982f299c8be4ae9a746e6ae7d04c04faa33 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 4 Aug 2015 14:41:22 +0800 Subject: add embedding_example --- embedding_example/.gitignore | 2 + embedding_example/Makefile | 9 +++ embedding_example/main.c | 95 ++++++++++++++++++++++++++ embedding_example/setup_nerv.lua | 26 +++++++ embedding_example/swb_baseline_decode.lua | 109 ++++++++++++++++++++++++++++++ 5 files changed, 241 insertions(+) create mode 100644 embedding_example/.gitignore create mode 100644 embedding_example/Makefile create mode 100644 embedding_example/main.c create mode 100644 embedding_example/setup_nerv.lua create mode 100644 embedding_example/swb_baseline_decode.lua (limited to 'embedding_example') diff --git a/embedding_example/.gitignore b/embedding_example/.gitignore new file mode 100644 index 0000000..8e68213 --- /dev/null +++ b/embedding_example/.gitignore @@ -0,0 +1,2 @@ +main +main.o diff --git a/embedding_example/Makefile b/embedding_example/Makefile new file mode 100644 index 0000000..e4ee314 --- /dev/null +++ b/embedding_example/Makefile @@ -0,0 +1,9 @@ +CFLAG += -I ../install/include/luajit-2.0/ -I ../install/include/nerv/ +LDFLAG += -L../install/lib/ -lluajit-5.1 -Wl,-rpath=../install/lib/ -lluaT -lnervcore +GCC := gcc + +main: main.o + $(GCC) -o $@ $< $(LDFLAG) + +main.o: main.c + $(GCC) $(CFLAG) -o $@ $< -c diff --git a/embedding_example/main.c b/embedding_example/main.c new file mode 100644 index 0000000..4e70892 --- /dev/null +++ b/embedding_example/main.c @@ -0,0 +1,95 @@ +#include "lua.h" +#include "lauxlib.h" +#include "lualib.h" +#include "matrix/matrix.h" +#include "common.h" +#include "luaT/luaT.h" +#include + +const char *nerv_matrix_host_float_tname = "nerv.MMatrixFloat"; +extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, Status *status); +extern void nerv_matrix_host_float_data_retain(Matrix *self); +extern void nerv_matrix_host_float_data_free(Matrix *self, Status *status); + +lua_State *L; +Matrix *input, *output; +Status status; + +void setup_nerv() { + L = lua_open(); + luaL_openlibs(L); + luaL_loadfile(L, "setup_nerv.lua"); + /* network configuration */ + lua_pushstring(L, "swb_baseline_decode.lua"); + if (lua_pcall(L, 1, LUA_MULTRET, 0)) + { + printf("%s\n", luaL_checkstring(L, 1)); + exit(1); + } + /* lua stack now: input width, output width, propagator */ + input = nerv_matrix_host_float_create(1, luaL_checkinteger(L, 1), &status); + NERV_LUA_CHECK_STATUS(L, status); + output = nerv_matrix_host_float_create(1, luaL_checkinteger(L, 2), &status); + NERV_LUA_CHECK_STATUS(L, status); +} + + +void propagate(float for_fun) { + int i, j; + printf("ok: %d\n", lua_gettop(L)); + lua_pushvalue(L, 3); + /* lua stack now: input width, output width, propagator, propagator */ + for (i = 0; i < input->nrow; i++) /* nrow is actually 1 */ + { + float *nerv_row = (float *)((char *)input->data.f + i * input->stride); + for (j = 0; j < input->ncol; j++) + { + nerv_row[j] = j * for_fun; + } + } + /* avoid gc */ + nerv_matrix_host_float_data_retain(input); + nerv_matrix_host_float_data_retain(input); + nerv_matrix_host_float_data_retain(input); + nerv_matrix_host_float_data_retain(input); + nerv_matrix_host_float_data_retain(output); + nerv_matrix_host_float_data_retain(output); + nerv_matrix_host_float_data_retain(output); + nerv_matrix_host_float_data_retain(output); + + luaT_pushudata(L, input, nerv_matrix_host_float_tname); + luaT_pushudata(L, output, nerv_matrix_host_float_tname); + /* lua stack now: input width, output width, propagator, propagator, input, output */ + if (lua_pcall(L, 2, 0, 0)) /* call propagator with two parameters */ + { + printf("%s\n", luaL_checkstring(L, -1)); + exit(-1); + } + /* lua stack now: input width, output width, propagator */ + printf("## caller ##\n"); + for (i = 0; i < output->nrow; i++) /* nrow is actually 1 */ + { + float *nerv_row = (float *)((char *)output->data.f + i * output->stride); + for (j = 0; j < output->ncol; j++) + { + printf("%.8f ", nerv_row[j]); + } + printf("\n"); + } +} + +void teardown_nerv() { + nerv_matrix_host_float_data_free(input, &status); + NERV_LUA_CHECK_STATUS(L, status); + nerv_matrix_host_float_data_free(output, &status); + NERV_LUA_CHECK_STATUS(L, status); +} + +int main() { + setup_nerv(); + propagate(1.0); + propagate(2.0); + propagate(3.0); + teardown_nerv(); + return 0; +} diff --git a/embedding_example/setup_nerv.lua b/embedding_example/setup_nerv.lua new file mode 100644 index 0000000..e33a1e7 --- /dev/null +++ b/embedding_example/setup_nerv.lua @@ -0,0 +1,26 @@ +package.path="/home/slhome/mfy43/.luarocks/share/lua/5.1/?.lua;/home/slhome/mfy43/.luarocks/share/lua/5.1/?/init.lua;/home/slhome/mfy43/nerv/install/share/lua/5.1/?.lua;/home/slhome/mfy43/nerv/install/share/lua/5.1/?/init.lua;"..package.path +package.cpath="/home/slhome/mfy43/.luarocks/lib/lua/5.1/?.so;/home/slhome/mfy43/nerv/install/lib/lua/5.1/?.so;"..package.cpath +local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("nerv","scm-1") + +local args = {...} +require 'nerv' +dofile(args[1]) +local param_repo = nerv.ParamRepo() +param_repo:import(gconf.initialized_param, nil, gconf) +local sublayer_repo = make_sublayer_repo(param_repo) +local layer_repo = make_layer_repo(sublayer_repo, param_repo) +local network = get_network(layer_repo) +local batch_size = 1 +network:init(batch_size) +function propagator(input, output) + local gpu_input = nerv.CuMatrixFloat(input:nrow(), input:ncol()) + local gpu_output = nerv.CuMatrixFloat(output:nrow(), output:ncol()) + gpu_input:copy_fromh(input) + print(gpu_input) + network:propagate({gpu_input}, {gpu_output}) + gpu_output:copy_toh(output) + print(output) + -- collect garbage in-time to save GPU memory + collectgarbage("collect") +end +return network.dim_in[1], network.dim_out[1], propagator diff --git a/embedding_example/swb_baseline_decode.lua b/embedding_example/swb_baseline_decode.lua new file mode 100644 index 0000000..14a463b --- /dev/null +++ b/embedding_example/swb_baseline_decode.lua @@ -0,0 +1,109 @@ +require 'htk_io' +gconf = {lrate = 0.8, wcost = 1e-6, momentum = 0.9, + cumat_type = nerv.CuMatrixFloat, + mmat_type = nerv.MMatrixFloat, + frm_ext = 5, + tr_scp = "/slfs1/users/mfy43/swb_ivec/train_bp.scp", + cv_scp = "/slfs1/users/mfy43/swb_ivec/train_cv.scp", + htk_conf = "/slfs1/users/mfy43/swb_ivec/plp_0_d_a.conf", + initialized_param = {"/slfs1/users/mfy43/swb_init.nerv", + "/slfs1/users/mfy43/swb_global_transf.nerv"}, + debug = false} + +function make_sublayer_repo(param_repo) + return nerv.LayerRepo( + { + -- global transf + ["nerv.BiasLayer"] = + { + blayer1 = {{bias = "bias1"}, {dim_in = {429}, dim_out = {429}}}, + blayer2 = {{bias = "bias2"}, {dim_in = {429}, dim_out = {429}}} + }, + ["nerv.WindowLayer"] = + { + wlayer1 = {{window = "window1"}, {dim_in = {429}, dim_out = {429}}}, + wlayer2 = {{window = "window2"}, {dim_in = {429}, dim_out = {429}}} + }, + -- biased linearity + ["nerv.AffineLayer"] = + { + affine0 = {{ltp = "affine0_ltp", bp = "affine0_bp"}, + {dim_in = {429}, dim_out = {2048}}}, + affine1 = {{ltp = "affine1_ltp", bp = "affine1_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine2 = {{ltp = "affine2_ltp", bp = "affine2_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine3 = {{ltp = "affine3_ltp", bp = "affine3_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine4 = {{ltp = "affine4_ltp", bp = "affine4_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine5 = {{ltp = "affine5_ltp", bp = "affine5_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine6 = {{ltp = "affine6_ltp", bp = "affine6_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine7 = {{ltp = "affine7_ltp", bp = "affine7_bp"}, + {dim_in = {2048}, dim_out = {3001}}} + }, + ["nerv.SigmoidLayer"] = + { + sigmoid0 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid1 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid2 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid3 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid4 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid5 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid6 = {{}, {dim_in = {2048}, dim_out = {2048}}} + }, + ["nerv.SoftmaxLayer"] = + { + soutput = {{}, {dim_in = {3001}, dim_out = {3001}}} + } + }, param_repo, gconf) +end + +function make_layer_repo(sublayer_repo, param_repo) + return nerv.LayerRepo( + { + ["nerv.DAGLayer"] = + { + global_transf = {{}, { + dim_in = {429}, dim_out = {429}, + sub_layers = sublayer_repo, + connections = { + ["[1]"] = "blayer1[1]", + ["blayer1[1]"] = "wlayer1[1]", + ["wlayer1[1]"] = "blayer2[1]", + ["blayer2[1]"] = "wlayer2[1]", + ["wlayer2[1]"] = "[1]" + } + }}, + main = {{}, { + dim_in = {429}, dim_out = {3001}, + sub_layers = sublayer_repo, + connections = { + ["[1]"] = "affine0[1]", + ["affine0[1]"] = "sigmoid0[1]", + ["sigmoid0[1]"] = "affine1[1]", + ["affine1[1]"] = "sigmoid1[1]", + ["sigmoid1[1]"] = "affine2[1]", + ["affine2[1]"] = "sigmoid2[1]", + ["sigmoid2[1]"] = "affine3[1]", + ["affine3[1]"] = "sigmoid3[1]", + ["sigmoid3[1]"] = "affine4[1]", + ["affine4[1]"] = "sigmoid4[1]", + ["sigmoid4[1]"] = "affine5[1]", + ["affine5[1]"] = "sigmoid5[1]", + ["sigmoid5[1]"] = "affine6[1]", + ["affine6[1]"] = "sigmoid6[1]", + ["sigmoid6[1]"] = "affine7[1]", + ["affine7[1]"] = "soutput[1]", + ["soutput[1]"] = "[1]" + } + }} + } + }, param_repo, gconf) +end + +function get_network(layer_repo) + return layer_repo:get_layer("main") +end -- cgit v1.2.3-70-g09d2 From 0223b6b88620d9235fc47818aaa5c86ae81d38d9 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 4 Aug 2015 14:42:08 +0800 Subject: ... --- embedding_example/main.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'embedding_example') diff --git a/embedding_example/main.c b/embedding_example/main.c index 4e70892..4c6459c 100644 --- a/embedding_example/main.c +++ b/embedding_example/main.c @@ -49,12 +49,6 @@ void propagate(float for_fun) { } /* avoid gc */ nerv_matrix_host_float_data_retain(input); - nerv_matrix_host_float_data_retain(input); - nerv_matrix_host_float_data_retain(input); - nerv_matrix_host_float_data_retain(input); - nerv_matrix_host_float_data_retain(output); - nerv_matrix_host_float_data_retain(output); - nerv_matrix_host_float_data_retain(output); nerv_matrix_host_float_data_retain(output); luaT_pushudata(L, input, nerv_matrix_host_float_tname); -- cgit v1.2.3-70-g09d2 From c3effaac9e9965371a73f9c84c2a4e0880f32138 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 4 Aug 2015 17:27:09 +0800 Subject: fix gc issues --- embedding_example/Makefile | 6 ++++++ embedding_example/main.c | 28 ++++++++++++++++------------ embedding_example/run.sh | 4 ++++ embedding_example/setup_nerv.lua | 9 ++++----- 4 files changed, 30 insertions(+), 17 deletions(-) create mode 100755 embedding_example/run.sh (limited to 'embedding_example') diff --git a/embedding_example/Makefile b/embedding_example/Makefile index e4ee314..3420b30 100644 --- a/embedding_example/Makefile +++ b/embedding_example/Makefile @@ -2,6 +2,12 @@ CFLAG += -I ../install/include/luajit-2.0/ -I ../install/include/nerv/ LDFLAG += -L../install/lib/ -lluajit-5.1 -Wl,-rpath=../install/lib/ -lluaT -lnervcore GCC := gcc +.PHONY: FORCE + +FORCE: ../install/bin/luarocks + echo "#!/bin/bash" > run.sh + $< path >> run.sh + echo "./main" >> run.sh main: main.o $(GCC) -o $@ $< $(LDFLAG) diff --git a/embedding_example/main.c b/embedding_example/main.c index 4c6459c..b3c9bf2 100644 --- a/embedding_example/main.c +++ b/embedding_example/main.c @@ -7,6 +7,8 @@ #include const char *nerv_matrix_host_float_tname = "nerv.MMatrixFloat"; +const char *input_name = "_nerv_embed_input"; +const char *output_name = "_nerv_embed_output"; extern Matrix *nerv_matrix_host_float_create(long nrow, long ncol, Status *status); extern void nerv_matrix_host_float_data_retain(Matrix *self); extern void nerv_matrix_host_float_data_free(Matrix *self, Status *status); @@ -31,6 +33,11 @@ void setup_nerv() { NERV_LUA_CHECK_STATUS(L, status); output = nerv_matrix_host_float_create(1, luaL_checkinteger(L, 2), &status); NERV_LUA_CHECK_STATUS(L, status); + /* add reference to avoid gc */ + luaT_pushudata(L, output, nerv_matrix_host_float_tname); + luaT_pushudata(L, input, nerv_matrix_host_float_tname); + lua_setfield(L, LUA_GLOBALSINDEX, input_name); + lua_setfield(L, LUA_GLOBALSINDEX, output_name); } @@ -47,12 +54,8 @@ void propagate(float for_fun) { nerv_row[j] = j * for_fun; } } - /* avoid gc */ - nerv_matrix_host_float_data_retain(input); - nerv_matrix_host_float_data_retain(output); - - luaT_pushudata(L, input, nerv_matrix_host_float_tname); - luaT_pushudata(L, output, nerv_matrix_host_float_tname); + lua_getfield(L, LUA_GLOBALSINDEX, input_name); + lua_getfield(L, LUA_GLOBALSINDEX, output_name); /* lua stack now: input width, output width, propagator, propagator, input, output */ if (lua_pcall(L, 2, 0, 0)) /* call propagator with two parameters */ { @@ -60,7 +63,7 @@ void propagate(float for_fun) { exit(-1); } /* lua stack now: input width, output width, propagator */ - printf("## caller ##\n"); + printf("## output: %ld %ld ##\n", output->nrow, output->ncol); for (i = 0; i < output->nrow; i++) /* nrow is actually 1 */ { float *nerv_row = (float *)((char *)output->data.f + i * output->stride); @@ -68,21 +71,22 @@ void propagate(float for_fun) { { printf("%.8f ", nerv_row[j]); } - printf("\n"); } } void teardown_nerv() { - nerv_matrix_host_float_data_free(input, &status); - NERV_LUA_CHECK_STATUS(L, status); - nerv_matrix_host_float_data_free(output, &status); - NERV_LUA_CHECK_STATUS(L, status); + lua_pushnil(L); + lua_pushnil(L); + lua_setfield(L, LUA_GLOBALSINDEX, input_name); + lua_setfield(L, LUA_GLOBALSINDEX, output_name); + lua_gc(L, LUA_GCCOLLECT, 0); } int main() { setup_nerv(); propagate(1.0); propagate(2.0); + propagate(2.0); propagate(3.0); teardown_nerv(); return 0; diff --git a/embedding_example/run.sh b/embedding_example/run.sh new file mode 100755 index 0000000..e919263 --- /dev/null +++ b/embedding_example/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash +export LUA_PATH='/home/slhome/mfy43/.luarocks/share/lua/5.1/?.lua;/home/slhome/mfy43/.luarocks/share/lua/5.1/?/init.lua;/home/slhome/mfy43/nerv/install/share/lua/5.1/?.lua;/home/slhome/mfy43/nerv/install/share/lua/5.1/?/init.lua;./?.lua;/usr/local/share/luajit-2.0.4/?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua' +export LUA_CPATH='/home/slhome/mfy43/.luarocks/lib/lua/5.1/?.so;/home/slhome/mfy43/nerv/install/lib/lua/5.1/?.so;./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so' +./main diff --git a/embedding_example/setup_nerv.lua b/embedding_example/setup_nerv.lua index e33a1e7..3ae878d 100644 --- a/embedding_example/setup_nerv.lua +++ b/embedding_example/setup_nerv.lua @@ -1,10 +1,7 @@ -package.path="/home/slhome/mfy43/.luarocks/share/lua/5.1/?.lua;/home/slhome/mfy43/.luarocks/share/lua/5.1/?/init.lua;/home/slhome/mfy43/nerv/install/share/lua/5.1/?.lua;/home/slhome/mfy43/nerv/install/share/lua/5.1/?/init.lua;"..package.path -package.cpath="/home/slhome/mfy43/.luarocks/lib/lua/5.1/?.so;/home/slhome/mfy43/nerv/install/lib/lua/5.1/?.so;"..package.cpath local k,l,_=pcall(require,"luarocks.loader") _=k and l.add_context("nerv","scm-1") - -local args = {...} require 'nerv' -dofile(args[1]) +local arg = {...} +dofile(arg[1]) local param_repo = nerv.ParamRepo() param_repo:import(gconf.initialized_param, nil, gconf) local sublayer_repo = make_sublayer_repo(param_repo) @@ -12,6 +9,7 @@ local layer_repo = make_layer_repo(sublayer_repo, param_repo) local network = get_network(layer_repo) local batch_size = 1 network:init(batch_size) + function propagator(input, output) local gpu_input = nerv.CuMatrixFloat(input:nrow(), input:ncol()) local gpu_output = nerv.CuMatrixFloat(output:nrow(), output:ncol()) @@ -23,4 +21,5 @@ function propagator(input, output) -- collect garbage in-time to save GPU memory collectgarbage("collect") end + return network.dim_in[1], network.dim_out[1], propagator -- cgit v1.2.3-70-g09d2 From 6aa0bb7b1ca5c92e50d6c7eeb1e3db2c16e71c3b Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 4 Aug 2015 17:32:30 +0800 Subject: ... --- embedding_example/Makefile | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'embedding_example') diff --git a/embedding_example/Makefile b/embedding_example/Makefile index 3420b30..73287f4 100644 --- a/embedding_example/Makefile +++ b/embedding_example/Makefile @@ -4,6 +4,11 @@ GCC := gcc .PHONY: FORCE +all: main FORCE +clean: + -rm -f *.o + -rm main + FORCE: ../install/bin/luarocks echo "#!/bin/bash" > run.sh $< path >> run.sh -- cgit v1.2.3-70-g09d2 From 7579ff4941d7019d4e911978879ec07b62a4e523 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 5 Aug 2015 09:29:24 +0800 Subject: use expanded features and do global transf in embedding_example --- embedding_example/setup_nerv.lua | 10 ++++++---- embedding_example/swb_baseline_decode.lua | 5 +++++ speech | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) (limited to 'embedding_example') diff --git a/embedding_example/setup_nerv.lua b/embedding_example/setup_nerv.lua index 3ae878d..49a5dd6 100644 --- a/embedding_example/setup_nerv.lua +++ b/embedding_example/setup_nerv.lua @@ -7,17 +7,19 @@ param_repo:import(gconf.initialized_param, nil, gconf) local sublayer_repo = make_sublayer_repo(param_repo) local layer_repo = make_layer_repo(sublayer_repo, param_repo) local network = get_network(layer_repo) +local global_transf = get_global_transf(layer_repo) local batch_size = 1 network:init(batch_size) function propagator(input, output) - local gpu_input = nerv.CuMatrixFloat(input:nrow(), input:ncol()) + local transformed = nerv.speech_utils.global_transf(input, + global_transf, 0, gconf) -- preprocessing + local gpu_input = nerv.CuMatrixFloat(transformed:nrow(), transformed:ncol()) local gpu_output = nerv.CuMatrixFloat(output:nrow(), output:ncol()) - gpu_input:copy_fromh(input) - print(gpu_input) + print(transformed) + gpu_input:copy_fromh(transformed) network:propagate({gpu_input}, {gpu_output}) gpu_output:copy_toh(output) - print(output) -- collect garbage in-time to save GPU memory collectgarbage("collect") end diff --git a/embedding_example/swb_baseline_decode.lua b/embedding_example/swb_baseline_decode.lua index 14a463b..8cdb320 100644 --- a/embedding_example/swb_baseline_decode.lua +++ b/embedding_example/swb_baseline_decode.lua @@ -107,3 +107,8 @@ end function get_network(layer_repo) return layer_repo:get_layer("main") end + + +function get_global_transf(layer_repo) + return layer_repo:get_layer("global_transf") +end diff --git a/speech b/speech index 261aed4..c6c6442 160000 --- a/speech +++ b/speech @@ -1 +1 @@ -Subproject commit 261aed43b863c85ecf709dcac2739c388ec491d2 +Subproject commit c6c644223dc9168befd189bf6f33243390671c99 -- cgit v1.2.3-70-g09d2 From b4d9cfa8e3a4735687311577dded97d889340134 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 6 Aug 2015 14:08:26 +0800 Subject: make network configuration example file clearer --- embedding_example/main.c | 2 +- embedding_example/setup_nerv.lua | 5 +- embedding_example/swb_baseline_decode.lua | 114 --------------------- nerv/doc/gh-pages | 1 + nerv/examples/asr_trainer.lua | 11 +- nerv/examples/swb_baseline.lua | 68 ++++++++++--- nerv/examples/swb_baseline_basic.lua | 161 ++++++++++++++++++++++++++++++ nerv/nn/layer_dag.lua | 3 +- nerv/nn/layer_repo.lua | 8 +- 9 files changed, 230 insertions(+), 143 deletions(-) delete mode 100644 embedding_example/swb_baseline_decode.lua create mode 160000 nerv/doc/gh-pages create mode 100644 nerv/examples/swb_baseline_basic.lua (limited to 'embedding_example') diff --git a/embedding_example/main.c b/embedding_example/main.c index b3c9bf2..8856d58 100644 --- a/embedding_example/main.c +++ b/embedding_example/main.c @@ -22,7 +22,7 @@ void setup_nerv() { luaL_openlibs(L); luaL_loadfile(L, "setup_nerv.lua"); /* network configuration */ - lua_pushstring(L, "swb_baseline_decode.lua"); + lua_pushstring(L, "../nerv/examples/swb_baseline.lua"); if (lua_pcall(L, 1, LUA_MULTRET, 0)) { printf("%s\n", luaL_checkstring(L, 1)); diff --git a/embedding_example/setup_nerv.lua b/embedding_example/setup_nerv.lua index 49a5dd6..5ade950 100644 --- a/embedding_example/setup_nerv.lua +++ b/embedding_example/setup_nerv.lua @@ -4,9 +4,8 @@ local arg = {...} dofile(arg[1]) local param_repo = nerv.ParamRepo() param_repo:import(gconf.initialized_param, nil, gconf) -local sublayer_repo = make_sublayer_repo(param_repo) -local layer_repo = make_layer_repo(sublayer_repo, param_repo) -local network = get_network(layer_repo) +local layer_repo = make_layer_repo(param_repo) +local network = get_decode_network(layer_repo) local global_transf = get_global_transf(layer_repo) local batch_size = 1 network:init(batch_size) diff --git a/embedding_example/swb_baseline_decode.lua b/embedding_example/swb_baseline_decode.lua deleted file mode 100644 index 8cdb320..0000000 --- a/embedding_example/swb_baseline_decode.lua +++ /dev/null @@ -1,114 +0,0 @@ -require 'htk_io' -gconf = {lrate = 0.8, wcost = 1e-6, momentum = 0.9, - cumat_type = nerv.CuMatrixFloat, - mmat_type = nerv.MMatrixFloat, - frm_ext = 5, - tr_scp = "/slfs1/users/mfy43/swb_ivec/train_bp.scp", - cv_scp = "/slfs1/users/mfy43/swb_ivec/train_cv.scp", - htk_conf = "/slfs1/users/mfy43/swb_ivec/plp_0_d_a.conf", - initialized_param = {"/slfs1/users/mfy43/swb_init.nerv", - "/slfs1/users/mfy43/swb_global_transf.nerv"}, - debug = false} - -function make_sublayer_repo(param_repo) - return nerv.LayerRepo( - { - -- global transf - ["nerv.BiasLayer"] = - { - blayer1 = {{bias = "bias1"}, {dim_in = {429}, dim_out = {429}}}, - blayer2 = {{bias = "bias2"}, {dim_in = {429}, dim_out = {429}}} - }, - ["nerv.WindowLayer"] = - { - wlayer1 = {{window = "window1"}, {dim_in = {429}, dim_out = {429}}}, - wlayer2 = {{window = "window2"}, {dim_in = {429}, dim_out = {429}}} - }, - -- biased linearity - ["nerv.AffineLayer"] = - { - affine0 = {{ltp = "affine0_ltp", bp = "affine0_bp"}, - {dim_in = {429}, dim_out = {2048}}}, - affine1 = {{ltp = "affine1_ltp", bp = "affine1_bp"}, - {dim_in = {2048}, dim_out = {2048}}}, - affine2 = {{ltp = "affine2_ltp", bp = "affine2_bp"}, - {dim_in = {2048}, dim_out = {2048}}}, - affine3 = {{ltp = "affine3_ltp", bp = "affine3_bp"}, - {dim_in = {2048}, dim_out = {2048}}}, - affine4 = {{ltp = "affine4_ltp", bp = "affine4_bp"}, - {dim_in = {2048}, dim_out = {2048}}}, - affine5 = {{ltp = "affine5_ltp", bp = "affine5_bp"}, - {dim_in = {2048}, dim_out = {2048}}}, - affine6 = {{ltp = "affine6_ltp", bp = "affine6_bp"}, - {dim_in = {2048}, dim_out = {2048}}}, - affine7 = {{ltp = "affine7_ltp", bp = "affine7_bp"}, - {dim_in = {2048}, dim_out = {3001}}} - }, - ["nerv.SigmoidLayer"] = - { - sigmoid0 = {{}, {dim_in = {2048}, dim_out = {2048}}}, - sigmoid1 = {{}, {dim_in = {2048}, dim_out = {2048}}}, - sigmoid2 = {{}, {dim_in = {2048}, dim_out = {2048}}}, - sigmoid3 = {{}, {dim_in = {2048}, dim_out = {2048}}}, - sigmoid4 = {{}, {dim_in = {2048}, dim_out = {2048}}}, - sigmoid5 = {{}, {dim_in = {2048}, dim_out = {2048}}}, - sigmoid6 = {{}, {dim_in = {2048}, dim_out = {2048}}} - }, - ["nerv.SoftmaxLayer"] = - { - soutput = {{}, {dim_in = {3001}, dim_out = {3001}}} - } - }, param_repo, gconf) -end - -function make_layer_repo(sublayer_repo, param_repo) - return nerv.LayerRepo( - { - ["nerv.DAGLayer"] = - { - global_transf = {{}, { - dim_in = {429}, dim_out = {429}, - sub_layers = sublayer_repo, - connections = { - ["[1]"] = "blayer1[1]", - ["blayer1[1]"] = "wlayer1[1]", - ["wlayer1[1]"] = "blayer2[1]", - ["blayer2[1]"] = "wlayer2[1]", - ["wlayer2[1]"] = "[1]" - } - }}, - main = {{}, { - dim_in = {429}, dim_out = {3001}, - sub_layers = sublayer_repo, - connections = { - ["[1]"] = "affine0[1]", - ["affine0[1]"] = "sigmoid0[1]", - ["sigmoid0[1]"] = "affine1[1]", - ["affine1[1]"] = "sigmoid1[1]", - ["sigmoid1[1]"] = "affine2[1]", - ["affine2[1]"] = "sigmoid2[1]", - ["sigmoid2[1]"] = "affine3[1]", - ["affine3[1]"] = "sigmoid3[1]", - ["sigmoid3[1]"] = "affine4[1]", - ["affine4[1]"] = "sigmoid4[1]", - ["sigmoid4[1]"] = "affine5[1]", - ["affine5[1]"] = "sigmoid5[1]", - ["sigmoid5[1]"] = "affine6[1]", - ["affine6[1]"] = "sigmoid6[1]", - ["sigmoid6[1]"] = "affine7[1]", - ["affine7[1]"] = "soutput[1]", - ["soutput[1]"] = "[1]" - } - }} - } - }, param_repo, gconf) -end - -function get_network(layer_repo) - return layer_repo:get_layer("main") -end - - -function get_global_transf(layer_repo) - return layer_repo:get_layer("global_transf") -end diff --git a/nerv/doc/gh-pages b/nerv/doc/gh-pages new file mode 160000 index 0000000..195d95b --- /dev/null +++ b/nerv/doc/gh-pages @@ -0,0 +1 @@ +Subproject commit 195d95bb663258e3b1c3962f946db9c374018a8c diff --git a/nerv/examples/asr_trainer.lua b/nerv/examples/asr_trainer.lua index 8dfb2ac..dcadfa3 100644 --- a/nerv/examples/asr_trainer.lua +++ b/nerv/examples/asr_trainer.lua @@ -1,8 +1,7 @@ function build_trainer(ifname) local param_repo = nerv.ParamRepo() param_repo:import(ifname, nil, gconf) - local sublayer_repo = make_sublayer_repo(param_repo) - local layer_repo = make_layer_repo(sublayer_repo, param_repo) + local layer_repo = make_layer_repo(param_repo) local network = get_network(layer_repo) local input_order = get_input_order() local iterative_trainer = function (prefix, scp_file, bp) @@ -18,7 +17,7 @@ function build_trainer(ifname) -- prine stat periodically gconf.cnt = gconf.cnt + 1 if gconf.cnt == 1000 then - print_stat(sublayer_repo) + print_stat(layer_repo) nerv.CuMatrix.print_profile() nerv.CuMatrix.clear_profile() gconf.cnt = 0 @@ -42,16 +41,16 @@ function build_trainer(ifname) -- collect garbage in-time to save GPU memory collectgarbage("collect") end - print_stat(sublayer_repo) + print_stat(layer_repo) nerv.CuMatrix.print_profile() nerv.CuMatrix.clear_profile() if (not bp) and prefix ~= nil then nerv.info("writing back...") local fname = string.format("%s_cv%.3f.nerv", - prefix, get_accuracy(sublayer_repo)) + prefix, get_accuracy(layer_repo)) network:get_params():export(fname, nil) end - return get_accuracy(sublayer_repo) + return get_accuracy(layer_repo) end return iterative_trainer end diff --git a/nerv/examples/swb_baseline.lua b/nerv/examples/swb_baseline.lua index 7783f2a..0e9f897 100644 --- a/nerv/examples/swb_baseline.lua +++ b/nerv/examples/swb_baseline.lua @@ -10,8 +10,8 @@ gconf = {lrate = 0.8, wcost = 1e-6, momentum = 0.9, "/slfs1/users/mfy43/swb_global_transf.nerv"}, debug = false} -function make_sublayer_repo(param_repo) - return nerv.LayerRepo( +function make_layer_repo(param_repo) + local layer_repo = nerv.LayerRepo( { -- global transf ["nerv.BiasLayer"] = @@ -54,21 +54,23 @@ function make_sublayer_repo(param_repo) sigmoid5 = {{}, {dim_in = {2048}, dim_out = {2048}}}, sigmoid6 = {{}, {dim_in = {2048}, dim_out = {2048}}} }, - ["nerv.SoftmaxCELayer"] = + ["nerv.SoftmaxCELayer"] = -- softmax + ce criterion layer for finetune output { ce_crit = {{}, {dim_in = {3001, 1}, dim_out = {1}, compressed = true}} + }, + ["nerv.SoftmaxLayer"] = -- softmax for decode output + { + softmax = {{}, {dim_in = {3001}, dim_out = {3001}}} } }, param_repo, gconf) -end -function make_layer_repo(sublayer_repo, param_repo) - return nerv.LayerRepo( + layer_repo:add_layers( { ["nerv.DAGLayer"] = { global_transf = {{}, { dim_in = {429}, dim_out = {429}, - sub_layers = sublayer_repo, + sub_layers = layer_repo, connections = { ["[1]"] = "blayer1[1]", ["blayer1[1]"] = "wlayer1[1]", @@ -78,8 +80,8 @@ function make_layer_repo(sublayer_repo, param_repo) } }}, main = {{}, { - dim_in = {429, 1}, dim_out = {1}, - sub_layers = sublayer_repo, + dim_in = {429}, dim_out = {3001}, + sub_layers = layer_repo, connections = { ["[1]"] = "affine0[1]", ["affine0[1]"] = "sigmoid0[1]", @@ -96,17 +98,51 @@ function make_layer_repo(sublayer_repo, param_repo) ["sigmoid5[1]"] = "affine6[1]", ["affine6[1]"] = "sigmoid6[1]", ["sigmoid6[1]"] = "affine7[1]", - ["affine7[1]"] = "ce_crit[1]", + ["affine7[1]"] = "[1]" + } + }} + } + }, param_repo, gconf) + + layer_repo:add_layers( + { + ["nerv.DAGLayer"] = + { + ce_output = {{}, { + dim_in = {429, 1}, dim_out = {1}, + sub_layers = layer_repo, + connections = { + ["[1]"] = "main[1]", + ["main[1]"] = "ce_crit[1]", ["[2]"] = "ce_crit[2]", ["ce_crit[1]"] = "[1]" } + }}, + softmax_output = {{}, { + dim_in = {429}, dim_out = {3001}, + sub_layers = layer_repo, + connections = { + ["[1]"] = "main[1]", + ["main[1]"] = "softmax[1]", + ["softmax[1]"] = "[1]" + } }} } }, param_repo, gconf) + + return layer_repo end function get_network(layer_repo) - return layer_repo:get_layer("main") + return layer_repo:get_layer("ce_output") +end + +function get_decode_network(layer_repo) + return layer_repo:get_layer("softmax_output") +end + +function get_global_transf(layer_repo) + return layer_repo:get_layer("global_transf") end function make_readers(scp_file, layer_repo) @@ -145,18 +181,18 @@ function get_input_order() return {"main_scp", "phone_state"} end -function get_accuracy(sublayer_repo) - local ce_crit = sublayer_repo:get_layer("ce_crit") +function get_accuracy(layer_repo) + local ce_crit = layer_repo:get_layer("ce_crit") return ce_crit.total_correct / ce_crit.total_frames * 100 end -function print_stat(sublayer_repo) - local ce_crit = sublayer_repo:get_layer("ce_crit") +function print_stat(layer_repo) + local ce_crit = layer_repo:get_layer("ce_crit") nerv.info("*** training stat begin ***") nerv.printf("cross entropy:\t\t%.8f\n", ce_crit.total_ce) nerv.printf("correct:\t\t%d\n", ce_crit.total_correct) nerv.printf("frames:\t\t\t%d\n", ce_crit.total_frames) nerv.printf("err/frm:\t\t%.8f\n", ce_crit.total_ce / ce_crit.total_frames) - nerv.printf("accuracy:\t\t%.3f%%\n", get_accuracy(sublayer_repo)) + nerv.printf("accuracy:\t\t%.3f%%\n", get_accuracy(layer_repo)) nerv.info("*** training stat end ***") end diff --git a/nerv/examples/swb_baseline_basic.lua b/nerv/examples/swb_baseline_basic.lua new file mode 100644 index 0000000..c47ec3e --- /dev/null +++ b/nerv/examples/swb_baseline_basic.lua @@ -0,0 +1,161 @@ +require 'htk_io' +gconf = {lrate = 0.8, wcost = 1e-6, momentum = 0.9, + cumat_type = nerv.CuMatrixFloat, + mmat_type = nerv.MMatrixFloat, + frm_ext = 5, + tr_scp = "/slfs1/users/mfy43/swb_ivec/train_bp.scp", + cv_scp = "/slfs1/users/mfy43/swb_ivec/train_cv.scp", + htk_conf = "/slfs1/users/mfy43/swb_ivec/plp_0_d_a.conf", + initialized_param = {"/slfs1/users/mfy43/swb_init.nerv", + "/slfs1/users/mfy43/swb_global_transf.nerv"}, + debug = false} + +function make_layer_repo(param_repo) + local layer_repo = nerv.LayerRepo( + { + -- global transf + ["nerv.BiasLayer"] = + { + blayer1 = {{bias = "bias1"}, {dim_in = {429}, dim_out = {429}}}, + blayer2 = {{bias = "bias2"}, {dim_in = {429}, dim_out = {429}}} + }, + ["nerv.WindowLayer"] = + { + wlayer1 = {{window = "window1"}, {dim_in = {429}, dim_out = {429}}}, + wlayer2 = {{window = "window2"}, {dim_in = {429}, dim_out = {429}}} + }, + -- biased linearity + ["nerv.AffineLayer"] = + { + affine0 = {{ltp = "affine0_ltp", bp = "affine0_bp"}, + {dim_in = {429}, dim_out = {2048}}}, + affine1 = {{ltp = "affine1_ltp", bp = "affine1_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine2 = {{ltp = "affine2_ltp", bp = "affine2_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine3 = {{ltp = "affine3_ltp", bp = "affine3_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine4 = {{ltp = "affine4_ltp", bp = "affine4_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine5 = {{ltp = "affine5_ltp", bp = "affine5_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine6 = {{ltp = "affine6_ltp", bp = "affine6_bp"}, + {dim_in = {2048}, dim_out = {2048}}}, + affine7 = {{ltp = "affine7_ltp", bp = "affine7_bp"}, + {dim_in = {2048}, dim_out = {3001}}} + }, + ["nerv.SigmoidLayer"] = + { + sigmoid0 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid1 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid2 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid3 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid4 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid5 = {{}, {dim_in = {2048}, dim_out = {2048}}}, + sigmoid6 = {{}, {dim_in = {2048}, dim_out = {2048}}} + }, + ["nerv.SoftmaxCELayer"] = + { + ce_crit = {{}, {dim_in = {3001, 1}, dim_out = {1}, compressed = true}} + } + }, param_repo, gconf) + + layer_repo:add_layers( + { + ["nerv.DAGLayer"] = + { + global_transf = {{}, { + dim_in = {429}, dim_out = {429}, + sub_layers = layer_repo, + connections = { + ["[1]"] = "blayer1[1]", + ["blayer1[1]"] = "wlayer1[1]", + ["wlayer1[1]"] = "blayer2[1]", + ["blayer2[1]"] = "wlayer2[1]", + ["wlayer2[1]"] = "[1]" + } + }}, + main = {{}, { + dim_in = {429, 1}, dim_out = {1}, + sub_layers = layer_repo, + connections = { + ["[1]"] = "affine0[1]", + ["affine0[1]"] = "sigmoid0[1]", + ["sigmoid0[1]"] = "affine1[1]", + ["affine1[1]"] = "sigmoid1[1]", + ["sigmoid1[1]"] = "affine2[1]", + ["affine2[1]"] = "sigmoid2[1]", + ["sigmoid2[1]"] = "affine3[1]", + ["affine3[1]"] = "sigmoid3[1]", + ["sigmoid3[1]"] = "affine4[1]", + ["affine4[1]"] = "sigmoid4[1]", + ["sigmoid4[1]"] = "affine5[1]", + ["affine5[1]"] = "sigmoid5[1]", + ["sigmoid5[1]"] = "affine6[1]", + ["affine6[1]"] = "sigmoid6[1]", + ["sigmoid6[1]"] = "affine7[1]", + ["affine7[1]"] = "ce_crit[1]", + ["[2]"] = "ce_crit[2]", + ["ce_crit[1]"] = "[1]" + } + }} + } + }, param_repo, gconf) + return layer_repo +end + +function get_network(layer_repo) + return layer_repo:get_layer("main") +end + +function make_readers(scp_file, layer_repo) + return { + {reader = nerv.TNetReader(gconf, + { + id = "main_scp", + scp_file = scp_file, + conf_file = gconf.htk_conf, + frm_ext = gconf.frm_ext, + mlfs = { + phone_state = { + file = "/slfs1/users/mfy43/swb_ivec/ref.mlf", + format = "map", + format_arg = "/slfs1/users/mfy43/swb_ivec/dict", + dir = "*/", + ext = "lab" + } + }, + global_transf = layer_repo:get_layer("global_transf") + }), + data = {main_scp = 429, phone_state = 1}} + } +end + +function make_buffer(readers) + return nerv.SGDBuffer(gconf, + { + buffer_size = gconf.buffer_size, + randomize = gconf.randomize, + readers = readers + }) +end + +function get_input_order() + return {"main_scp", "phone_state"} +end + +function get_accuracy(layer_repo) + local ce_crit = layer_repo:get_layer("ce_crit") + return ce_crit.total_correct / ce_crit.total_frames * 100 +end + +function print_stat(layer_repo) + local ce_crit = layer_repo:get_layer("ce_crit") + nerv.info("*** training stat begin ***") + nerv.printf("cross entropy:\t\t%.8f\n", ce_crit.total_ce) + nerv.printf("correct:\t\t%d\n", ce_crit.total_correct) + nerv.printf("frames:\t\t\t%d\n", ce_crit.total_frames) + nerv.printf("err/frm:\t\t%.8f\n", ce_crit.total_ce / ce_crit.total_frames) + nerv.printf("accuracy:\t\t%.3f%%\n", get_accuracy(layer_repo)) + nerv.info("*** training stat end ***") +end diff --git a/nerv/nn/layer_dag.lua b/nerv/nn/layer_dag.lua index e5c1ac7..e9d4d86 100644 --- a/nerv/nn/layer_dag.lua +++ b/nerv/nn/layer_dag.lua @@ -112,7 +112,7 @@ function DAGLayer:__init(id, global_conf, layer_conf) end end for i = 1, #queue do - nerv.info("enqueued layer: %s", queue[i].layer.id) + nerv.info("enqueued layer: %s %s", queue[i].layer, queue[i].layer.id) end for id, ref in pairs(layers) do @@ -125,6 +125,7 @@ function DAGLayer:__init(id, global_conf, layer_conf) self.layers = layers self.inputs = inputs self.outputs = outputs + self.id = id self.dim_in = dim_in self.dim_out = dim_out self.parsed_conn = parsed_conn diff --git a/nerv/nn/layer_repo.lua b/nerv/nn/layer_repo.lua index 602c37c..ef333a7 100644 --- a/nerv/nn/layer_repo.lua +++ b/nerv/nn/layer_repo.lua @@ -1,7 +1,12 @@ local LayerRepo = nerv.class("nerv.LayerRepo") function LayerRepo:__init(layer_spec, param_repo, global_conf) - local layers = {} + self.layers = {} + self:add_layers(layer_spec, param_repo, global_conf) +end + +function LayerRepo:add_layers(layer_spec, param_repo, global_conf) + local layers = self.layers for ltype, llist in pairs(layer_spec) do local layer_type = nerv.get_type(ltype) for id, spec in pairs(llist) do @@ -22,7 +27,6 @@ function LayerRepo:__init(layer_spec, param_repo, global_conf) layers[id] = layer_type(id, global_conf, layer_config) end end - self.layers = layers end function LayerRepo:get_layer(lid) -- cgit v1.2.3-70-g09d2 From 8bf9c7575ffeeabb3924e9e02a35afe187071fe2 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 27 Aug 2015 17:38:09 +0800 Subject: update embedding example to use new global_transf() --- embedding_example/setup_nerv.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'embedding_example') diff --git a/embedding_example/setup_nerv.lua b/embedding_example/setup_nerv.lua index 5ade950..d80c306 100644 --- a/embedding_example/setup_nerv.lua +++ b/embedding_example/setup_nerv.lua @@ -11,12 +11,11 @@ local batch_size = 1 network:init(batch_size) function propagator(input, output) - local transformed = nerv.speech_utils.global_transf(input, - global_transf, 0, gconf) -- preprocessing - local gpu_input = nerv.CuMatrixFloat(transformed:nrow(), transformed:ncol()) + local transformed = nerv.speech_utils.global_transf( + gconf.cumat_type.new_from_host(input), + global_transf, 0, 0, gconf) -- preprocessing + local gpu_input = transformed local gpu_output = nerv.CuMatrixFloat(output:nrow(), output:ncol()) - print(transformed) - gpu_input:copy_fromh(transformed) network:propagate({gpu_input}, {gpu_output}) gpu_output:copy_toh(output) -- collect garbage in-time to save GPU memory -- cgit v1.2.3-70-g09d2