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
80 for m, d in pairs(distance) do
81 table.insert(self.sorted, { distance = d, nnm = m })
84 table.sort(self.sorted, function(a, b) return a.distance < b.distance end)
86 for i, a in ipairs(self.sorted) do self.sorted[i] = a.nnm end
89 function DAG:updateGradOutput(node)
90 local gradInputSucc = node.gradInputSucc
91 if #gradInputSucc == 1 then
92 node.gradOutput = gradInputSucc[1]
93 elseif #gradInputSucc > 1 then
94 if node.gradOutput then
95 node.gradOutput:resize(gradInputSucc[1]):copy(gradInputSucc[1])
97 node.gradOutput = gradInputSucc[1]:clone()
99 for k = 2, #gradInputSucc do
100 node.gradOutput:add(gradInputSucc[k])
105 ----------------------------------------------------------------------
107 -- Connect a sequence of modules
108 function DAG:connect(...)
111 for _, nnm in pairs({...}) do
114 table.insert(self.node[nnm].pred, prev)
115 table.insert(self.node[prev].succ, nnm)
121 function DAG:setInput(i)
123 self.inputModules = i
126 if #self.node[nnm].succ == 0 then
127 error('Input modules must have outgoing edges.')
129 if #self.node[nnm].pred > 0 then
130 error('Input modules cannog have incoming edges.')
137 function DAG:setOutput(o)
139 self.outputModules = o
142 if #self.node[nnm].pred == 0 then
143 error('Output module must have incoming edges.')
145 if #self.node[nnm].succ > 0 then
146 error('Output module cannot have outgoing edges.')
156 for i, d in ipairs(self.sorted) do
157 print('#' .. i .. ' -> ' .. torch.type(d))
161 ----------------------------------------------------------------------
163 function DAG:saveDot(filename)
164 local file = (filename and io.open(filename, 'w')) or io.stdout
166 file:write('digraph {\n')
170 for nnmb, node in pairs(self.node) do
174 .. ' [shape=box,label=\"' .. torch.type(nnmb) .. '\"]'
178 for i, nnma in pairs(node.pred) do
179 local decoration = ''
180 if #node.pred > 1 then
181 -- decoration = ' [headlabel=\"' .. i .. '\"]'
182 decoration = ' [label=\"' .. i .. '\"]'
186 .. self.node[nnma].index
188 .. self.node[nnmb].index
201 ----------------------------------------------------------------------
203 function DAG:updateOutput(input)
208 self.node[nnm].input = i
209 self:rethrowErrors(nnm, self.node[nnm].index, 'updateOutput', i)
215 for _, nnm in ipairs(self.sorted) do
216 local node = self.node[nnm]
217 if #node.pred > 0 then
219 if #node.pred == 1 then
220 i = node.pred[1].output
221 elseif #node.pred > 1 then
223 for k = 1, #node.pred do
224 i[k] = node.pred[k].output
228 self:rethrowErrors(nnm, self.node[nnm].index, 'updateOutput', i)
232 self.output = self:nestedApply(
233 function(m) return m.output end,
240 function DAG:updateGradInput(input, gradOutput)
241 assert(self.sorted, 'There has been a DAG structure change before a DAG:updateGradInput')
245 local node = self.node[nnm]
247 self:rethrowErrors(nnm, node.index, 'updateGradInput', self.node[nnm].input, go)
249 self.outputModules, gradOutput
253 function(nnm, i) self.node[nnm].input = i end,
254 self.inputModules, input
257 for _, node in pairs(self.node) do
258 node.gradInputSucc = {}
261 for k = #self.sorted, 1, -1 do
262 local nnm = self.sorted[k]
263 local node = self.node[nnm]
264 local pred = node.pred
266 if #node.gradInputSucc > 0 then
267 self:updateGradOutput(node)
268 self:rethrowErrors(nnm, self.node[nnm].index, 'updateGradInput', node.input, node.gradOutput)
271 -- We fill the gradInputSucc of our predecessors
273 table.insert(self.node[pred[1]].gradInputSucc, nnm.gradInput)
274 elseif #pred > 1 then
275 if not torch.type(nnm.gradInput) == 'table' then
276 error('Should have a table gradInput since it has multiple predecessors')
279 table.insert(self.node[node.pred[n]].gradInputSucc, nnm.gradInput[n])
284 self.gradInput = self:nestedApply(function(m) return m.gradInput end, self.inputModules)
286 return self.gradInput
289 function DAG:accGradParameters(input, gradOutput, scale)
292 assert(self.sorted, 'There has been a DAG structure change before a DAG:accGradParameters')
295 function(nnm, go) self.node[nnm].gradOutput = go end,
296 self.outputModules, gradOutput
300 function(nnm, i) self.node[nnm].input = i end,
301 self.inputModules, input
304 for k = 1, #self.modules do
305 local nnm = self.modules[k]
306 local node = self.node[nnm]
307 self:rethrowErrors(nnm, k, 'accGradParameters', node.input, node.gradOutput, scale)