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 -- Apply f on t recursively; use the corresponding elements from args
33 -- (i.e. same keys) as second parameter to f when available; return
34 -- the results from f, organized in a similarly nested table.
35 function DAG:nestedApply(f, t, args)
36 if torch.type(t) == 'table' then
38 for k, s in pairs(t) do
39 result[k] = self:nestedApply(f, s, args and args[k])
47 function DAG:createNode(nnm)
48 if not self.node[nnm] then
49 self:add(nnm) -- Add it to the object as a Container
53 node.index = #self.modules
58 function DAG:putInOrder()
64 self:nestedApply(function(m) distance[m] = 1 end, self.inputModules)
69 for nnma, node in pairs(self.node) do
70 for _, nnmb in pairs(node.succ) do
71 if distance[nnma] and (not distance[nnmb] or distance[nnmb] < distance[nnma] + 1) then
72 distance[nnmb] = distance[nnma] + 1
79 for _, nnm in pairs(self.modules) do
80 assert(distance[nnm], 'Some modules are not connected to inputs')
84 for m, d in pairs(distance) do
85 table.insert(self.sorted, { distance = d, nnm = m })
88 table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
90 for i, a in ipairs(self.sorted) do self.sorted[i] = a.nnm end
93 -- This accumulates x in a where they are both nested tables of
94 -- tensors. If first is true, set a = x. Behavior is undefined if a
95 -- and x do not have the exact same structure.
96 function DAG:nestedAccTensor(a, x, first)
97 if torch.type(x) == 'table' then
100 b[i] = self:nestedAccTensor(a[i], x[i], first)
106 a:resizeAs(x):copy(x)
117 function DAG:updateGradOutput(node)
118 local gradInputSucc = node.gradInputSucc
119 if #gradInputSucc == 1 then
120 node.gradOutput = gradInputSucc[1]
121 elseif #gradInputSucc > 1 then
122 for k = 1, #gradInputSucc do
123 node.gradOutput = self:nestedAccTensor(node.gradOutput, gradInputSucc[k], k == 1)
128 ----------------------------------------------------------------------
130 -- Connect a sequence of modules
131 function DAG:connect(...)
134 for _, nnm in pairs({...}) do
137 table.insert(self.node[nnm].pred, prev)
138 table.insert(self.node[prev].succ, nnm)
144 function DAG:setInput(i)
146 self.inputModules = i
149 if #self.node[nnm].succ == 0 then
150 error('Input modules must have outgoing edges.')
152 if #self.node[nnm].pred > 0 then
153 error('Input modules cannot have incoming edges.')
160 function DAG:setOutput(o)
162 self.outputModules = o
165 if #self.node[nnm].pred == 0 then
166 error('Output module must have incoming edges.')
168 if #self.node[nnm].succ > 0 then
169 error('Output module cannot have outgoing edges.')
179 for i, d in ipairs(self.sorted) do
180 print('#' .. i .. ' -> ' .. torch.type(d))
184 ----------------------------------------------------------------------
186 function DAG:saveDot(filename)
187 local file = (filename and io.open(filename, 'w')) or io.stdout
189 file:write('digraph {\n')
193 for nnmb, node in pairs(self.node) do
197 .. ' [shape=box,label=\"' .. torch.type(nnmb) .. '\"]'
201 for i, nnma in pairs(node.pred) do
202 local decoration = ''
203 if #node.pred > 1 then
204 -- decoration = ' [headlabel=\"' .. i .. '\"]'
205 decoration = ' [label=\"' .. i .. '\"]'
209 .. self.node[nnma].index
211 .. self.node[nnmb].index
224 ----------------------------------------------------------------------
226 function DAG:updateOutput(input)
231 local node = self.node[nnm]
233 self:rethrowErrors(nnm, node.index, 'updateOutput', i)
239 for _, nnm in ipairs(self.sorted) do
240 local node = self.node[nnm]
241 if #node.pred > 0 then
243 if #node.pred == 1 then
244 i = node.pred[1].output
245 elseif #node.pred > 1 then
247 for k = 1, #node.pred do
248 i[k] = node.pred[k].output
252 self:rethrowErrors(nnm, node.index, 'updateOutput', i)
256 self.output = self:nestedApply(
257 function(m) return m.output end,
264 function DAG:updateGradInput(input, gradOutput)
265 assert(self.sorted, 'There has been a DAG structure change before a DAG:updateGradInput')
269 local node = self.node[nnm]
271 self:rethrowErrors(nnm, node.index, 'updateGradInput', node.input, go)
273 self.outputModules, gradOutput
277 function(nnm, i) self.node[nnm].input = i end,
278 self.inputModules, input
281 for _, node in pairs(self.node) do
282 node.gradInputSucc = {}
285 for k = #self.sorted, 1, -1 do
286 local nnm = self.sorted[k]
287 local node = self.node[nnm]
288 local pred = node.pred
290 if #node.gradInputSucc > 0 then
291 self:updateGradOutput(node)
292 self:rethrowErrors(nnm, node.index, 'updateGradInput', node.input, node.gradOutput)
295 -- We fill the gradInputSucc of our predecessors
297 table.insert(self.node[pred[1]].gradInputSucc, nnm.gradInput)
298 elseif #pred > 1 then
299 if not torch.type(nnm.gradInput) == 'table' then
300 error('Should have a table gradInput since it has multiple predecessors')
303 table.insert(self.node[node.pred[n]].gradInputSucc, nnm.gradInput[n])
308 self.gradInput = self:nestedApply(function(m) return m.gradInput end, self.inputModules)
310 return self.gradInput
313 function DAG:accGradParameters(input, gradOutput, scale)
314 assert(self.sorted, 'There has been a DAG structure change before a DAG:accGradParameters')
317 function(nnm, go) self.node[nnm].gradOutput = go end,
318 self.outputModules, gradOutput
322 function(nnm, i) self.node[nnm].input = i end,
323 self.inputModules, input
326 for k = 1, #self.modules do
327 local nnm = self.modules[k]
328 local node = self.node[nnm]
329 self:rethrowErrors(nnm, k, 'accGradParameters', node.input, node.gradOutput, scale)
333 function DAG:clearState()
335 for _, node in pairs(self.node) do
336 node.gradInputSucc = nil
338 node.gradOutput = nil
340 return parent.clearState(self)