5 local DAG, parent = torch.class('nn.DAG', 'nn.Container')
9 -- Nodes are indexed by the module they encompass
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 a1 and a2 elements
31 -- (i.e. same keys) as second and third parameters to f when
32 -- available; return the results from f, organized in a similarly
34 function DAG:nestApply(f, t, a1, a2)
35 if torch.type(t) == 'table' then
37 for k, s in pairs(t) do
38 result[k] = self:nestApply(f, s, a1 and a1[k], a2 and a2[k])
46 function DAG:setInput(i)
51 if #self.node[nnm].succ == 0 then
52 error('Input modules must have outgoing edges.')
54 if #self.node[nnm].pred > 0 then
55 error('Input modules cannog have incoming edges.')
62 function DAG:setOutput(o)
64 self.outputModules = o
67 if #self.node[nnm].pred == 0 then
68 error('Output module must have incoming edges.')
70 if #self.node[nnm].succ > 0 then
71 error('Output module cannot have outgoing edges.')
78 function DAG:putInOrder()
83 -- First, we sort the nodes according to the DAG order
87 self:nestApply(function(m) distance[m] = 1 end, self.inputModules)
93 for nnma, node in pairs(self.node) do
94 for _, nnmb in pairs(node.succ) do
95 if distance[nnma] and (not distance[nnmb] or distance[nnmb] < distance[nnma] + 1) then
96 distance[nnmb] = distance[nnma] + 1
104 for m, d in pairs(distance) do
105 table.insert(self.sorted, { distance = d, nnm = m })
108 table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
110 for i, a in ipairs(self.sorted) do self.sorted[i] = a.nnm end
116 for i, d in ipairs(self.sorted) do
117 print('#' .. i .. ' -> ' .. torch.type(d))
121 function DAG:updateOutput(input)
126 self.node[nnm].input = i
133 for _, nnm in ipairs(self.sorted) do
134 local node = self.node[nnm]
135 if #node.pred > 0 then
137 if #node.pred == 1 then
138 i = node.pred[1].output
139 elseif #node.pred > 1 then
141 for k = 1, #node.pred do
142 i[k] = node.pred[k].output
150 self.output = self:nestApply(function(m) return m.output end, self.outputModules)
155 function DAG:computeGradInput(gradInputSucc)
157 if #gradInputSucc == 1 then
158 gi = gradInputSucc[1] -- we avoid a clone()
159 elseif #gradInputSucc > 1 then
160 for k = 1, #gradInputSucc do
162 gi:add(gradInputSucc[k])
164 gi = gradInputSucc[k]:clone()
171 function DAG:updateGradInput(input, gradOutput)
175 function(nnm, go) nnm:updateGradInput(self.node[nnm].input, go) end,
176 self.outputModules, gradOutput
180 function(nnm, i) self.node[nnm].input = i end,
181 self.inputModules, input
184 for _, node in pairs(self.node) do
185 node.gradInputSucc = {}
188 for k = #self.sorted, 1, -1 do
189 local nnm = self.sorted[k]
190 local node = self.node[nnm]
191 local pred, gradInputSucc = node.pred, node.gradInputSucc
193 if #gradInputSucc > 0 then
194 nnm:updateGradInput(node.input, self:computeGradInput(gradInputSucc))
197 -- We fill the gradInputSucc of our predecessors
199 table.insert(self.node[pred[1]].gradInputSucc, nnm.gradInput)
200 elseif #pred > 1 then
201 if not torch.type(nnm.gradInput) == 'table' then
202 error('Should have a table gradInput since it has multiple predecessors')
205 table.insert(self.node[node.pred[n]].gradInputSucc, nnm.gradInput[n])
210 self.gradInput = self:nestApply(function(m) return m.gradInput end, self.inputModules)
212 return self.gradInput