aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortxh18 <[email protected]>2015-11-23 21:45:05 +0800
committertxh18 <[email protected]>2015-11-23 21:45:05 +0800
commit80b18045c2f7d0cc5aba5c4b852694d869c3f830 (patch)
tree8ce2050acbafab9712c144dfde5f535f8325d67c
parent884cb00dd3aca43fbe6d5b72cfc43264cccb8a86 (diff)
completed layerdag_t, now testing...
-rw-r--r--nerv/examples/lmptb/tnn/init.lua (renamed from nerv/examples/lmptb/rnn/init.lua)1
-rw-r--r--nerv/examples/lmptb/tnn/layer_dag_t.lua369
-rw-r--r--nerv/examples/lmptb/tnn/layers/gate_fff.lua (renamed from nerv/examples/lmptb/rnn/layers/gate_fff.lua)0
-rw-r--r--nerv/examples/lmptb/tnn/layersT/softmax_ce_t.lua (renamed from nerv/examples/lmptb/rnn/layersT/softmax_ce_t.lua)0
-rw-r--r--nerv/examples/lmptb/tnn/tnn.lua (renamed from nerv/examples/lmptb/rnn/tnn.lua)0
-rw-r--r--nerv/examples/lmptb/tnn_ptb_main.lua2
6 files changed, 371 insertions, 1 deletions
diff --git a/nerv/examples/lmptb/rnn/init.lua b/nerv/examples/lmptb/tnn/init.lua
index 6507582..a069527 100644
--- a/nerv/examples/lmptb/rnn/init.lua
+++ b/nerv/examples/lmptb/tnn/init.lua
@@ -44,3 +44,4 @@ end
nerv.include('tnn.lua')
nerv.include('layersT/softmax_ce_t.lua')
nerv.include('layers/gate_fff.lua')
+nerv.include('layer_dag_t.lua')
diff --git a/nerv/examples/lmptb/tnn/layer_dag_t.lua b/nerv/examples/lmptb/tnn/layer_dag_t.lua
new file mode 100644
index 0000000..1a89816
--- /dev/null
+++ b/nerv/examples/lmptb/tnn/layer_dag_t.lua
@@ -0,0 +1,369 @@
+local DAGLayerT = nerv.class("nerv.DAGLayerT", "nerv.LayerT")
+
+local function parse_id(str)
+ local id, port, _
+ _, _, id, port = string.find(str, "([a-zA-Z0-9_]+)%[([0-9]+)%]")
+ if id == nil or port == nil then
+ _, _, id, port = string.find(str, "(.+)%[([0-9]+)%]")
+ if not (id == "<input>" or id == "<output>") then
+ nerv.error("wrong format of connection id")
+ end
+ end
+ port = tonumber(port)
+ return id, port
+end
+
+local function discover(id, layers, layer_repo)
+ local ref = layers[id]
+ if id == "<input>" or id == "<output>" then
+ return nil
+ end
+ if ref == nil then
+ local layer = layer_repo:get_layer(id)
+ local dim_in, dim_out = layer:get_dim()
+ ref = {
+ layer = layer,
+ inputs = {},
+ outputs = {},
+ err_inputs = {},
+ err_outputs = {},
+ next_layers = {},
+ input_len = #dim_in,
+ output_len = #dim_out,
+ in_deg = 0,
+ visited = false
+ }
+ layers[id] = ref
+ end
+ return ref
+end
+
+function DAGLayerT:__init(id, global_conf, layer_conf)
+ local layers = {}
+ local inputs = {}
+ local outputs = {}
+ local dim_in = layer_conf.dim_in
+ local dim_out = layer_conf.dim_out
+ local parsed_conn = {}
+ for from, to in pairs(layer_conf.connections) do
+ local id_from, port_from = parse_id(from)
+ local id_to, port_to = parse_id(to)
+ local ref_from = discover(id_from, layers, layer_conf.sub_layers)
+ local ref_to = discover(id_to, layers, layer_conf.sub_layers)
+ local input_dim, output_dim, _
+ if ref_from and ref_from.outputs[port_from] ~= nil then
+ nerv.error("%s has already been attached", from)
+ end
+ if ref_to and ref_to.inputs[port_to] ~= nil then
+ nerv.error("%s has already been attached", to)
+ end
+ if id_from == "<input>" then
+ input_dim, _ = ref_to.layer:get_dim()
+ if dim_in[port_from] ~= input_dim[port_to] then
+ nerv.error("mismatching data dimension between %s and %s", from, to)
+ end
+ inputs[port_from] = {ref_to, port_to}
+ ref_to.inputs[port_to] = inputs -- just a place holder
+ elseif id_to == "<output>" then
+ _, output_dim = ref_from.layer:get_dim()
+ if output_dim[port_from] ~= dim_out[port_to] then
+ nerv.error("mismatching data dimension between %s and %s", from, to)
+ end
+ outputs[port_to] = {ref_from, port_from}
+ ref_from.outputs[port_from] = outputs -- just a place holder
+ else
+ _, output_dim = ref_from.layer:get_dim()
+ input_dim, _ = ref_to.layer:get_dim()
+ if output_dim[port_from] ~= input_dim[port_to] then
+ nerv.error("mismatching data dimension between %s and %s", from, to)
+ end
+
+ table.insert(parsed_conn,
+ {{ref_from, port_from}, {ref_to, port_to}})
+ table.insert(ref_from.next_layers, ref_to) -- add edge
+ ref_to.in_deg = ref_to.in_deg + 1 -- increase the in-degree of the target layer
+ end
+ end
+
+ -- topology sort
+ local queue = {}
+ local l = 1
+ local r = 1
+ for id, ref in pairs(layers) do
+ if ref.in_deg == 0 then
+ table.insert(queue, ref)
+ nerv.info("adding source layer: %s", id)
+ r = r + 1
+ end
+ end
+ if l == r then
+ nerv.error("loop detected")
+ end
+ while l < r do
+ local cur = queue[l]
+ cur.visited = true
+ l = l + 1
+ for _, nl in pairs(cur.next_layers) do
+ nl.in_deg = nl.in_deg - 1
+ if nl.in_deg == 0 then
+ table.insert(queue, nl)
+ r = r + 1
+ end
+ end
+ end
+ for i = 1, #queue do
+ nerv.info("enqueued layer: %s %s", queue[i].layer, queue[i].layer.id)
+ end
+
+ for id, ref in pairs(layers) do
+ -- check wether the graph is connected
+ if ref.visited == false then
+ nerv.warning("layer %s is ignored", id)
+ end
+ end
+
+ 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
+ self.queue = queue
+ self.gconf = global_conf
+end
+
+function DAGLayerT:init(batch_size, chunk_size)
+ nerv.printf("initing DAGLayerT\n")
+ if chunk_size == nil then
+ chunk_size = 1
+ nerv.printf("(Initing DAGLayerT) chunk_size is nil, setting it to default 1\n")
+ end
+
+ self.chunk_size = chunk_size
+
+ for i, conn in ipairs(self.parsed_conn) do
+ local _, output_dim
+ local ref_from, port_from, ref_to, port_to
+ ref_from, port_from = unpack(conn[1])
+ ref_to, port_to = unpack(conn[2])
+ _, output_dim = ref_from.layer:get_dim()
+ local dim = 1
+ if output_dim[port_from] > 0 then
+ dim = output_dim[port_from]
+ end
+
+ for t = 1, chunk_size do
+ local mid = self.gconf.cumat_type(batch_size, dim)
+ local err_mid = mid:create()
+
+ if ref_from.outputs[t] == nil then
+ ref_from.outputs[t] = {}
+ end
+ if ref_to.inputs[t] == nil then
+ ref_to.inputs[t] = {}
+ end
+ if ref_to.err_outputs[t] == nil then
+ ref_to.err_outputs[t] = {}
+ end
+ if ref_from.err_inputs[t] == nil then
+ ref_from.err_inputs[t] = {}
+ end
+
+ ref_from.outputs[t][port_from] = mid
+ ref_to.inputs[t][port_to] = mid
+
+ ref_from.err_inputs[t][port_from] = err_mid
+ ref_to.err_outputs[t][port_to] = err_mid
+ end
+ end
+ for id, ref in pairs(self.layers) do
+ for i = 1, ref.input_len do
+ if ref.inputs[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
+ nerv.error("dangling output port %d of layer %s", i, id)
+ end
+ end
+ -- initialize sub layers
+ ref.layer:init(batch_size, chunk_size)
+ end
+ for i = 1, #self.dim_in do
+ if self.inputs[i] == nil then
+ nerv.error("dangling port %d of layer <input>", 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)
+ end
+ end
+end
+
+function DAGLayerT:batch_resize(batch_size, chunk_size)
+ if chunk_size == nil then
+ chunk_size = 1
+ end
+ if batch_size ~= self.gconf.batch_size
+ or chunk_size ~= self.gconf.chunk_size then
+ nerv.printf("warn: in DAGLayerT:batch_resize, the batch_size ~= gconf.batch_size, or chunk_size ~= gconf.chunk_size")
+ end
+ self.gconf.batch_size = batch_size
+ self.gconf.chunk_size = chunk_size
+
+ for i, conn in ipairs(self.parsed_conn) do
+ local _, output_dim
+ local ref_from, port_from, ref_to, port_to
+ ref_from, port_from = unpack(conn[1])
+ ref_to, port_to = unpack(conn[2])
+ _, output_dim = ref_from.layer:get_dim()
+
+ for t = 1, chunk_size do
+ if ref_from.outputs[t] == nil then
+ ref_from.outputs[t] = {}
+ end
+ if ref_to.inputs[t] == nil then
+ ref_to.inputs[t] = {}
+ end
+ if ref_from.err_outputs[t] == nil then
+ ref_from.err_outputs[t] = {}
+ end
+ if ref_from.err_inputs[t] == nil then
+ ref_from.err_inputs[t] = {}
+ end
+
+ local mid = self.gconf.cumat_type(batch_size, dim)
+ local err_mid = mid:create()
+
+ ref_from.outputs[t][port_from] = mid
+ ref_to.inputs[t][port_to] = mid
+
+ ref_from.err_inputs[t][port_from] = err_mid
+ ref_to.err_outputs[t][port_to] = err_mid
+ end
+ end
+ for id, ref in pairs(self.layers) do
+ ref.layer:batch_resize(batch_size, chunk_size)
+ end
+ collectgarbage("collect")
+end
+
+function DAGLayerT:set_inputs(input, t)
+ for i = 1, #self.dim_in do
+ if input[i] == nil then
+ nerv.error("some input is not provided");
+ end
+ local layer = self.inputs[i][1]
+ local port = self.inputs[i][2]
+ layer.inputs[t][port] = input[i]
+ end
+end
+
+function DAGLayerT:set_outputs(output, t)
+ for i = 1, #self.dim_out do
+ if output[i] == nil then
+ nerv.error("some output is not provided");
+ end
+ local layer = self.outputs[i][1]
+ local port = self.outputs[i][2]
+ layer.outputs[t][port] = output[i]
+ end
+end
+
+function DAGLayerT:set_err_inputs(bp_err, t)
+ for i = 1, #self.dim_out do
+ local layer = self.outputs[i][1]
+ local port = self.outputs[i][2]
+ layer.err_inputs[t][port] = bp_err[i]
+ end
+end
+
+function DAGLayerT:set_err_outputs(next_bp_err, t)
+ for i = 1, #self.dim_in do
+ local layer = self.inputs[i][1]
+ local port = self.inputs[i][2]
+ layer.err_outputs[t][port] = next_bp_err[i]
+ end
+end
+
+function DAGLayerT:update(bp_err, input, output, t)
+ if t == nil then
+ t = 1
+ end
+ self:set_err_inputs(bp_err, t)
+ self:set_inputs(input, t)
+ self:set_outputs(output, t)
+ -- print("update")
+ for id, ref in pairs(self.queue) do
+ -- print(ref.layer.id)
+ ref.layer:update(ref.err_inputs, ref.inputs, ref.outputs, t)
+ end
+end
+
+function DAGLayerT:propagate(input, output)
+ if t == nil then
+ t = 1
+ end
+ self:set_inputs(input, t)
+ self:set_outputs(output, t)
+ local ret = false
+ for i = 1, #self.queue do
+ local ref = self.queue[i]
+ -- print(ref.layer.id)
+ ret = ref.layer:propagate(ref.inputs, ref.outputs, t)
+ end
+ return ret
+end
+
+function DAGLayerT:back_propagate(bp_err, next_bp_err, input, output, t)
+ if t == nil then
+ t = 1
+ end
+ self:set_err_outputs(next_bp_err, t)
+ self:set_err_inputs(bp_err, t)
+ self:set_inputs(input, t)
+ self:set_outputs(output, t)
+ for i = #self.queue, 1, -1 do
+ local ref = self.queue[i]
+ -- print(ref.layer.id)
+ ref.layer:back_propagate(ref.err_inputs, ref.err_outputs, ref.inputs, ref.outputs, t)
+ end
+end
+
+function DAGLayerT:get_params()
+ local param_repos = {}
+ for id, ref in pairs(self.queue) do
+ table.insert(param_repos, ref.layer:get_params())
+ end
+ return nerv.ParamRepo.merge(param_repos)
+end
+
+DAGLayerT.PORT_TYPES = {
+ INPUT = {},
+ OUTPUT = {},
+ ERR_INPUT = {},
+ ERR_OUTPUT = {}
+}
+
+function DAGLayerT:get_intermediate(id, port_type)
+ if id == "<input>" or id == "<output>" then
+ nerv.error("an actual real layer id is expected")
+ end
+ local layer = self.layers[id]
+ if layer == nil then
+ nerv.error("layer id %s not found", id)
+ end
+ if port_type == DAGLayerT.PORT_TYPES.INPUT then
+ return layer.inputs
+ elseif port_type == DAGLayerT.PORT_TYPES.OUTPUT then
+ return layer.outputs
+ elseif port_type == DAGLayerT.PORT_TYPES.ERR_INPUT then
+ return layer.err_inputs
+ elseif port_type == DAGLayerT.PORT_TYPES.ERR_OUTPUT then
+ return layer.err_outputs
+ end
+ nerv.error("unrecognized port type")
+end
diff --git a/nerv/examples/lmptb/rnn/layers/gate_fff.lua b/nerv/examples/lmptb/tnn/layers/gate_fff.lua
index 751dde1..751dde1 100644
--- a/nerv/examples/lmptb/rnn/layers/gate_fff.lua
+++ b/nerv/examples/lmptb/tnn/layers/gate_fff.lua
diff --git a/nerv/examples/lmptb/rnn/layersT/softmax_ce_t.lua b/nerv/examples/lmptb/tnn/layersT/softmax_ce_t.lua
index dddb05a..dddb05a 100644
--- a/nerv/examples/lmptb/rnn/layersT/softmax_ce_t.lua
+++ b/nerv/examples/lmptb/tnn/layersT/softmax_ce_t.lua
diff --git a/nerv/examples/lmptb/rnn/tnn.lua b/nerv/examples/lmptb/tnn/tnn.lua
index c2e397c..c2e397c 100644
--- a/nerv/examples/lmptb/rnn/tnn.lua
+++ b/nerv/examples/lmptb/tnn/tnn.lua
diff --git a/nerv/examples/lmptb/tnn_ptb_main.lua b/nerv/examples/lmptb/tnn_ptb_main.lua
index 3096a3f..66c7317 100644
--- a/nerv/examples/lmptb/tnn_ptb_main.lua
+++ b/nerv/examples/lmptb/tnn_ptb_main.lua
@@ -2,7 +2,7 @@ require 'lmptb.lmvocab'
require 'lmptb.lmfeeder'
require 'lmptb.lmutil'
require 'lmptb.layer.init'
-require 'rnn.init'
+require 'tnn.init'
require 'lmptb.lmseqreader'
require 'lm_trainer'