From: Francois Fleuret Date: Fri, 13 Jan 2017 06:52:58 +0000 (+0100) Subject: Cosmetics. X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=dagnn.git;a=commitdiff_plain;h=fe54a7c5c8425ee9783d82e16a42924e23add457 Cosmetics. --- 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')