From 2497fd9e7a0fae5ee4887890d7a312e0e08a93b8 Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 22 Jun 2015 19:01:29 +0800 Subject: major change: use luarocks to manage project --- .gitignore | 2 +- .gitmodules | 3 + Makefile | 71 +- build_luajit.sh | 5 - common.c | 76 --- common.h | 36 -- doc/nerv.md | 17 - doc/nerv_class.md | 36 -- doc/nerv_io.md | 113 ---- doc/nerv_layer.md | 180 ------ doc/nerv_matrix.md | 165 ----- doc/nerv_nn.md | 256 -------- doc/nerv_param.md | 27 - examples/asr_trainer.lua | 106 --- examples/chunk_file_example.lua | 53 -- examples/cumatrix_example.lua | 31 - examples/cumatrix_from_mmatrix.lua | 32 - examples/mmatrix_example.lua | 20 - examples/oop_example.c | 101 --- examples/oop_example.lua | 16 - examples/swb_baseline.lua | 166 ----- examples/test_dnn_layers.lua | 78 --- examples/test_nn_lib.lua | 164 ----- io/chunk_file.c | 325 ---------- io/chunk_file.h | 23 - io/init.c | 6 - io/init.lua | 55 -- io/sgd_buffer.lua | 111 ---- layer/affine.lua | 91 --- layer/bias.lua | 28 - layer/combiner.lua | 59 -- layer/init.lua | 79 --- layer/mse.lua | 52 -- layer/sigmoid.lua | 31 - layer/softmax_ce.lua | 68 -- layer/window.lua | 28 - luaT/README.md | 239 ------- luaT/luaT.c | 1079 ------------------------------- luaT/luaT.h | 111 ---- luarocks | 1 + matrix/cuda_helper.h | 75 --- matrix/cukernel.cu | 17 - matrix/cukernel.h | 20 - matrix/cumatrix.c | 87 --- matrix/generic/cukernel.cu | 571 ---------------- matrix/generic/cumatrix.c | 493 -------------- matrix/generic/elem_type.h | 22 - matrix/generic/matrix.c | 155 ----- matrix/generic/matrix.h | 19 - matrix/generic/mmatrix.c | 122 ---- matrix/init.c | 35 - matrix/init.lua | 77 --- matrix/mmatrix.c | 77 --- nerv | 2 - nerv.c | 38 -- nerv.lua | 114 ---- nerv/.gitignore | 1 + nerv/Makefile | 60 ++ nerv/common.c | 76 +++ nerv/common.h | 36 ++ nerv/doc/nerv.md | 17 + nerv/doc/nerv_class.md | 36 ++ nerv/doc/nerv_io.md | 113 ++++ nerv/doc/nerv_layer.md | 180 ++++++ nerv/doc/nerv_matrix.md | 165 +++++ nerv/doc/nerv_nn.md | 256 ++++++++ nerv/doc/nerv_param.md | 27 + nerv/examples/asr_trainer.lua | 106 +++ nerv/examples/chunk_file_example.lua | 53 ++ nerv/examples/cumatrix_example.lua | 31 + nerv/examples/cumatrix_from_mmatrix.lua | 32 + nerv/examples/mmatrix_example.lua | 20 + nerv/examples/oop_example.c | 101 +++ nerv/examples/oop_example.lua | 16 + nerv/examples/swb_baseline.lua | 166 +++++ nerv/examples/test_dnn_layers.lua | 78 +++ nerv/examples/test_nn_lib.lua | 164 +++++ nerv/init.lua | 128 ++++ nerv/io/chunk_file.c | 325 ++++++++++ nerv/io/chunk_file.h | 23 + nerv/io/init.c | 6 + nerv/io/init.lua | 55 ++ nerv/io/sgd_buffer.lua | 111 ++++ nerv/layer/affine.lua | 91 +++ nerv/layer/bias.lua | 28 + nerv/layer/combiner.lua | 59 ++ nerv/layer/init.lua | 79 +++ nerv/layer/mse.lua | 52 ++ nerv/layer/sigmoid.lua | 31 + nerv/layer/softmax_ce.lua | 68 ++ nerv/layer/window.lua | 28 + nerv/luaT/README.md | 239 +++++++ nerv/luaT/luaT.c | 1079 +++++++++++++++++++++++++++++++ nerv/luaT/luaT.h | 111 ++++ nerv/matrix/cuda_helper.h | 75 +++ nerv/matrix/cukernel.cu | 17 + nerv/matrix/cukernel.h | 20 + nerv/matrix/cumatrix.c | 87 +++ nerv/matrix/generic/cukernel.cu | 571 ++++++++++++++++ nerv/matrix/generic/cumatrix.c | 493 ++++++++++++++ nerv/matrix/generic/elem_type.h | 22 + nerv/matrix/generic/matrix.c | 155 +++++ nerv/matrix/generic/matrix.h | 19 + nerv/matrix/generic/mmatrix.c | 122 ++++ nerv/matrix/init.c | 35 + nerv/matrix/init.lua | 77 +++ nerv/matrix/mmatrix.c | 77 +++ nerv/nerv | 13 + nerv/nerv-scm-1.rockspec | 38 ++ nerv/nerv.c | 38 ++ nerv/nn/init.lua | 3 + nerv/nn/layer_dag.lua | 249 +++++++ nerv/nn/layer_repo.lua | 34 + nerv/nn/param_repo.lua | 76 +++ nn/init.lua | 3 - nn/layer_dag.lua | 249 ------- nn/layer_repo.lua | 34 - nn/param_repo.lua | 76 --- pl/compat.lua | 137 ---- pl/utils.lua | 476 -------------- speech | 2 +- tools/build_luajit.sh | 4 + tools/build_luarocks.sh | 4 + 123 files changed, 6491 insertions(+), 7026 deletions(-) delete mode 100755 build_luajit.sh delete mode 100644 common.c delete mode 100644 common.h delete mode 100644 doc/nerv.md delete mode 100644 doc/nerv_class.md delete mode 100644 doc/nerv_io.md delete mode 100644 doc/nerv_layer.md delete mode 100644 doc/nerv_matrix.md delete mode 100644 doc/nerv_nn.md delete mode 100644 doc/nerv_param.md delete mode 100644 examples/asr_trainer.lua delete mode 100644 examples/chunk_file_example.lua delete mode 100644 examples/cumatrix_example.lua delete mode 100644 examples/cumatrix_from_mmatrix.lua delete mode 100644 examples/mmatrix_example.lua delete mode 100644 examples/oop_example.c delete mode 100644 examples/oop_example.lua delete mode 100644 examples/swb_baseline.lua delete mode 100644 examples/test_dnn_layers.lua delete mode 100644 examples/test_nn_lib.lua delete mode 100644 io/chunk_file.c delete mode 100644 io/chunk_file.h delete mode 100644 io/init.c delete mode 100644 io/init.lua delete mode 100644 io/sgd_buffer.lua delete mode 100644 layer/affine.lua delete mode 100644 layer/bias.lua delete mode 100644 layer/combiner.lua delete mode 100644 layer/init.lua delete mode 100644 layer/mse.lua delete mode 100644 layer/sigmoid.lua delete mode 100644 layer/softmax_ce.lua delete mode 100644 layer/window.lua delete mode 100644 luaT/README.md delete mode 100644 luaT/luaT.c delete mode 100644 luaT/luaT.h create mode 160000 luarocks delete mode 100644 matrix/cuda_helper.h delete mode 100644 matrix/cukernel.cu delete mode 100644 matrix/cukernel.h delete mode 100644 matrix/cumatrix.c delete mode 100644 matrix/generic/cukernel.cu delete mode 100644 matrix/generic/cumatrix.c delete mode 100644 matrix/generic/elem_type.h delete mode 100644 matrix/generic/matrix.c delete mode 100644 matrix/generic/matrix.h delete mode 100644 matrix/generic/mmatrix.c delete mode 100644 matrix/init.c delete mode 100644 matrix/init.lua delete mode 100644 matrix/mmatrix.c delete mode 100755 nerv delete mode 100644 nerv.c delete mode 100644 nerv.lua create mode 100644 nerv/.gitignore create mode 100644 nerv/Makefile create mode 100644 nerv/common.c create mode 100644 nerv/common.h create mode 100644 nerv/doc/nerv.md create mode 100644 nerv/doc/nerv_class.md create mode 100644 nerv/doc/nerv_io.md create mode 100644 nerv/doc/nerv_layer.md create mode 100644 nerv/doc/nerv_matrix.md create mode 100644 nerv/doc/nerv_nn.md create mode 100644 nerv/doc/nerv_param.md create mode 100644 nerv/examples/asr_trainer.lua create mode 100644 nerv/examples/chunk_file_example.lua create mode 100644 nerv/examples/cumatrix_example.lua create mode 100644 nerv/examples/cumatrix_from_mmatrix.lua create mode 100644 nerv/examples/mmatrix_example.lua create mode 100644 nerv/examples/oop_example.c create mode 100644 nerv/examples/oop_example.lua create mode 100644 nerv/examples/swb_baseline.lua create mode 100644 nerv/examples/test_dnn_layers.lua create mode 100644 nerv/examples/test_nn_lib.lua create mode 100644 nerv/init.lua create mode 100644 nerv/io/chunk_file.c create mode 100644 nerv/io/chunk_file.h create mode 100644 nerv/io/init.c create mode 100644 nerv/io/init.lua create mode 100644 nerv/io/sgd_buffer.lua create mode 100644 nerv/layer/affine.lua create mode 100644 nerv/layer/bias.lua create mode 100644 nerv/layer/combiner.lua create mode 100644 nerv/layer/init.lua create mode 100644 nerv/layer/mse.lua create mode 100644 nerv/layer/sigmoid.lua create mode 100644 nerv/layer/softmax_ce.lua create mode 100644 nerv/layer/window.lua create mode 100644 nerv/luaT/README.md create mode 100644 nerv/luaT/luaT.c create mode 100644 nerv/luaT/luaT.h create mode 100644 nerv/matrix/cuda_helper.h create mode 100644 nerv/matrix/cukernel.cu create mode 100644 nerv/matrix/cukernel.h create mode 100644 nerv/matrix/cumatrix.c create mode 100644 nerv/matrix/generic/cukernel.cu create mode 100644 nerv/matrix/generic/cumatrix.c create mode 100644 nerv/matrix/generic/elem_type.h create mode 100644 nerv/matrix/generic/matrix.c create mode 100644 nerv/matrix/generic/matrix.h create mode 100644 nerv/matrix/generic/mmatrix.c create mode 100644 nerv/matrix/init.c create mode 100644 nerv/matrix/init.lua create mode 100644 nerv/matrix/mmatrix.c create mode 100644 nerv/nerv create mode 100644 nerv/nerv-scm-1.rockspec create mode 100644 nerv/nerv.c create mode 100644 nerv/nn/init.lua create mode 100644 nerv/nn/layer_dag.lua create mode 100644 nerv/nn/layer_repo.lua create mode 100644 nerv/nn/param_repo.lua delete mode 100644 nn/init.lua delete mode 100644 nn/layer_dag.lua delete mode 100644 nn/layer_repo.lua delete mode 100644 nn/param_repo.lua delete mode 100644 pl/compat.lua delete mode 100644 pl/utils.lua create mode 100755 tools/build_luajit.sh create mode 100755 tools/build_luarocks.sh diff --git a/.gitignore b/.gitignore index d8e6051..24f2f11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.o -build/ +install/ *.swp *.swo diff --git a/.gitmodules b/.gitmodules index 2bb1788..1432de9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "speech"] path = speech url = https://github.com/Determinant/nerv-speech.git +[submodule "luarocks"] + path = luarocks + url = https://github.com/keplerproject/luarocks.git diff --git a/Makefile b/Makefile index 8f1d491..4282d6d 100644 --- a/Makefile +++ b/Makefile @@ -1,63 +1,10 @@ -.PHONY: all clean luajit -BUILD_DIR := $(CURDIR)/build -OBJS := nerv.o luaT.o common.o \ - matrix/mmatrix.o matrix/cumatrix.o matrix/init.o matrix/cukernel.o \ - io/init.o io/chunk_file.o \ - examples/oop_example.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/window.lua layer/bias.lua layer/combiner.lua layer/mse.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-5.0 -CUDA_INCLUDE := -I $(CUDA_BASE)/include/ -INCLUDE += $(CUDA_INCLUDE) -LDFLAGS := -L$(CUDA_BASE)/lib64/ -Wl,-rpath=$(CUDA_BASE)/lib64/ -lcudart -lcublas -CFLAGS := -Wall -Wextra -O2 -OBJ_DIR := $(BUILD_DIR)/objs -LUA_DIR := $(BUILD_DIR)/lua -LIB_DIR := $(BUILD_DIR)/lib -SUBDIR := matrix io layer examples pl nn -NVCC := $(CUDA_BASE)/bin/nvcc -NVCC_FLAGS := -Xcompiler -fPIC,-Wall,-Wextra - -OBJS := $(addprefix $(OBJ_DIR)/,$(OBJS)) -OBJ_SUBDIR := $(addprefix $(OBJ_DIR)/,$(SUBDIR)) -LUA_SUBDIR := $(addprefix $(LUA_DIR)/,$(SUBDIR)) -LIBS := $(addprefix $(BUILD_DIR)/lib/,$(LIBS)) -LUA_LIBS := $(addprefix $(LUA_DIR)/,$(LUA_LIBS)) - -all: luajit $(OBJ_DIR) $(LIB_DIR) $(OBJ_SUBDIR) $(LIBS) $(LUA_DIR) $(LUA_SUBDIR) $(LUA_LIBS) +.PHONY: all clean install luajit luarocks nerv +SHELL := /bin/bash +PREFIX := $(CURDIR)/install/ +all: luajit luarocks luajit: - ./build_luajit.sh -$(OBJ_DIR) $(LIB_DIR) $(LUA_DIR) $(OBJ_SUBDIR) $(LUA_SUBDIR): - -mkdir -p $@ -$(OBJ_DIR)/%.o: %.c $(patsubst /%.o,/%.c,$@) - gcc -c -o $@ $< $(INCLUDE) -fPIC $(CFLAGS) -$(OBJ_DIR)/matrix/cukernel.o: matrix/cukernel.cu - $(NVCC) -c -o $@ $< $(INCLUDE) $(NVCC_FLAGS) -$(LUA_DIR)/%.lua: %.lua - cp $< $@ -$(OBJ_DIR)/luaT.o: - gcc -c -o $@ luaT/luaT.c $(INCLUDE) -fPIC -$(LIBS): $(OBJS) - gcc -shared -o $@ $(OBJS) $(LDFLAGS) - -$(OBJ_DIR)/matrix/cumatrix.o: matrix/generic/cumatrix.c matrix/generic/matrix.c matrix/generic/cukernel.cu -$(OBJ_DIR)/matrix/mmatrix.o: matrix/generic/mmatrix.c matrix/generic/matrix.c -$(OBJ_DIR)/matrix/cukernel.o: matrix/generic/cukernel.cu - -.PHONY: speech - -speech: - -mkdir -p build/objs/speech/tnet_io - $(MAKE) -C speech/ BUILD_DIR=$(BUILD_DIR) LIB_DIR=$(LIB_DIR) OBJ_DIR=$(CURDIR)/build/objs/speech/ LUA_DIR=$(LUA_DIR) - -clean: - -rm -rf $(OBJ_DIR) - -rm -rf $(LUA_DIR) - -rm -rf $(LIB_DIR) + PREFIX=$(PREFIX) ./tools/build_luajit.sh +luarocks: + PREFIX=$(PREFIX) ./tools/build_luarocks.sh +install: + cd nerv; $(PREFIX)/bin/luarocks make diff --git a/build_luajit.sh b/build_luajit.sh deleted file mode 100755 index bb23b5f..0000000 --- a/build_luajit.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -PREFIX="${PWD}/build" -LUAJIT_SRC='luajit-2.0/' -LUAJIT_PREFIX="${PREFIX}/luajit-2.0" -[[ -d $LUAJIT_PREFIX ]] || (cd "$LUAJIT_SRC"; make && make PREFIX="$LUAJIT_PREFIX" install) diff --git a/common.c b/common.c deleted file mode 100644 index b4e39e6..0000000 --- a/common.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "common.h" -#include -int nerv_error(lua_State *L, const char *err_mesg_fmt, ...) { - va_list ap; - va_start(ap, err_mesg_fmt); - lua_pushstring(L, "[nerv] internal error: "); - lua_pushvfstring(L, err_mesg_fmt, ap); - lua_concat(L, 2); - lua_error(L); - va_end(ap); - return 0; -} - -int nerv_error_method_not_implemented(lua_State *L) { - return nerv_error(L, "method not implemented"); -} - -void luaN_append_methods(lua_State *L, const luaL_Reg *mlist) { - for (; mlist->func; mlist++) - { - lua_pushcfunction(L, mlist->func); - lua_setfield(L, -2, mlist->name); - } -} - -HashMap *hashmap_create(size_t size, HashKey_t hfunc, HashMapCmp_t cmp) { - HashMap *res = (HashMap *)malloc(sizeof(HashMap)); - res->bucket = calloc(size, sizeof(HashNode)); - res->cmp = cmp; - res->hfunc = hfunc; - res->size = size; - return res; -} - -void *hashmap_getval(HashMap *h, const char *key) { - size_t idx = h->hfunc(key) % h->size; - HashNode *ptr; - for (ptr = h->bucket[idx]; ptr; ptr = ptr->next) - { - if (!h->cmp(ptr->key, key)) - return ptr->val; - } - return NULL; -} - -void hashmap_setval(HashMap *h, const char *key, void *val) { - size_t idx = h->hfunc(key) % h->size; - HashNode *ptr = malloc(sizeof(HashNode)); - ptr->next = h->bucket[idx]; - h->bucket[idx] = ptr; - ptr->key = key; - ptr->val = val; -} - -void hashmap_clear(HashMap *h) { - size_t i; - for (i = 0; i < h->size; i++) - { - HashNode *ptr, *nptr; - for (ptr = h->bucket[i]; ptr; ptr = nptr) - { - nptr = ptr->next; - free(ptr->val); - free(ptr); - } - h->bucket[i] = NULL; - } -} - -size_t bkdr_hash(const char *key) { - unsigned int seed = 131; - unsigned int res = 0; - while (*key) - res = res * seed + *key++; - return res; -} diff --git a/common.h b/common.h deleted file mode 100644 index e21c7a5..0000000 --- a/common.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef NERV_COMMON_H -#define NERV_COMMON_H -#include "lua.h" -#include "lauxlib.h" -#include "lualib.h" -#include "luaT/luaT.h" -#include -#include - -typedef struct HashNode { - const char *key; - void *val; - struct HashNode *next; -} HashNode; - -typedef int (*HashMapCmp_t)(const char *a, const char *b); -typedef size_t (*HashKey_t)(const char *key); - -typedef struct HashMap { - HashNode **bucket; - HashMapCmp_t cmp; - HashKey_t hfunc; - size_t size; -} HashMap; - -HashMap *hashmap_create(size_t size, HashKey_t hfunc, HashMapCmp_t cmp); -void *hashmap_getval(HashMap *h, const char *key); -void hashmap_setval(HashMap *h, const char *key, void *val); -void hashmap_clear(HashMap *h); - -size_t bkdr_hash(const char *key); - -int nerv_error(lua_State *L, const char *err_mesg_fmt, ...); -int nerv_error_method_not_implemented(lua_State *L); -void luaN_append_methods(lua_State *L, const luaL_Reg *mlist); -#endif diff --git a/doc/nerv.md b/doc/nerv.md deleted file mode 100644 index 28411f5..0000000 --- a/doc/nerv.md +++ /dev/null @@ -1,17 +0,0 @@ -#The Nerv utility functions# -Part of the [Nerv](../README.md) toolkit. -##Methods## -* __string = nerv.typename(obj a)__ -A registered function, the original function is `luaT_lua_typename`. In some cases if you call `type(a)` for object of some class in __Nerv__(like __Nerv.CuMatrix__) it will only return "userdata"(because it is created in C), in this case you can use this method to get its type. - ---- - -* __metatable = nerv.getmetatable(string tname)__ -A registered function, the original function is `luaT_lua_getmetatable`. `tname` should be a class name that has been registered in __luaT__. - -* __metatable = nerv.newmetatable(string tname, string parenttname, function constructor, function destructor, function factory)__ -A registered function, the original function is `luaT_newmetatable`, it returns the metatable of the created class by the name `tname`. -* __string = nerv.setmetatable(table self, string tname)__ -A registered function, the original function is `luaT_lua_setmetatable`. It assigns the metatable registered in __luaT__ by the name *tname* to the table *self*. And return *tname* to user. -* __table = nerv.get_type(string typename)__ -Returns the type(`loadstring("return " .. typename)`). \ No newline at end of file diff --git a/doc/nerv_class.md b/doc/nerv_class.md deleted file mode 100644 index 99f63e7..0000000 --- a/doc/nerv_class.md +++ /dev/null @@ -1,36 +0,0 @@ -#The Nerv OOP# -Part of the [Nerv](../README.md) toolkit. -##Methods## -* __metatable mt, metatable mpt = nerv.class(string tname, string parenttname)__ -This method is used to create a class by the name `tname`, which inherits `parenttname` in __Nerv__, then you create a new instance of this class by calling `obj=tname(...)`. The `tname.__init(...)` method(if defined) will be called in the constructing. The metatable of the class and its parent class will be returned. - -##Examples## -* This example implements a simple `nerv.Counter` class which is inherited by `nerv.BetterCounter`. - -``` -do - nerv.class("nerv.Counter") - function nerv.Counter:__init(c) - if (c) then - self.c = c - else - self.c = 0 - end - end -end -do - local mt, mpt = nerv.class("nerv.BetterCounter", "nerv.Counter") - function nerv.BetterCounter:__init(c, bc) - mpt.__init(self, c) - if (bc) then - self.bc = bc - else - self.bc = 0 - end - end -end -c1 = nerv.Counter(1) -print(c1.c) -bc1 = nerv.BetterCounter(1, 1) -print(bc1.c, bc1.bc) -``` \ No newline at end of file diff --git a/doc/nerv_io.md b/doc/nerv_io.md deleted file mode 100644 index 07589df..0000000 --- a/doc/nerv_io.md +++ /dev/null @@ -1,113 +0,0 @@ -#The Nerv IO Package# -Part of the [Nerv](../README.md) toolkit. - -##Description## -The main class that the user uses to store and read parameter object to and from files is __nerv.ChunkFile__. -In the file, a parameter object will be saved using a standard format. First is the length(in byte) of this object, then a table which includes some meta information of the object, and a data area. Below is an example text file. -``` -[0000000000202] -{type="nerv.ExampleP",info={message="just-a-try"},id="exampleP1"} -3 3 -5.000000 5.000000 5.000000 -5.000000 5.000000 5.000000 -5.000000 5.000000 5.000000 -1 3 -4.000000 4.000000 4.000000 -[0000000000202] -{type="nerv.ExampleP",info={message="just-a-try"},id="exampleP2"} -3 3 -4.000000 4.000000 4.000000 -4.000000 4.000000 4.000000 -4.000000 4.000000 4.000000 -1 3 -3.000000 3.000000 3.000000 -``` - -##Methods## -* __ChunkFile ChunkFile(string fn, string mode)__ -`mode` can be `r` or `w`, for reading or writing a file. The returned __ChunkFile__ will be ready to write or read objects which follows the __nerv.Param__ interface(using `write_chunk` and `read_chunk`). -* __void ChunkFile.write_chunk(ChunkFile self, Param p)__ -Write `p` into the file. `p:write` will be called. -* __Param ChunkFile.read_chunk(ChunkFile self, string id, table global_conf)__ -Read the __Param__ object by id `id` from the file `self`. It will be constructed using `__init(id, global_conf)`. `p:read` will be called. -* __void ChunkFile.close(ChunkFile self)__ -Close the opened file. - -##Examples## -* An example showing how to use __ChunkFile__ to store and read parameter objects. -``` -require 'io' -do - local mt, mpt = nerv.class('nerv.ExampleP', 'nerv.Param') - function nerv.ExampleP:__init(id, global_conf) - self.id = id - self.global_conf = global_conf - self.matrix = nerv.MMatrixFloat(3, 3) - for i = 0, 2, 1 do - for j = 0, 2, 1 do - self.matrix[i][j] = 3 - end - end - self.bias = nerv.MMatrixFloat(1, 3) - for i = 0, 2, 1 do - self.bias[i] = 2; - end - self:set_info({message = 'just-a-try'}) - end - function nerv.ExampleP:addOne() - for i = 0, 2, 1 do - for j = 0, 2, 1 do - self.matrix[i][j] = self.matrix[i][j] + 1 - end - end - for i = 0, 2, 1 do - self.bias[i] = self.bias[i] + 1 - end - end - function nerv.ExampleP:read(pcdata) - self.matrix = nerv.MMatrixFloat.load(pcdata) - self.bias = nerv.MMatrixFloat.load(pcdata) - end - function nerv.ExampleP:write(pfhandle) - self.matrix:save(pfhandle) - self.bias:save(pfhandle) - end -end -global_conf = {} -do - local f = nerv.ChunkFile('../tmp', 'w') - local exampleP1 = nerv.ExampleP('exampleP1', global_conf) - local exampleP2 = nerv.ExampleP('exampleP2', global_conf) - exampleP1:addOne() - exampleP1:addOne() - exampleP2:addOne() - - f:write_chunk(exampleP1) - f:write_chunk(exampleP2) - f:close() -end -do - local f = nerv.ChunkFile('../tmp', 'r') - local exampleP1 = f:read_chunk('exampleP1', global_conf) - local exampleP2 = f:read_chunk('exampleP2', global_conf) - f:close() - print(exampleP1.matrix) - print(exampleP2.matrix) -end -``` - -##Developer Notes## -* There are four classes in to deal with chunk data, which are __nerv.ChunkFile__, __nerv.ChunkFileHandle__, __nerv.ChunkInfo__, __nerv.ChunkData__. Below is the underlying C structs. -``` -typedef struct ChunkFileHandle { - FILE *fp; -} ChunkFileHandle; -typedef struct ChunkInfo { - off_t offset, length; -} ChunkInfo; -typedef struct ChunkData { - FILE *fp; - char *data; -} ChunkData; -``` -* In __Nerv.io__, a returned(by `ChunkFile.__init`) __nerv.ChunkFile__ will have a member `handle`, which is a __nerv.ChunkFileHandle__. \ No newline at end of file diff --git a/doc/nerv_layer.md b/doc/nerv_layer.md deleted file mode 100644 index de2fb12..0000000 --- a/doc/nerv_layer.md +++ /dev/null @@ -1,180 +0,0 @@ -#The Nerv Layer Package# -Part of the [Nerv](../README.md) toolkit. - -##Description## -__nerv.Layer__ is the base class and most of its methods are abstract. -###Class hierarchy and their members### -* __nerv.Layer__. - * `table dim_in` It specifies the dimensions of the inputs. - * `table dim_out` It specifies the dimensions of the outputs. - * `string id` ID of this layer. - * `table gconf` Stores the `global_conf`. -* __nerv.AffineLayer__ inherits __nerv.Layer__, both `#dim_in` and `#dim_out` are 1. - * `MatrixParam ltp` The liner transform parameter. - * `BiasParam bp` The bias parameter. -* __nerv.BiasLayer__ inherits __nerv.Layer__, both `#dim_in` nad `#dim_out` are 1. - * `BiasParam bias` The bias parameter. -* __nerv.SigmoidLayer__ inherits __nerv.Layer__, both `#dim_in` and `#dim_out` are 1. -* __nerv.SoftmaxCELayer__ inherits __nerv.Layer__, `#dim_in` is 2 and `#dim_out` is -1(optional). `input[1]` is the input to the softmax layer, `input[2]` is the reference distribution. In its `propagate(input, output)` method, if `output[1] ~= nil`, cross\_entropy value will outputed. - * `float total_ce` Records the accumlated cross entropy value. - * `int total_frams` Records how many frames have passed. - * `bool compressed` The reference distribution can be a one-hot format. This feature is enabled by `layer_conf.compressed`. - -##Methods## -* __void Layer.\_\_init(Layer self, string id, table global_conf, table layer_conf)__ -Abstract method. -The constructing method should assign `id` to `self.id` and `global_conf` to `self.gconf`, `layer_conf.dim_in` to `self.dim_in`, `layer_conf.dim_out` to `self.dim_out`. `dim_in` and `dim_out` are a list specifies the dimensions of the inputs and outputs. Also, `layer_conf` will include the parameters, which should also be properly saved. -* __void Layer.init(Layer self)__ -Abstract method. -Initialization method, in this method the layer should do some self-checking and allocate space for intermediate results. -* __void Layer.update(Layer self, table bp_err, table input, table output)__ -Abstract method. -`bp_err[i]` should be the error on `output[i]`. In this method the parameters of `self` is updated. -* __void Layer.propagate(Layer self, table input, table output)__ -Abstract method. -Given `input` and the current parameters, propagate and store the result in `output`. -* __void Layer.back_propagate(Layer self, Matrix next_bp_err, Matrix bp_err, Matrix input, Matrix output)__ -Abstract method. -Calculate the error on the inputs and store them in `next_bp_err`. - -* __void Layer.check_dim_len(int len_in, int len_out)__ -Check whether `#self.dim_in == len_in` and `#self.dim_out == len_out`, if violated, an error will be posted. -* __void Layer.get_params(Layer self)__ -Abstract method. -The layer should return a list containing its parameters. - -####nerv.Layer.get\_dim(self)#### -* Returns: - `dim_in`: __table__. - `dim_out`: __table__. -* Parameters: - `self`: __nerv.Layer__. -* Description: - Returns `self.dim_in, self.dim_out`. - -##Examples## -* a basic example using __Nerv__ layers to a linear classification. - -``` -require 'math' - -require 'layer.affine' -require 'layer.softmax_ce' - ---[[Example using layers, a simple two-classification problem]]-- - -function calculate_accurate(networkO, labelM) - sum = 0 - for i = 0, networkO:nrow() - 1, 1 do - if (labelM[i][0] == 1 and networkO[i][0] >= 0.5) then - sum = sum + 1 - end - if (labelM[i][1] == 1 and networkO[i][1] >= 0.5) then - sum = sum + 1 - end - end - return sum -end - ---[[begin global setting and data generation]]-- -global_conf = {lrate = 10, - wcost = 1e-6, - momentum = 0.9, - cumat_type = nerv.CuMatrixFloat} - -input_dim = 5 -data_num = 100 -ansV = nerv.CuMatrixFloat(input_dim, 1) -for i = 0, input_dim - 1, 1 do - ansV[i][0] = math.random() - 0.5 -end -ansB = math.random() - 0.5 -print('displaying ansV') -print(ansV) -print('displaying ansB(bias)') -print(ansB) - -dataM = nerv.CuMatrixFloat(data_num, input_dim) -for i = 0, data_num - 1, 1 do - for j = 0, input_dim - 1, 1 do - dataM[i][j] = math.random() * 2 - 1 - end -end -refM = nerv.CuMatrixFloat(data_num, 1) -refM:fill(ansB) -refM:mul(dataM, ansV, 1, 1) --refM = dataM * ansV + ansB - -labelM = nerv.CuMatrixFloat(data_num, 2) -for i = 0, data_num - 1, 1 do - if (refM[i][0] > 0) then - labelM[i][0] = 1 - labelM[i][1] = 0 - else - labelM[i][0] = 0 - labelM[i][1] = 1 - end -end ---[[global setting and data generation end]]-- - - ---[[begin network building]]-- ---parameters -affineL_ltp = nerv.LinearTransParam('AffineL_ltp', global_conf) -affineL_ltp.trans = nerv.CuMatrixFloat(input_dim, 2) -for i = 0, input_dim - 1, 1 do - for j = 0, 1, 1 do - affineL_ltp.trans[i][j] = math.random() - 0.5 - end -end -affineL_bp = nerv.BiasParam('AffineL_bp', global_conf) -affineL_bp.trans = nerv.CuMatrixFloat(1, 2) -for j = 0, 1, 1 do - affineL_bp.trans[j] = math.random() - 0.5 -end - ---layers -affineL = nerv.AffineLayer('AffineL', global_conf, {['ltp'] = affineL_ltp, - ['bp'] = affineL_bp, - dim_in = {input_dim}, - dim_out = {2}}) -softmaxL = nerv.SoftmaxCELayer('softmaxL', global_conf, {dim_in = {2, 2}, - dim_out = {}}) -print('layers initializing...') -affineL:init() -softmaxL:init() ---[[network building end]]-- - - ---[[begin space allocation]]-- -print('network input&output&error space allocation...') -affineI = {dataM} --input to the network is data -affineO = {nerv.CuMatrixFloat(data_num, 2)} -softmaxI = {affineO[1], labelM} -softmaxO = {} -output = nerv.CuMatrixFloat(data_num, 2) - -affineE = {nerv.CuMatrixFloat(data_num, 2)} ---[[space allocation end]]-- - - ---[[begin training]]-- -ce_last = 0 -for l = 0, 10, 1 do - affineL:propagate(affineI, affineO) - softmaxL:propagate(softmaxI, softmaxO) - output:softmax(softmaxI[1]) - - softmaxL:back_propagate(affineE, {}, softmaxI, softmaxO) - - affineL:update(affineE, affineI, affineO) - - if (l % 5 == 0) then - nerv.utils.printf("training iteration %d finished\n", l) - nerv.utils.printf("cross entropy: %.8f\n", softmaxL.total_ce - ce_last) - ce_last = softmaxL.total_ce - nerv.utils.printf("accurate labels: %d\n", calculate_accurate(output, labelM)) - nerv.utils.printf("total frames processed: %.8f\n", softmaxL.total_frames) - end -end ---[[end training]]-- -``` diff --git a/doc/nerv_matrix.md b/doc/nerv_matrix.md deleted file mode 100644 index 22971d2..0000000 --- a/doc/nerv_matrix.md +++ /dev/null @@ -1,165 +0,0 @@ -#The Nerv Matrix Package# -Part of the [Nerv](../README.md) toolkit. - -##Description## -###Underlying structure### -In the begining is could be useful to know something about the underlying structure of a __Nerv__ matrix. Please keep in mind that matrice in __Nerv__ is row-major. -Every matrix object is a encapsulation of a C struct that describes the attributes of this matrix. -``` -typedef struct Matrix { - size_t stride; /* size of a row */ - long ncol, nrow, nmax; /* dimension of the matrix, nmax is simply nrow * ncol */ - union { - float *f; - double *d; - long *i; - } data; /* pointer to actual storage */ - long *data_ref; -} Matrix; -``` -It is worth mentioning that that `data_ref` is a counter which counts the number of references to its memory space, mind that it will also be increased when a row of the matrix is referenced(`col = m[2]`). A __Nerv__ matrix will deallocate its space when this counter is decreased to zero. -Also note that all assigning operation in __Nerv__ is reference copy, you can use `copy_tod` or `copy_toh` method to copy value. Also, row assigning operations like `m1[2]=m2[3]` is forbidden in __Nerv__. - -###Class hierarchy### -The class hierarchy of the matrix classes can be clearly observed in `matrix/init.c`. -First there is a abstract base class __Nerv.Matrix__, which is inherited by __Nerv.CuMatrix__ and __Nerv.MMatrix__(also abstract). -Finally, there is __Nerv.CuMatrixFloat__, __Nerv.CuMatrixDouble__, inheriting __Nerv.CuMatrix__, and __Nerv.MMatrixFloat__, __Nerv.MMatrixDouble__, __Nerv.MMatrixInt__ , inheriting __Nerv.MMatrix__. - -##Methods## -Mind that usually a matrix object can only do calculation with matrix of its own type(a __Nerv.CuMatrixFloat__ matrix can only do add operation with a __Nerv.CuMatrixFloat__). -In the methods description below, __Matrix__ could be __Nerv.CuMatrixFloat__, __Nerv.CuMatrixDouble__, __Nerv.MMatrixFloat__ or __Nerv.MMatrixDouble__. __Element_type__ could be `float` or `double`, respectively. -* __Matrix = Matrix(int nrow, int ncol)__ -Returns a __Matrix__ object of `nrow` rows and `ncol` columns. -* __Element_type = Matrix.get_elem(Matrix self, int index)__ -Returns the element value at the specific index(treating the matrix as a vector). The index should be less than `nmax` of the matrix. -* __void Matrix.set_elem(Matrix self, int index, Element_type value)__ -Set the value at `index` to be `value`. -* __int Matrix.ncol(Matrix self)__ -Get `ncol`, the number of columns. -* __int Matrix.nrow(Matrix self)__ -Get `nrow`, the number of rows. -* __int Matrix.get_dataref_value(Matrix self)__ -Returns the value(not a pointer) of space the `data_ref` pointer pointed to. This function is mainly for debugging. -* __Matrix/Element\_type, boolean Matrix.\_\_index\_\_(Matrix self, int index)__ -If the matrix has more than one row, will return the row at `index` as a __Matrix__ . Otherwise it will return the value at `index`. -* __void Matrix.\_\_newindex\_\_(Matrix self, int index, Element_type value)__ -Set the element at `index` to be `value`. ---- -* __Matrix Matrix.create(Matrix a)__ -Return a new __Matrix__ of `a`'s size(of the same number of rows and columns). -* __Matrix Matrix.colsum(Matrix self)__ -Return a new __Matrix__ of size (1,`self.ncol`), which stores the sum of all columns of __Matrix__ `self`. -* __Matrix Matrix.rowsum(Matrix self)__ -Return a new __Matrix__ of size (`self.nrow`,1), which stores the sum of all rows of __Matrix__ `self`. -* __Matrix Matrix.rowmax(Matrix self)__ -Return a new __Matrix__ of size (`self.nrow`,1), which stores the max value of all rows of __Matrix__ `self`. -* __Matrix Matrix.trans(Matrix self)__ -Return a new __Matrix__ of size (`self.ncol`,`self.nrow`), which stores the transpose of __Matrix__ `self`. -* __void Matrix.copy_fromh(Matrix self, MMatrix a)__ -Copy the content of a __MMatrix__ `a` to __Matrix__ `self`, they should be of the same size. -* __void Matrix.copy_fromd(Matrix self, CuMatrix a)__ -Copy the content of a __CuMatrix__ `a` to __Matrix__ `self`, they should be of the same size. -* __void Matrix.copy_toh(Matrix self, MMatrix a)__ -Copy the content of the __Matrix__ `self` to a __MMatrix__ `a`. -* __void Matrix.copy_tod(Matrix self, CuMatrix a)__ -Copy the content of the __Matrix__ `self` to a __CuMatrix__ `a`. -* __void Matrix.add(Matrix self, Matrix ma, Matrix mb, Element_type alpha, Element_type beta)__ -It sets the content of __Matrix__ `self` to be `alpha * ma + beta * mb`.__Matrix__ `ma,mb,self` should be of the same size. -* __void Matrix.mul(Matrix self, Matrix ma, Matrix mb, Element_type alpha, Element_type beta, [string ta, string tb])__ -It sets the content of __Matrix__ `self` to be `beta * self + alpha * ma * mb`. `ta` and `tb` is optional, if `ta` is 'T', then `ma` will be transposed, also if `tb` is 'T', `mb` will be transposed. -* __void Matrix.add_row(Matrix self, Matrix va, Element_type beta)__ -Add `beta * va` to every row of __Matrix__ `self`. -* __void Matrix.fill(Matrix self, Element_type value)__ -Fill the content of __Matrix__ `self` to be `value`. -* __void Matrix.sigmoid(Matrix self, Matrix ma)__ -Set the element of __Matrix__ `self` to be elementwise-sigmoid of `ma`. -* __void Matrix.sigmoid_grad(Matrix self, Matrix err, Matrix output)__ -Set the element of __Matrix__ `self`, to be `self[i][j]=err[i][j]*output[i][j]*(1-output[i][j])`. This function is used to propagate sigmoid layer error. -* __void Matrix.softmax(Matrix self, Matrix a)__ -Calculate a row-by-row softmax of __Matrix__ `a` and save the result in `self`. -* __void Matrix.mul_elem(Matrix self, Matrix ma, Matrix mb)__ -Calculate element-wise multiplication of __Matrix__ `ma` and `mb`, store the result in `self`. -* __void Matrix.log_elem(Matrix self, Matrix ma)__ -Calculate element-wise log of __Matrix__ `ma`, store the result in `self`. -* __void Matrix.copy_rows_fromh_by_idx(Matrix self, MMatrix ma, MMatrixInt idx)__ -`idx` should be a row vector. This function copy the rows of `ma` to `self` according to `idx`, in other words, it assigns `ma[idx[i]]` to `self[i]`. -* __void Matrix.expand_frm(Matrix self, Matrix a, int context)__ -Treating each row of `a` as speech feature, and do a feature expansion. The `self` should of size `(a.nrow, a.ncol * (context * 2 + 1))`. `self[i]` will be `(a[i-context] a[i-context+1] ... a[i] a[i+1] a[i+context])`. `a[0]` and `a[nrow]` will be copied to extend the index range. -* __void Matrix.rearrange_frm(Matrix self, Matrix a, int step)__ -Rearrange `a` according to its feature dimension. The `step` is the length of context. So, `self[i][j]` will be assigned `a[i][j / step + (j % step) * (a.ncol / step)]`. `a` and `self` should be of the same size and `step` should be divisible by `a.ncol`. -* __void Matrix.scale_row(Matrix self, Matrix scale)__ -Scale each column of `self` according to a vector `scale`. `scale` should be of size `1 * self.ncol`. -* __Matrix Matrix.\_\_add\_\_(Matrix ma, Matrix mb)__ -Returns a new __Matrix__ which stores the result of `ma+mb`. -* __Matrix Matrix.\_\_sub\_\_(Matrix ma, Matrix mb)__ -Returns a new __Matrix__ which stores the result of `ma-mb`. -* __Matrix Matrix.\_\_mul\_\_(Matrix ma, Matrix mb)__ -Returns a new __Matrix__ which stores the result of `ma*mb`. -* __CuMatrix CuMatrix.new_from_host(MMatrix m)__ -Return a new __CuMatrix__ which is a copy of `m`. -* __MMatrix CuMatrix.new_to_host(CuMatrix self)__ -Return a new __MMatrix__ which is a copy of `self`. -* __string Matrix.\_\_tostring\_\_(Matrix self)__ -Returns a string containing values of __Matrix__ `self`. ---- -* __MMatrix MMatrix.load(ChunkData chunk)__ -Return a new __MMatrix__ loaded from the file position in `chunk`. -* __void MMatrix.save(MMatrix self, ChunkFileHandle chunk)__ -Write `self` to the file position in `chunk`. -* __void MMatrix.copy_from(MMatrix ma, MMatrix mb,[int b_bgein, int b_end, int a_begin])__ -Copy a part of `mb`(rows of index `[b_begin..b_end)`) to `ma` beginning at row index `a_begin`. If not specified, `b_begin` will be `0`, `b_end` will be `b.nrow`, `a_begin` will be `0`. - -##Examples## -* Use `get_dataref_value` to test __Nerv__'s matrix space allocation. -``` -m = 10 -n = 10 -fm = nerv.MMatrixFloat(m, n) -dm = nerv.MMatrixDouble(m, n) -for i = 0, m - 1 do - for j = 0, n - 1 do - t = i / (j + 1) - fm[i][j] = t - dm[i][j] = t - end -end -print("test fm:get_dataref_value:", fm:get_dataref_value()) -print("forced a garbade collect") -collectgarbage("collect") -print("test fm:get_dataref_value:", fm:get_dataref_value()) -print(fm) -print(dm) -``` -* Test some __Matrix__ calculations. -``` -m = 4 -n = 4 -fm = nerv.CuMatrixFloat(m, n) -dm = nerv.CuMatrixDouble(m, n) -for i = 0, m - 1 do - for j = 0, n - 1 do - -- local t = math.random(10) - t = i / (j + 1) - fm[i][j] = t - dm[i][j] = t - end -end -print(fm) -fs = fm:create() -fs:softmax(fm) --- print(fs) -print(dm) -ds = dm:create() -ds:softmax(dm) --- print(ds) -print(fs) -print(fs + fs) -print(ds + ds) -print(fs - fs) -print(ds - ds) -a = fs:create() -a:mul_elem(fs, fs) -print(a) -a:log_elem(fs) -print(a) -``` \ No newline at end of file diff --git a/doc/nerv_nn.md b/doc/nerv_nn.md deleted file mode 100644 index c57447d..0000000 --- a/doc/nerv_nn.md +++ /dev/null @@ -1,256 +0,0 @@ -#The Nerv NN Package# -Part of the [Nerv](../README.md) toolkit. - -##Description## -###Class hierarchy### -it contains __nerv.LayerRepo__, __nerv.ParamRepo__, and __nerv.DAGLayer__(inherits __nerv.Layer__). - -###Class hierarchy and their members### -####nerv.ParamRepo#### -Get parameter object by ID. -* `table param_table` Contains the mapping of parameter ID to parameter file(__nerv.ChunkFile__) -* __nerv.LayerRepo__ Get layer object by ID. -* `table layers` Contains the mapping of layer ID to layer object. -objects. - -####__nerv.DAGLayer__#### -Inherits __nerv.Layer__. -* `layers`: __table__, a mapping from a layer ID to its "ref". A ref is a structure that contains reference to space allocations and other info of the layer. -* `inputs`: __table__, a mapping from the inputs ports of the DAG layer to the input ports of the sublayer, the key is the port number, the value is `{ref, port}`. -* `outputs`:__table__, the counterpart of `inputs`. -* `parsed_conn`: __table__, a list of parsed connections, each entry is of format `{{ref_from, port_from}, {ref_to, port_to}}`. -* `queue`: __table__, a list of "ref"s, the propagation of the DAGLayer will follow this order, and back-propagation will follow a reverse order. - -##Methods## - -###__nerv.ParamRepo__### - -####nerv.ParamRepo:\_\_init(param\_files)#### -* Parameters: - `param_files`: __table__ -* Description: - `param_files` is a list of file names that stores parameters, the newed __ParamRepo__ will read them from file and store the mapping for future fetching. - -####nerv.Param ParamRepo.get_param(ParamRepo self, string pid, table global_conf)#### -* Returns: - __nerv.Layer__ -* Parameters: - `self`: __nerv.ParamRepo__. - `pid`: __string__. - `global_conf`: __table__. -* Description: - __ParamRepo__ will find the __nerv.ChunkFile__ `pf` that contains parameter of ID `pid` and return `pf:read_chunk(pid, global_conf)`. - -###__nerv.LayerRepo__### -####nerv.LayerRepo:\_\_init(layer\_spec, param\_repo, global\_conf)#### -* Returns: - __nerv.LayerRepo__. -* Parameters: - `self`: __nerv.ParamRepo__. - `layer_spec`: __table__. - `param_repo`: __nerv.ParamRepo__. - `global_conf`: __table__. -* Description: - __LayerRepo__ will construct the layers specified in `layer_spec`. Every entry in the `layer_spec` table should follow the format below: - - > layer_spec : {[layer_type1] = llist1, [layer_type2] = llist2, ...} - > llist : {layer1, layer2, ...} - > layer : layerid = {param_config, layer_config} - > param_config : {param1 = paramID1, param2 = paramID2} - - __LayerRepo__ will merge `param_config` into `layer_config` and construct a layer by calling `layer_type(layerid, global_conf, layer_config)`. - -####nerv.LayerRepo.get\_layer(self, lid)#### -* Returns: - __nerv.LayerRepo__, the layer with ID `lid`. -* Parameters: - `self`:__nerv.LayerRepo__. - `lid`:__string__. -* Description: - Returns the layer with ID `lid`. - -###nerv.DAGLayer### -####nerv.DAGLayer:\_\_init(id, global\_conf, layer\_conf)#### -* Returns: - __nerv.DAGLayer__ -* Parameters: - `id`: __string__ - `global_conf`: __table__ - `layer_conf`: __table__ -* Description: - The `layer_conf` should contain `layer_conf.sub_layers` which is a __nerv.LayerRepo__ storing the sub layers of the DAGLayer. It should also contain `layer_conf.connections`, which is a string-to-string mapping table describing the DAG connections. See an example below: - - ``` - dagL = nerv.DAGLayer("DAGL", global_conf, {["dim_in"] = {input_dim, 2}, ["dim_out"] = {}, ["sub_layers"] = layerRepo, - ["connections"] = { - ["[1]"] = "AffineL[1]", - ["AffineL[1]"] = "SoftmaxL[1]", - ["[2]"] = "SoftmaxL[2]", - }}) - ``` - -####nerv.DAGLayer.init(self, batch\_size)#### -* Parameters: - `self`: __nerv.DAGLayer__ - `batch_size`: __int__ -* Description: - This initialization method will allocate space for output and input matrice, and will call `init()` for each of its sub layers. - - -####nerv.DAGLayer.propagate(self, input, output)#### -* Parameters: - `self`: __nerv.DAGLayer__ - `input`: __table__ - `output`: __table__ -* Description: - The same function as __nerv.Layer.propagate__, do propagation for each layer in the order of `self.queue`. - -####nerv.DAGLayer.back\_propagate(self, next\_bp\_err, bp\_err, input, output)#### -* Parameters: - `self`: __nerv.DAGLayer__ - `next_bp_err`: __table__ - `bp_err`: __table__ - `input`: __table__ - `output`: __table__ -* Description: - The same function as __nerv.Layer.back_propagate__, do back-propagation for each layer in the reverse order of `self.queue`. - -####nerv.DAGLayer.update(self, bp\_err, input, output)#### -* Parameters: - `self`: __nerv.DAGLayer__ - `bp_err`: __table__ - `input`: __table__ - `output`: __table__ -* Description: - The same function as __nerv.Layer.update__, do update for each layer in the order of `self.queue`. - -##Examples## -* aaa - -``` -require 'math' - -require 'layer.affine' -require 'layer.softmax_ce' - ---[[Example using DAGLayer, a simple two-classification problem]]-- - ---[[begin global setting and data generation]]-- -global_conf = {lrate = 10, - wcost = 1e-6, - momentum = 0.9, - cumat_type = nerv.CuMatrixFloat, - } - -input_dim = 5 -data_num = 100 -param_fn = "../tmp" -ansV = nerv.CuMatrixFloat(input_dim, 1) -for i = 0, input_dim - 1, 1 do - ansV[i][0] = math.random() - 0.5 -end -ansB = math.random() - 0.5 -print('displaying ansV') -print(ansV) -print('displaying ansB(bias)') -print(ansB) - -dataM = nerv.CuMatrixFloat(data_num, input_dim) -for i = 0, data_num - 1, 1 do - for j = 0, input_dim - 1, 1 do - dataM[i][j] = math.random() * 2 - 1 - end -end -refM = nerv.CuMatrixFloat(data_num, 1) -refM:fill(ansB) -refM:mul(dataM, ansV, 1, 1) --refM = dataM * ansV + ansB - -labelM = nerv.CuMatrixFloat(data_num, 2) -for i = 0, data_num - 1, 1 do - if (refM[i][0] > 0) then - labelM[i][0] = 1 - labelM[i][1] = 0 - else - labelM[i][0] = 0 - labelM[i][1] = 1 - end -end ---[[global setting and data generation end]]-- - - ---[[begin network building]]-- ---parameters -do - local affineL_ltp = nerv.LinearTransParam('AffineL_ltp', global_conf) - affineL_ltp.trans = nerv.CuMatrixFloat(input_dim, 2) - for i = 0, input_dim - 1, 1 do - for j = 0, 1, 1 do - affineL_ltp.trans[i][j] = math.random() - 0.5 - end - end - local affineL_bp = nerv.BiasParam('AffineL_bp', global_conf) - affineL_bp.trans = nerv.CuMatrixFloat(1, 2) - for j = 0, 1, 1 do - affineL_bp.trans[j] = math.random() - 0.5 - end - - local chunk = nerv.ChunkFile(param_fn, 'w') - chunk:write_chunk(affineL_ltp) - chunk:write_chunk(affineL_bp) - chunk:close() - - paramRepo = nerv.ParamRepo({param_fn}) -end - ---layers -layerRepo = nerv.LayerRepo({ - ["nerv.AffineLayer"] = - { - ["AffineL"] = {{["ltp"] = "AffineL_ltp", ["bp"] = "AffineL_bp"}, {["dim_in"] = {input_dim}, ["dim_out"] = {2}}}, - }, - ["nerv.SoftmaxCELayer"] = - { - ["SoftmaxL"] = {{}, {["dim_in"] = {2, 2}, ["dim_out"] = {}}} - }, - }, paramRepo, global_conf) -affineL = layerRepo:get_layer("AffineL") -softmaxL = layerRepo:get_layer("SoftmaxL") -print('layers initializing...') -dagL = nerv.DAGLayer("DAGL", global_conf, {["dim_in"] = {input_dim, 2}, ["dim_out"] = {}, ["sub_layers"] = layerRepo, - ["connections"] = { - ["[1]"] = "AffineL[1]", - ["AffineL[1]"] = "SoftmaxL[1]", - ["[2]"] = "SoftmaxL[2]", - }}) -dagL:init(data_num) ---affineL:init() ---softmaxL:init() ---[[network building end]]-- - - ---[[begin space allocation]]-- -print('network input&output&error space allocation...') -dagL_input = {dataM, labelM} -dagL_output = {} -dagL_err = {} -dagL_ierr = {nerv.CuMatrixFloat(data_num, input_dim), nerv.CuMatrixFloat(data_num, 2)} ---[[space allocation end]]-- - - ---[[begin training]]-- -ce_last = 0 -for l = 0, 10, 1 do - dagL:propagate(dagL_input, dagL_output) - dagL:back_propagate(dagL_ierr, dagL_err, dagL_input, dagL_output) - dagL:update(dagL_err, dagL_input, dagL_output) - - if (l % 2 == 0) then - nerv.utils.printf("training iteration %d finished\n", l) - nerv.utils.printf("cross entropy: %.8f\n", softmaxL.total_ce - ce_last) - --nerv.utils.printf("accurate labels: %d\n", calculate_accurate(output, labelM)) - nerv.utils.printf("total frames processed: %.8f\n", softmaxL.total_frames) - end - ce_last = softmaxL.total_ce -end ---[[end training]]-- -``` \ No newline at end of file diff --git a/doc/nerv_param.md b/doc/nerv_param.md deleted file mode 100644 index 167cb11..0000000 --- a/doc/nerv_param.md +++ /dev/null @@ -1,27 +0,0 @@ -#The Nerv Parameter Package# -Part of the [Nerv](../README.md) toolkit. - -##Description## -###Class hierarchy### -There is a base class __Nerv.Param__ defined in `layer/init.lua`. - -###Class hierarchy and their members### -* __nerv.MatrixParam__ inherits __nerv.Param__ - * `Matrix trans` stores the parameter matrix. -* __nerv.LinearTransParam__ inherits __Nerv.MatrixParam__. -* __Nerv.BiasParam__ inherits __Nerv.MatrixParam__. - -##Methods## -* __void Param.\_\_init(Param self, string id, table global_conf)__ -Constructor of a __Param__, it will set `self.id` to be `id` and `self.gconf` to be `global_conf`. -* __void Param.set_info(Param self, table info)__ -Set `self.info` to be `info`. -* __table Param.get_info(Param self)__ -Returns `self.info`. -* __void Param.read(Param self, ChunkData pcdata)__ -Abstract method. -In this method, `self` should in turn calls its members to load from `pcdata`. -* __void Param.write(Param self, ChunkFileHandle pfhandle)__ -Abstract method. -Save parameters to file. In this method, `self` should in turn calls its members to save to `pfhandle`. - diff --git a/examples/asr_trainer.lua b/examples/asr_trainer.lua deleted file mode 100644 index a5727be..0000000 --- a/examples/asr_trainer.lua +++ /dev/null @@ -1,106 +0,0 @@ -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 crit = get_criterion_layer(sublayer_repo) - local network = get_network(layer_repo) - local input_order = get_input_order() - local iterative_trainer = function (prefix, scp_file, bp) - gconf.randomize = bp - -- build buffer - local buffer = make_buffer(make_readers(scp_file, layer_repo)) - -- initialize the network - network:init(gconf.batch_size) - gconf.cnt = 0 - err_input = {nerv.CuMatrixFloat(256, 1)} - err_input[1]:fill(1) - for data in buffer.get_data, buffer do - -- prine stat periodically - gconf.cnt = gconf.cnt + 1 - if gconf.cnt == 1000 then - print_stat(sublayer_repo) - nerv.CuMatrix.print_profile() - nerv.CuMatrix.clear_profile() - gconf.cnt = 0 - -- break - end - local input = {} --- if gconf.cnt == 100 then break end - for i, id in ipairs(input_order) do - if data[id] == nil then - nerv.error("input data %s not found", id) - end - table.insert(input, data[id]) - end - local output = {nerv.CuMatrixFloat(256, 1)} - err_output = {input[1]:create()} - network:propagate(input, output) - if bp then - network:back_propagate(err_input, err_output, input, output) - network:update(err_input, input, output) - end - -- collect garbage in-time to save GPU memory - collectgarbage("collect") - end - print_stat(sublayer_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)) - network:get_params():export(fname, nil) - end - return get_accuracy(sublayer_repo) - end - return iterative_trainer -end - -dofile(arg[1]) -start_halving_inc = 0.5 -halving_factor = 0.6 -end_halving_inc = 0.1 -min_iter = 1 -max_iter = 20 -min_halving = 5 -gconf.batch_size = 256 -gconf.buffer_size = 81920 - -local pf0 = gconf.initialized_param -local trainer = build_trainer(pf0) ---local trainer = build_trainer("c3.nerv") -local accu_best = trainer(nil, gconf.cv_scp, false) -local do_halving = false - -nerv.info("initial cross validation: %.3f", accu_best) -for i = 1, max_iter do - nerv.info("[NN] begin iteration %d with lrate = %.6f", i, gconf.lrate) - local accu_tr = trainer(nil, gconf.tr_scp, true) - nerv.info("[TR] training set %d: %.3f", i, accu_tr) - local accu_new = trainer( - string.format("%s_%s_iter_%d_lr%f_tr%.3f", - string.gsub( - (string.gsub(pf0[1], "(.*/)(.*)", "%2")), - "(.*)%..*", "%1"), - os.date("%Y%m%d%H%M%S"), - i, gconf.lrate, - accu_tr), - gconf.cv_scp, false) - nerv.info("[CV] cross validation %d: %.3f", i, accu_new) - -- TODO: revert the weights - local accu_diff = accu_new - accu_best - if do_halving and accu_diff < end_halving_inc and i > min_iter then - break - end - if accu_diff < start_halving_inc and i >= min_halving then - do_halving = true - end - if do_halving then - gconf.lrate = gconf.lrate * halving_factor - end - if accu_new > accu_best then - accu_best = accu_new - end --- nerv.Matrix.print_profile() -end diff --git a/examples/chunk_file_example.lua b/examples/chunk_file_example.lua deleted file mode 100644 index 5961c98..0000000 --- a/examples/chunk_file_example.lua +++ /dev/null @@ -1,53 +0,0 @@ --- To define a readable and writable chunk, one must define a class with the --- following methods: __init(id, global_conf), read(handle), write(handle), --- get_info(), set_info(info) and an id attribute. This file demonstrates a --- basic chunk implementation which manages the I/O of a matrix - -local MatrixChunk = nerv.class("nerv.MatrixChunk") - -function MatrixChunk:__init(id, global_conf) - self.id = id - self.info = {} - self.gconf = global_conf -end - -function MatrixChunk:read(handle) - -- pass the read handle to the matrix method - self.data = nerv.MMatrixFloat.load(handle) -end - -function MatrixChunk:write(handle) - -- pass the write handle to the matrix method - self.data:save(handle) -end - -function MatrixChunk:get_info() - return self.info -end - -function MatrixChunk:set_info(info) - self.info = info -end - -function MatrixChunk.create_from_matrix(id, mat) - local ins = nerv.MatrixChunk(id) - ins.data = mat - return ins -end - -mat = nerv.MMatrixFloat(3, 4) -for i = 0, 2 do - for j = 0, 3 do - mat[i][j] = i + j - end -end - -cd = nerv.MatrixChunk.create_from_matrix("matrix1", mat) - -cf = nerv.ChunkFile("test.nerv", "w") -cf:write_chunk(cd) -cf:close() - -cf2 = nerv.ChunkFile("test.nerv", "r") -cd2 = cf2:read_chunk("matrix1") -print(cd2.data) diff --git a/examples/cumatrix_example.lua b/examples/cumatrix_example.lua deleted file mode 100644 index 544fc7f..0000000 --- a/examples/cumatrix_example.lua +++ /dev/null @@ -1,31 +0,0 @@ -m = 4 -n = 4 -fm = nerv.CuMatrixFloat(m, n) -dm = nerv.CuMatrixDouble(m, n) -for i = 0, m - 1 do - for j = 0, n - 1 do - -- local t = math.random(10) - t = i / (j + 1) - fm[i][j] = t - dm[i][j] = t - end -end -print(fm) -fs = fm:create() -fs:softmax(fm) --- print(fs) -print(dm) -ds = dm:create() -ds:softmax(dm) --- print(ds) -print(fs) -print(fs + fs) -print(ds + ds) -print(fs - fs) -print(ds - ds) - -a = fs:create() -a:mul_elem(fs, fs) -print(a) -a:log_elem(fs) -print(a) diff --git a/examples/cumatrix_from_mmatrix.lua b/examples/cumatrix_from_mmatrix.lua deleted file mode 100644 index 2309e14..0000000 --- a/examples/cumatrix_from_mmatrix.lua +++ /dev/null @@ -1,32 +0,0 @@ -m = 3 -n = 4 -fm = nerv.MMatrixFloat(m, n) -dm = nerv.MMatrixDouble(m, n) -for i = 0, m - 1 do - for j = 0, n - 1 do - -- local t = math.random(10) - t = i / (j + 1) - fm[i][j] = t - dm[i][j] = t - end -end -print(fm) -print(dm) - -fc = nerv.CuMatrixFloat(m, n) -dc = nerv.CuMatrixDouble(m, n) -fc:copy_fromh(fm) -dc:copy_fromh(dm) -print("fc and dc") -print(fc) -print(dc) -dc[1]:copy_tod(dc[0]) -print("dc[1] copied to dc[0]") -print(dc) -print("softmax of fc and dc") -sfc = fc:create() -sdc = dc:create() -sfc:softmax(fc) -print(sfc) -sdc:softmax(dc) -print(sdc) diff --git a/examples/mmatrix_example.lua b/examples/mmatrix_example.lua deleted file mode 100644 index 8ddfe84..0000000 --- a/examples/mmatrix_example.lua +++ /dev/null @@ -1,20 +0,0 @@ -m = 10 -n = 10 -fm = nerv.MMatrixFloat(m, n) -dm = nerv.MMatrixDouble(m, n) -for i = 0, m - 1 do - for j = 0, n - 1 do - -- local t = math.random(10) - t = i / (j + 1) - fm[i][j] = t - dm[i][j] = t - end -end -print("test fm:get_dataref_value:", fm:get_dataref_value()) -print("forced a garbade collect") -collectgarbage("collect") -print("test fm:get_dataref_value:", fm:get_dataref_value()) -print(fm) --- print(fm:softmax()) -print(dm) --- print(dm:softmax()) diff --git a/examples/oop_example.c b/examples/oop_example.c deleted file mode 100644 index 59dfc5a..0000000 --- a/examples/oop_example.c +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include -#include -#include "../common.h" - -#define SQR(x) ((x) * (x)) - -const char *point_tname = "nerv.Point"; -const char *better_point_tname = "nerv.BetterPoint"; - -typedef struct { - double x, y; -} Point; - -static int point_norm (lua_State *L) { - Point *p = luaT_checkudata(L, 1, point_tname); - lua_pushnumber(L, sqrt(SQR(p->x) + SQR(p->y))); - return 1; -} - -static int point_set_x (lua_State *L) { - Point *p = luaT_checkudata(L, 1, point_tname); - p->x = luaL_checknumber(L, 2); - return 0; -} - -static int point_set_y (lua_State *L) { - Point *p = luaT_checkudata(L, 1, point_tname); - p->y = luaL_checknumber(L, 2); - return 0; -} - -/* generic constructor */ -void point_new_(Point *self, double x, double y) { - self->x = x; - self->y = y; -} - -int point_new(lua_State *L) { - /* `_new` function should create the object itself */ - Point *self = (Point *)malloc(sizeof(Point)); - point_new_(self, luaL_checknumber(L, 1), luaL_checknumber(L, 2)); - luaT_pushudata(L, self, point_tname); - fprintf(stderr, "[example] %s constructor is invoked\n", - point_tname); - return 1; -} - -static const luaL_Reg point_methods[] = { - {"set_x", point_set_x}, - {"set_y", point_set_y}, - {"norm", point_norm}, - {NULL, NULL} -}; - - -/* the subclass method overrides the one from baseclass */ -static int better_point_norm (lua_State *L) { - Point *p = luaT_checkudata(L, 1, point_tname); - lua_pushnumber(L, fabs(p->x) + fabs(p->y)); - return 1; -} - -int better_point_new(lua_State *L) { - /* `_new` function should create the object itself */ - Point *self = (Point *)malloc(sizeof(Point)); - point_new_(self, luaL_checknumber(L, 1), luaL_checknumber(L, 2)); - luaT_pushudata(L, self, better_point_tname); - fprintf(stderr, "[example] %s constructor is invoked\n", - better_point_tname); - return 1; -} - -static const luaL_Reg better_point_methods[] = { - {"norm", better_point_norm}, - {NULL, NULL} -}; - -void nerv_point_init(lua_State *L) { - /* create a class and let luaT know */ - luaT_newmetatable(L, point_tname, NULL, point_new, NULL, NULL); - /* register member functions */ - luaL_register(L, NULL, point_methods); - /* keep the stack balanced, see `nerv.c` */ - lua_pop(L, 1); -} - -void nerv_better_point_init(lua_State *L) { - /* create a class and let luaT know */ - luaT_newmetatable(L, better_point_tname, point_tname, - better_point_new, NULL, NULL); - /* register member functions */ - luaL_register(L, NULL, better_point_methods); - /* keep the stack balanced, see `nerv.c` */ - lua_pop(L, 1); -} - -void nerv_example_init(lua_State *L) { - nerv_point_init(L); - nerv_better_point_init(L); -} diff --git a/examples/oop_example.lua b/examples/oop_example.lua deleted file mode 100644 index b753288..0000000 --- a/examples/oop_example.lua +++ /dev/null @@ -1,16 +0,0 @@ -p = nerv.Point(0, 0) -- create a Point instance -print(p) -print(p:norm()) -- get 2-norm of the Point -p:set_x(1.0) -p:set_y(2.0) -print(p:norm()) -- get 2-norm of the Point - -bp = nerv.BetterPoint(1, 2) --- use methods from base class -bp:set_x(1.0) -bp:set_y(2.0) -print(bp) -print(bp:norm()) --get 1-norm of the Point - -print(p.__typename) -print(bp.__typename) diff --git a/examples/swb_baseline.lua b/examples/swb_baseline.lua deleted file mode 100644 index 8b7e01a..0000000 --- a/examples/swb_baseline.lua +++ /dev/null @@ -1,166 +0,0 @@ -require 'speech.init' -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}}}, -