5 local DAG, parent = torch.class('nn.DAG', 'nn.Container')
9 -- Nodes are indexed by the module they contain
13 function DAG:createNode(nnm)
14 if not self.node[nnm] then
15 self:add(nnm) -- Add it to the object as a Container
17 self.node[nnm].succ = {}
18 self.node[nnm].pred = {}
22 function DAG:addEdge(nnma, nnmb)
26 table.insert(self.node[nnmb].pred, nnma)
27 table.insert(self.node[nnma].succ, nnmb)
30 -- Apply f on t recursively; use the corresponding element from args
31 -- (i.e. same keys) as second parameter to f when available; return
32 -- the results from f, organized in a similarly nested table.
33 function DAG:nestedApply(f, t, args)
34 if torch.type(t) == 'table' then
36 for k, s in pairs(t) do
37 result[k] = self:nestedApply(f, s, args and args[k])
45 function DAG:setInput(i)
50 if #self.node[nnm].succ == 0 then
51 error('Input modules must have outgoing edges.')
53 if #self.node[nnm].pred > 0 then
54 error('Input modules cannog have incoming edges.')
61 function DAG:setOutput(o)
63 self.outputModules = o
66 if #self.node[nnm].pred == 0 then
67 error('Output module must have incoming edges.')
69 if #self.node[nnm].succ > 0 then
70 error('Output module cannot have outgoing edges.')
77 function DAG:putInOrder()
82 -- First, we sort the nodes according to the DAG order
86 self:nestedApply(function(m) distance[m] = 1 end, self.inputModules)
92 for nnma, node in pairs(self.node) do
93 for _, nnmb in pairs(node.succ) do
94 if distance[nnma] and (not distance[nnmb] or distance[nnmb] < distance[nnma] + 1) then
95 distance[nnmb] = distance[nnma] + 1
103 for m, d in pairs(distance) do
104 table.insert(self.sorted, { distance = d, nnm = m })
107 table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
109 for i, a in ipairs(self.sorted) do self.sorted[i] = a.nnm end
115 for i, d in ipairs(self.sorted) do
116 print('#' .. i .. ' -> ' .. torch.type(d))
120 function DAG:updateOutput(input)
125 self.node[nnm].input = i
132 for _, nnm in ipairs(self.sorted) do
133 local node = self.node[nnm]
134 if #node.pred > 0 then
136 if #node.pred == 1 then
137 i = node.pred[1].output
138 elseif #node.pred > 1 then
140 for k = 1, #node.pred do
141 i[k] = node.pred[k].output
149 self.output = self:nestedApply(
150 function(m) return m.output end,
157 function DAG:computeGradInput(gradInputSucc)
159 if #gradInputSucc == 1 then
160 gi = gradInputSucc[1] -- we avoid a clone()
161 elseif #gradInputSucc > 1 then
162 for k = 1, #gradInputSucc do
164 gi:add(gradInputSucc[k])
166 gi = gradInputSucc[k]:clone()
173 function DAG:updateGradInput(input, gradOutput)
177 function(nnm, go) nnm:updateGradInput(self.node[nnm].input, go) end,
178 self.outputModules, gradOutput
182 function(nnm, i) self.node[nnm].input = i end,
183 self.inputModules, input
186 for _, node in pairs(self.node) do
187 node.gradInputSucc = {}
190 for k = #self.sorted, 1, -1 do
191 local nnm = self.sorted[k]
192 local node = self.node[nnm]
193 local pred, gradInputSucc = node.pred, node.gradInputSucc
195 if #gradInputSucc > 0 then
196 nnm:updateGradInput(node.input, self:computeGradInput(gradInputSucc))
199 -- We fill the gradInputSucc of our predecessors
201 table.insert(self.node[pred[1]].gradInputSucc, nnm.gradInput)
202 elseif #pred > 1 then
203 if not torch.type(nnm.gradInput) == 'table' then
204 error('Should have a table gradInput since it has multiple predecessors')
207 table.insert(self.node[node.pred[n]].gradInputSucc, nnm.gradInput[n])
212 self.gradInput = self:nestedApply(function(m) return m.gradInput end, self.inputModules)
214 return self.gradInput
217 function DAG:accGradParameters(input, gradOutput, scale)
223 function(nnm, go) nnm:updateGradInput(self.node[nnm].input, go) end,
224 self.outputModules, gradOutput
228 function(nnm, i) self.node[nnm].input = i end,
229 self.inputModules, input
232 for k = #self.sorted, 1, -1 do
233 local nnm = self.sorted[k]
234 local node = self.node[nnm]
235 nnm:accGradParameters(node.input, self:computeGradInput(node.gradInputSucc), scale)