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.
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()
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()
----------------------------------------------------------------------
+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()
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