4 Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
5 Written by Francois Fleuret <francois.fleuret@idiap.ch>
7 This file is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 3 as
9 published by the Free Software Foundation.
11 It is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this file. If not, see <http://www.gnu.org/licenses/>.
24 local DAG, parent = torch.class('nn.DAG', 'nn.Container')
28 -- Nodes are indexed by the module they contain
32 function DAG:createNode(nnm)
33 if not self.node[nnm] then
34 self:add(nnm) -- Add it to the object as a Container
36 self.node[nnm].succ = {}
37 self.node[nnm].pred = {}
41 function DAG:addEdge(nnma, nnmb)
45 table.insert(self.node[nnmb].pred, nnma)
46 table.insert(self.node[nnma].succ, nnmb)
49 -- Apply f on t recursively; use the corresponding element from args
50 -- (i.e. same keys) as second parameter to f when available; return
51 -- the results from f, organized in a similarly nested table.
52 function DAG:nestedApply(f, t, args)
53 if torch.type(t) == 'table' then
55 for k, s in pairs(t) do
56 result[k] = self:nestedApply(f, s, args and args[k])
64 function DAG:setInput(i)
69 if #self.node[nnm].succ == 0 then
70 error('Input modules must have outgoing edges.')
72 if #self.node[nnm].pred > 0 then
73 error('Input modules cannog have incoming edges.')
80 function DAG:setOutput(o)
82 self.outputModules = o
85 if #self.node[nnm].pred == 0 then
86 error('Output module must have incoming edges.')
88 if #self.node[nnm].succ > 0 then
89 error('Output module cannot have outgoing edges.')
96 function DAG:putInOrder()
101 -- First, we sort the nodes according to the DAG order
105 self:nestedApply(function(m) distance[m] = 1 end, self.inputModules)
111 for nnma, node in pairs(self.node) do
112 for _, nnmb in pairs(node.succ) do
113 if distance[nnma] and (not distance[nnmb] or distance[nnmb] < distance[nnma] + 1) then
114 distance[nnmb] = distance[nnma] + 1
122 for m, d in pairs(distance) do
123 table.insert(self.sorted, { distance = d, nnm = m })
126 table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
128 for i, a in ipairs(self.sorted) do self.sorted[i] = a.nnm end
134 for i, d in ipairs(self.sorted) do
135 print('#' .. i .. ' -> ' .. torch.type(d))
139 function DAG:updateOutput(input)
144 self.node[nnm].input = i
151 for _, nnm in ipairs(self.sorted) do
152 local node = self.node[nnm]
153 if #node.pred > 0 then
155 if #node.pred == 1 then
156 i = node.pred[1].output
157 elseif #node.pred > 1 then
159 for k = 1, #node.pred do
160 i[k] = node.pred[k].output
168 self.output = self:nestedApply(
169 function(m) return m.output end,
176 function DAG:computeGradInput(gradInputSucc)
178 if #gradInputSucc == 1 then
179 gi = gradInputSucc[1] -- we avoid a clone()
180 elseif #gradInputSucc > 1 then
181 for k = 1, #gradInputSucc do
183 gi:add(gradInputSucc[k])
185 gi = gradInputSucc[k]:clone()
192 function DAG:updateGradInput(input, gradOutput)
196 function(nnm, go) nnm:updateGradInput(self.node[nnm].input, go) end,
197 self.outputModules, gradOutput
201 function(nnm, i) self.node[nnm].input = i end,
202 self.inputModules, input
205 for _, node in pairs(self.node) do
206 node.gradInputSucc = {}
209 for k = #self.sorted, 1, -1 do
210 local nnm = self.sorted[k]
211 local node = self.node[nnm]
212 local pred, gradInputSucc = node.pred, node.gradInputSucc
214 if #gradInputSucc > 0 then
215 nnm:updateGradInput(node.input, self:computeGradInput(gradInputSucc))
218 -- We fill the gradInputSucc of our predecessors
220 table.insert(self.node[pred[1]].gradInputSucc, nnm.gradInput)
221 elseif #pred > 1 then
222 if not torch.type(nnm.gradInput) == 'table' then
223 error('Should have a table gradInput since it has multiple predecessors')
226 table.insert(self.node[node.pred[n]].gradInputSucc, nnm.gradInput[n])
231 self.gradInput = self:nestedApply(function(m) return m.gradInput end, self.inputModules)
233 return self.gradInput
236 function DAG:accGradParameters(input, gradOutput, scale)
242 function(nnm, go) nnm:updateGradInput(self.node[nnm].input, go) end,
243 self.outputModules, gradOutput
247 function(nnm, i) self.node[nnm].input = i end,
248 self.inputModules, input
251 for k = #self.sorted, 1, -1 do
252 local nnm = self.sorted[k]
253 local node = self.node[nnm]
254 nnm:accGradParameters(node.input, self:computeGradInput(node.gradInputSucc), scale)