X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=dagnn.lua;h=14cd5821c2064ac74b50dde101449d10d8f2274e;hb=fe54a7c5c8425ee9783d82e16a42924e23add457;hp=7fc1018f8dd7f3f88021c914608c5af1f5a710a5;hpb=063f198047f0202fa921aa09b772369b14ae8be2;p=dagnn.git diff --git a/dagnn.lua b/dagnn.lua index 7fc1018..14cd582 100755 --- a/dagnn.lua +++ b/dagnn.lua @@ -29,25 +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 - -function DAG:addEdge(nnma, nnmb) - self.sorted = nil - self:createNode(nnma) - self:createNode(nnmb) - table.insert(self.node[nnmb].pred, nnma) - table.insert(self.node[nnma].succ, nnmb) -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. @@ -63,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() @@ -142,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() @@ -152,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() @@ -252,5 +294,3 @@ function DAG:accGradParameters(input, gradOutput, scale) self:rethrowErrors(nnm, k, 'accGradParameters', node.input, self:computeGradOutput(node.gradInputSucc), scale) end end - -----------------------------------------------------------------------