From fe54a7c5c8425ee9783d82e16a42924e23add457 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Fri, 13 Jan 2017 07:52:58 +0100 Subject: [PATCH] Cosmetics. --- dagnn.lua | 181 +++++++++++++++++++++++++------------------------ test-dagnn.lua | 5 +- 2 files changed, 93 insertions(+), 93 deletions(-) diff --git a/dagnn.lua b/dagnn.lua index 9203264..14cd582 100755 --- a/dagnn.lua +++ b/dagnn.lua @@ -29,32 +29,6 @@ function DAG:__init() self.node = { } end -function DAG:createNode(nnm) - if not self.node[nnm] then - self:add(nnm) -- Add it to the object as a Container - local node = {} - node.succ = {} - node.pred = {} - node.index = #self.modules - self.node[nnm] = node - end -end - --- The main use should be to add an edge between two modules, but it --- can also add a full sequence of modules -function DAG:connect(...) - self.sorted = nil - local prev - for _, nnm in pairs({...}) do - self:createNode(nnm) - if prev then - table.insert(self.node[nnm].pred, prev) - table.insert(self.node[prev].succ, nnm) - end - prev = nnm - end -end - -- Apply f on t recursively; use the corresponding element from args -- (i.e. same keys) as second parameter to f when available; return -- the results from f, organized in a similarly nested table. @@ -70,36 +44,15 @@ function DAG:nestedApply(f, t, args) end end -function DAG:setInput(i) - self.sorted = nil - self.inputModules = i - self:nestedApply( - function(nnm) - if #self.node[nnm].succ == 0 then - error('Input modules must have outgoing edges.') - end - if #self.node[nnm].pred > 0 then - error('Input modules cannog have incoming edges.') - end - end, - self.inputModules - ) -end - -function DAG:setOutput(o) - self.sorted = nil - self.outputModules = o - self:nestedApply( - function(nnm) - if #self.node[nnm].pred == 0 then - error('Output module must have incoming edges.') - end - if #self.node[nnm].succ > 0 then - error('Output module cannot have outgoing edges.') - end - end, - self.outputModules - ) +function DAG:createNode(nnm) + if not self.node[nnm] then + self:add(nnm) -- Add it to the object as a Container + local node = {} + node.succ = {} + node.pred = {} + node.index = #self.modules + self.node[nnm] = node + end end function DAG:putInOrder() @@ -149,6 +102,54 @@ function DAG:computeGradOutput(gradInputSucc) return gi end +---------------------------------------------------------------------- + +-- Connect a sequence of modules +function DAG:connect(...) + self.sorted = nil + local prev + for _, nnm in pairs({...}) do + self:createNode(nnm) + if prev then + table.insert(self.node[nnm].pred, prev) + table.insert(self.node[prev].succ, nnm) + end + prev = nnm + end +end + +function DAG:setInput(i) + self.sorted = nil + self.inputModules = i + self:nestedApply( + function(nnm) + if #self.node[nnm].succ == 0 then + error('Input modules must have outgoing edges.') + end + if #self.node[nnm].pred > 0 then + error('Input modules cannog have incoming edges.') + end + end, + self.inputModules + ) +end + +function DAG:setOutput(o) + self.sorted = nil + self.outputModules = o + self:nestedApply( + function(nnm) + if #self.node[nnm].pred == 0 then + error('Output module must have incoming edges.') + end + if #self.node[nnm].succ > 0 then + error('Output module cannot have outgoing edges.') + end + end, + self.outputModules + ) +end + function DAG:print() self:putInOrder() @@ -159,6 +160,40 @@ end ---------------------------------------------------------------------- +function DAG:saveDot(filename) + local file = (filename and io.open(filename, 'w')) or io.stdout + + file:write('digraph {\n') + + file:write('\n') + + for nnma, node in pairs(self.node) do + file:write( + ' ' + .. node.index + .. ' [shape=box,label=\"' .. torch.type(nnma) .. '\"]' + .. '\n' + ) + + for _, nnmb in pairs(node.succ) do + file:write( + ' ' + .. node.index + .. ' -> ' + .. self.node[nnmb].index + .. '\n' + ) + end + + file:write('\n') + end + + file:write('}\n') + +end + +---------------------------------------------------------------------- + function DAG:updateOutput(input) self:putInOrder() @@ -259,37 +294,3 @@ function DAG:accGradParameters(input, gradOutput, scale) self:rethrowErrors(nnm, k, 'accGradParameters', node.input, self:computeGradOutput(node.gradInputSucc), scale) end end - ----------------------------------------------------------------------- - -function DAG:dot(filename) - local file = (filename and io.open(filename, 'w')) or io.stdout - - file:write('digraph {\n') - - file:write('\n') - - for nnma, node in pairs(self.node) do - file:write( - ' ' - .. node.index - .. ' [shape=box,label=\"' .. torch.type(nnma) .. '\"]' - .. '\n' - ) - - for _, nnmb in pairs(node.succ) do - file:write( - ' ' - .. node.index - .. ' -> ' - .. self.node[nnmb].index - .. '\n' - ) - end - - file:write('\n') - end - - file:write('}\n') - -end diff --git a/test-dagnn.lua b/test-dagnn.lua index 366e98f..462c287 100755 --- a/test-dagnn.lua +++ b/test-dagnn.lua @@ -92,10 +92,9 @@ c = nn.Linear(10, 15) d = nn.CMulTable() e = nn.CAddTable() -model:connect(a, b) +model:connect(a, b, c) model:connect(b, nn.Linear(10, 15), nn.ReLU(), d) model:connect(d, e) -model:connect(b, c) model:connect(c, d) model:connect(c, nn.Mul(-1), e) @@ -110,4 +109,4 @@ output:uniform() print('Error = ' .. checkGrad(model, nn.MSECriterion(), input, output)) print('Writing /tmp/graph.dot') -model:dot('/tmp/graph.dot') +model:saveDot('/tmp/graph.dot') -- 2.39.5