Update.
[dagnn.git] / dagnn.lua
1
2 require 'torch'
3 require 'nn'
4
5 local DAG, parent = torch.class('nn.DAG', 'nn.Container')
6
7 function DAG:__init()
8    parent.__init(self)
9    self.pred = {}
10    self.succ = {}
11 end
12
13 function DAG:addEdge(a, b)
14    self.sorted = nil
15    local pred, succ = self.pred, self.succ
16    if not pred[a] and not succ[a] then
17       self:add(a)
18    end
19    if not pred[b] and not succ[b] then
20       self:add(b)
21    end
22    pred[b] = pred[b] or {}
23    pred[b][#pred[b] + 1] = a
24    succ[a] = succ[a] or {}
25    succ[a][#succ[a] + 1] = b
26 end
27
28 -- Apply f on t recursively; use the corresponding a1 and a2 elements
29 -- (i.e. same keys) as second and third parameters to f when
30 -- available; return the results from f, organized in a similarly
31 -- nested table.
32 function DAG:applyOnModules(f, t, a1, a2)
33    if torch.type(t) == 'table' then
34       local result = {}
35       for k, s in pairs(t) do
36          result[k] = self:applyOnModules(f, s, a1 and a1[k], a2 and a2[k])
37       end
38       return result
39    else
40       return f(t, a1, a2)
41    end
42 end
43
44 function DAG:setInput(i)
45    self.sorted = nil
46    self.inputModules = i
47    self:applyOnModules(
48       function(m)
49          if (not self.succ[m] or #self.succ[m] == 0) or (self.pred[m] and #self.pred[m] > 0) then
50             error('Invalid input edges.')
51          end
52       end,
53       self.inputModules
54    )
55 end
56
57 function DAG:setOutput(o)
58    self.sorted = nil
59    self.outputModules = o
60    self:applyOnModules(
61       function(m)
62          if (not self.pred[m] or #self.pred[m] == 0) or (self.succ[m] and #self.succ[m] > 0) then
63             error('Invalid output edges.')
64          end
65       end,
66       self.outputModules
67    )
68 end
69
70 function DAG:sort()
71    if self.sorted then
72       return
73    end
74
75    local distance = {}
76
77    self:applyOnModules(function(m) distance[m] = 1 end, self.inputModules)
78
79    local nc
80
81    repeat
82       nc = 0
83       for i, isucc in pairs(self.succ) do
84          for _, j in pairs(isucc) do
85             if distance[i] and (not distance[j] or distance[j] < distance[i] + 1) then
86                distance[j] = distance[i] + 1
87                nc = nc + 1
88             end
89          end
90       end
91    until nc == 0
92
93    self.sorted = { }
94    for i, d in pairs(distance) do
95       table.insert(self.sorted, { d, i })
96    end
97
98    table.sort(self.sorted, function(a, b) return a[1] < b[1] end)
99    for i, a in ipairs(self.sorted) do self.sorted[i] = a[2] end
100 end
101
102 function DAG:print()
103    self:sort()
104
105    for i, d in ipairs(self.sorted) do
106       print('#' .. i .. ' -> ' .. torch.type(d))
107    end
108 end
109
110 function DAG:updateOutput(input)
111    self:sort()
112
113    self:applyOnModules(function(m, i) m:updateOutput(i) end, self.inputModules, input)
114
115    for _, d in ipairs(self.sorted) do
116       if self.pred[d] then
117          if #self.pred[d] == 1 then
118             d:updateOutput(self.pred[d][1].output)
119          elseif #self.pred[d] > 1 then
120             local c = {}
121             for k = 1, #self.pred[d] do
122                c[k] = self.pred[d][k].output
123             end
124             d:updateOutput(c)
125          end
126       end
127    end
128
129    self.output = self:applyOnModules(function(m) return m.output end, self.outputModules)
130
131    return self.output
132 end
133
134 function DAG:updateGradInput(input, gradOutput)
135    self:sort()
136
137    self:applyOnModules(function(m, i, go) m:updateGradInput(i, go) end, self.outputModules, input, gradOutput)
138
139    for k = self.sorted, 1, -1 do
140       local m = sorted[k]
141       if self.succ[d] then
142          if #self.succ[d] == 1 then
143             d:updateGradInput(self.succ[d][1].gradInput)
144          elseif #self.succ[d] > 1 then
145             local sum
146             for k = 1, #self.succ[d] do
147                if sum then
148                   sum:add(self.succ[d][k].gradInput)
149                else
150                   sum = self.succ[d][k].gradInput:clone()
151                end
152             end
153             d:updateGradInput(sum)
154          end
155       end
156    end
157
158    self.gradInput = self:applyOnModules(function(m) return m.gradInput end, self.inputModules)
159
160    return self.gradInput
161 end
162
163 return DAG