8 ----------------------------------------------------------------------
10 local Graph, parent = torch.class('nn.Graph', 'nn.Container')
12 function Graph:__init()
18 function Graph:addEdge(a, b)
19 local pred, succ = self.pred, self.succ
20 if not pred[a] and not succ[a] then
23 if not pred[b] and not succ[b] then
26 pred[b] = pred[b] or {}
27 pred[b][#pred[b] + 1] = a
28 succ[a] = succ[a] or {}
29 succ[a][#succ[a] + 1] = b
32 function Graph:setInput(i)
33 if torch.type(i) == 'table' then
35 for _, m in ipairs(i) do
36 if not self.pred[m] and not self.succ[m] then
45 function Graph:setOutput(o)
46 if torch.type(o) == 'table' then
48 for _, m in ipairs(o) do
49 if not self.pred[m] and not self.succ[m] then
58 function Graph:order()
61 for _, a in pairs(self.input) do
69 for i, isucc in pairs(self.succ) do
70 for _, j in pairs(isucc) do
71 if distance[i] and (not distance[j] or distance[j] < distance[i] + 1) then
72 distance[j] = distance[i] + 1
80 for i, d in pairs(distance) do
81 table.insert(self.sorted, { d, i })
84 table.sort(self.sorted, function(a, b) return a[1] < b[1] end)
85 for i, a in ipairs(self.sorted) do self.sorted[i] = a[2] end
88 function Graph:print()
89 for i, d in ipairs(self.sorted) do
90 print('#' .. i .. ' -> ' .. torch.type(d))
94 function Graph:updateOutput(input)
95 return self.output.output
98 ----------------------------------------------------------------------
100 a = nn.Linear(10, 10)
108 a -----> b ---> c ---- e ---