aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2015-06-02 20:28:16 +0800
committerDeterminant <[email protected]>2015-06-02 20:28:16 +0800
commit74d9e9e7371c80394698fb9805cbf0cbde67a8f3 (patch)
tree36b070f1fcfa2be8fc80c50b7a221862a0dfd14a /examples
parent60083f2e51935ce55cec7a4c39d1724a16d9c769 (diff)
add ParamRepo, LayerRepo, DAGLayer
Diffstat (limited to 'examples')
-rw-r--r--examples/test_dnn_layers.lua9
-rw-r--r--examples/test_nn_lib.lua63
2 files changed, 67 insertions, 5 deletions
diff --git a/examples/test_dnn_layers.lua b/examples/test_dnn_layers.lua
index 9be9d71..6e4d98d 100644
--- a/examples/test_dnn_layers.lua
+++ b/examples/test_dnn_layers.lua
@@ -50,15 +50,14 @@ for i = 0, 3 do
sg:propagate(input2, output2)
sm:propagate(input3, output3)
-
-- back_propagate
sm:back_propagate(err_output1, err_input1, input3, output3)
- sm:update(err_input1, input3, output3)
-
sg:back_propagate(err_output2, err_input2, input2, output2)
- sg:update(err_input2, input2, output2)
-
af:back_propagate(err_output3, err_input3, input1, output1)
+
+ -- update
+ sm:update(err_input1, input3, output3)
+ sg:update(err_input2, input2, output2)
af:update(err_input3, input1, output1)
diff --git a/examples/test_nn_lib.lua b/examples/test_nn_lib.lua
new file mode 100644
index 0000000..fd7167a
--- /dev/null
+++ b/examples/test_nn_lib.lua
@@ -0,0 +1,63 @@
+require 'layer.affine'
+require 'layer.sigmoid'
+require 'layer.softmax_ce'
+
+gconf = {lrate = 0.8, wcost = 1e-6, momentum = 0.9,
+ mat_type = nerv.CuMatrixFloat,
+ batch_size = 10}
+
+param_repo = nerv.ParamRepo({"affine.param"})
+sublayer_repo = nerv.LayerRepo(
+ {
+ ["nerv.AffineLayer"] =
+ {
+ affine1 = {{ltp = "a", bp = "b"}, {dim_in = {429}, dim_out = {2048}}}
+ },
+ ["nerv.SigmoidLayer"] =
+ {
+ sigmoid1 = {{}, {dim_in = {2048}, dim_out = {2048}}}
+ },
+ ["nerv.SoftmaxCELayer"] =
+ {
+ softmax_ce1 = {{}, {dim_in = {2048, 2048}, dim_out = {}}}
+ }
+ }, param_repo, gconf)
+
+layer_repo = nerv.LayerRepo(
+ {
+ ["nerv.DAGLayer"] =
+ {
+ main = {{}, {
+ dim_in = {429, 2048}, dim_out = {},
+ sub_layers = sublayer_repo,
+ connections = {
+ ["<input>[1]"] = "affine1[1]",
+ ["affine1[1]"] = "sigmoid1[1]",
+ ["sigmoid1[1]"] = "softmax_ce1[1]",
+ ["<input>[2]"] = "softmax_ce1[2]"
+ }
+ }}
+ }
+ }, param_repo, gconf)
+
+df = nerv.ChunkFile("input.param", "r")
+label = nerv.CuMatrixFloat(10, 2048)
+label:fill(0)
+for i = 0, 9 do
+ label[i][i] = 1.0
+end
+
+input = {df:read_chunk("input", gconf).trans, label}
+output = {}
+err_input = {}
+err_output = {input[1]:create()}
+sm = sublayer_repo:get_layer("softmax_ce1")
+main = layer_repo:get_layer("main")
+main:init()
+for i = 0, 3 do
+ main:propagate(input, output)
+ main:back_propagate(err_output, err_input, input, output)
+ main:update(err_input, input, output)
+ nerv.utils.printf("cross entropy: %.8f\n", sm.total_ce)
+ nerv.utils.printf("frames: %.8f\n", sm.total_frames)
+end