diff options
-rw-r--r-- | nerv/examples/lmptb/m-tests/dagl_test.lua | 6 | ||||
-rw-r--r-- | nerv/examples/lmptb/rnn/layer_tdag.lua | 78 |
2 files changed, 47 insertions, 37 deletions
diff --git a/nerv/examples/lmptb/m-tests/dagl_test.lua b/nerv/examples/lmptb/m-tests/dagl_test.lua index 9f45b6a..8959a04 100644 --- a/nerv/examples/lmptb/m-tests/dagl_test.lua +++ b/nerv/examples/lmptb/m-tests/dagl_test.lua @@ -140,8 +140,8 @@ global_conf = { mmat_type = nerv.CuMatrixFloat, hidden_size = 20, - batch_size = 5, - seq_size = 3, + chunk_size = 5, + batch_size = 3, max_iter = 18, param_random = function() return (math.random() / 5 - 0.1) end, independent = true, @@ -161,4 +161,4 @@ global_conf.vocab:build_file(global_conf.train_fn, false) local paramRepo = prepare_parameters(global_conf, true) local layerRepo = prepare_layers(global_conf, paramRepo) local dagL = prepare_dagLayer(global_conf, layerRepo) ---dagL:init(global_conf.batch_size) +dagL:init(global_conf.batch_size, global_conf.chunk_size) diff --git a/nerv/examples/lmptb/rnn/layer_tdag.lua b/nerv/examples/lmptb/rnn/layer_tdag.lua index f417f91..3fa501e 100644 --- a/nerv/examples/lmptb/rnn/layer_tdag.lua +++ b/nerv/examples/lmptb/rnn/layer_tdag.lua @@ -31,11 +31,10 @@ local function discover(id, layers, layer_repo) local dim_in, dim_out = layer:get_dim() ref = { layer = layer, - inputs_m = {}, --storage for computation + inputs_m = {}, --storage for computation, inputs_m[port][time] outputs_m = {}, err_inputs_m = {}, err_outputs_m = {}, - next_layers = {}, conns_i = {}, --list of inputing connections conns_o = {}, --list of outputing connections dim_in = dim_in, --list of dimensions of ports @@ -46,11 +45,13 @@ local function discover(id, layers, layer_repo) return ref end -local function makeInitialStore(dim, batch_size, global_conf) +function DAGLayer.makeInitialStore(dim, batch_size, chunk_size, global_conf) + --Return a table of matrix storage from time (1-chunk_size)..(2*chunk_size) st = {} - for i = 1 - batch_size, batch_size * 2 do + for i = 1 - chunk_size, chunk_size * 2 do st[i] = global_conf.cumat_type(batch_size, dim) end + return st end function DAGLayer:__init(id, global_conf, layer_conf) @@ -77,11 +78,13 @@ function DAGLayer:__init(id, global_conf, layer_conf) nerv.error("mismatch dimension or wrong time %s,%s,%d", ll[1], ll[2], ll[3]) end inputs_p[port_from] = {["ref"] = ref_to, ["port"] = port_to} + ref_to.inputs_m[port_to] = {} --just a place holder elseif (id_to == "<output>") then if (dim_out[port_to] ~= ref_from.dim_out[port_from] or time_to ~= 0) then nerv.error("mismatch dimension or wrong time %s,%s,%d", ll[1], ll[2], ll[3]) end outputs_p[port_to] = {["ref"] = ref_from, ["port"] = port_from} + ref_from.outputs_m[port_from] = {} --just a place holder else conn_now = { ["src"] = {["ref"] = ref_from, ["port"] = port_from}, @@ -107,8 +110,8 @@ function DAGLayer:__init(id, global_conf, layer_conf) self.gconf = global_conf end -function DAGLayer:init(seq_size) - for i, conn in ipairs(self.parsed_conn) do +function DAGLayer:init(batch_size, chunk_size) + for i, conn in ipairs(self.parsed_conns) do local _, output_dim local ref_from, port_from, ref_to, port_to ref_from, port_from = conn.src.ref, conn.src.port @@ -118,25 +121,25 @@ function DAGLayer:init(seq_size) nerv.error("layer %s has a zero dim port", ref_from.layer.id) end - local mid = makeInitialStore(dim, seq_size, global_conf) - local err_mid = makeInitialStore(dim, seq_size, global_conf) + local mid = DAGLayer.makeInitialStore(dim, batch_size, chunk_size, global_conf) + local err_mid = DAGLayer.makeInitialStore(dim, batch_size, chunk_size, global_conf) print(ref_from.layer.id, "->", ref_to.layer.id) ref_from.outputs_m[port_from] = mid ref_to.inputs_m[port_to] = mid - ref_from.err_outputs_m[port_from] = err_mid - ref_to.err_inputs_m[port_to] = err_mid + ref_from.err_inputs_m[port_from] = err_mid + ref_to.err_outputs_m[port_to] = err_mid end for id, ref in pairs(self.layers) do - for i = 1, ref.input_len do - if ref.inputs[i] == nil then + for i = 1, #ref.dim_in do + if ref.inputs_m[i] == nil then nerv.error("dangling input port %d of layer %s", i, id) end end - for i = 1, ref.output_len do - if ref.outputs[i] == nil then + for i = 1, #ref.dim_out do + if ref.outputs_m[i] == nil then nerv.error("dangling output port %d of layer %s", i, id) end end @@ -144,17 +147,18 @@ function DAGLayer:init(seq_size) ref.layer:init(batch_size) end for i = 1, #self.dim_in do - if self.inputs[i] == nil then - nerv.error("dangling port %d of layer <input>", i) + if self.inputs_p[i] == nil then + nerv.error("<input> port %d not attached", i) end end for i = 1, #self.dim_out do - if self.outputs[i] == nil then - nerv.error("dangling port %d of layer <output>", i) + if self.outputs_p[i] == nil then + nerv.error("<output> port %d not attached", i) end end end +--[[ function DAGLayer:batch_resize(batch_size) self.gconf.batch_size = batch_size @@ -181,37 +185,42 @@ function DAGLayer:batch_resize(batch_size) end collectgarbage("collect") end +]]-- -function DAGLayer:set_inputs(input) +function DAGLayer:set_inputs(inputs_m) for i = 1, #self.dim_in do - if input[i] == nil then - nerv.error("some input is not provided"); + if inputs_m[i] == nil then + nerv.error("inputs_m[%d] is not provided", i); end - local layer = self.inputs[i][1] - local port = self.inputs[i][2] - layer.inputs[port] = input[i] --TODO: should be inputs_m + local ref = self.inputs_p[i].ref + local p = self.inputs_p[i].port + ref.inputs_m[p] = inputs_m[i] end end -function DAGLayer:set_outputs(output) +function DAGLayer:set_outputs(outputs_m) for i = 1, #self.dim_out do - if output[i] == nil then - nerv.error("some output is not provided"); + if outputs_m[i] == nil then + nerv.error("outputs_m[%d] is not provided", i); end - local layer = self.outputs[i][1] - local port = self.outputs[i][2] - layer.outputs[port] = output[i] + local ref = self.outputs_p[i].ref + local p = self.outputs_p[i].port + ref.outputs_m[p] = outputs_m[i] end end -function DAGLayer:set_err_inputs(bp_err) +function DAGLayer:set_err_inputs(bp_errs_m) for i = 1, #self.dim_out do - local layer = self.outputs[i][1] - local port = self.outputs[i][2] - layer.err_inputs[port] = bp_err[i] + if bp_errs_m[i] == nil then + nerv.error("bp_errs_m[%d] is not provided", i); + end + local ref = self.outputs_p[i].ref + local p = self.outputs_p[i].port + ref.err_inputs_m[p] = bp_errs_m[i] end end +--[[ function DAGLayer:set_err_outputs(next_bp_err) for i = 1, #self.dim_in do local layer = self.inputs[i][1] @@ -219,6 +228,7 @@ function DAGLayer:set_err_outputs(next_bp_err) layer.err_outputs[port] = next_bp_err[i] end end +]]-- function DAGLayer:update(bp_err, input, output) self:set_err_inputs(bp_err) |