-#!/usr/bin/env luajit
require 'torch'
require 'nn'
-require 'image'
-require 'optim'
-----------------------------------------------------------------------
+local DAG, parent = torch.class('nn.DAG', 'nn.Container')
-local Graph, parent = torch.class('nn.Graph', 'nn.Container')
-
-function Graph:__init()
+function DAG:__init()
parent.__init(self)
self.pred = {}
self.succ = {}
end
-function Graph:addEdge(a, b)
+function DAG:addEdge(a, b)
local pred, succ = self.pred, self.succ
if not pred[a] and not succ[a] then
self:add(a)
succ[a][#succ[a] + 1] = b
end
-function Graph:setInput(i)
+function DAG:setInput(i)
if torch.type(i) == 'table' then
self.inputModules = i
for _, m in ipairs(i) do
end
end
-function Graph:setOutput(o)
+function DAG:setOutput(o)
if torch.type(o) == 'table' then
self.outputModules = o
for _, m in ipairs(o) do
end
end
-function Graph:order()
+function DAG:order()
local distance = {}
for _, a in pairs(self.inputModules) do
for i, a in ipairs(self.sorted) do self.sorted[i] = a[2] end
end
-function Graph:print()
+function DAG:print()
for i, d in ipairs(self.sorted) do
print('#' .. i .. ' -> ' .. torch.type(d))
end
end
-function Graph:updateOutput(input)
+function DAG:updateOutput(input)
if #self.inputModules == 1 then
self.inputModules[1]:updateOutput(input)
else
return self.output
end
-
-----------------------------------------------------------------------
-
-a = nn.Linear(10, 10)
-b = nn.ReLU()
-c = nn.Linear(10, 3)
-d = nn.Linear(10, 3)
-e = nn.CMulTable()
-f = nn.Linear(3, 2)
-
---[[
-
- a -----> b ---> c ----> e ---
- \ /
- \--> d ---/
- \
- \---> f ---
-]]--
-
-g = Graph:new()
-
-g:setInput(a)
-g:setOutput({ e, f })
-g:addEdge(c, e)
-g:addEdge(a, b)
-g:addEdge(d, e)
-g:addEdge(b, c)
-g:addEdge(b, d)
-g:addEdge(d, f)
-
-g:order()
-
-g:print(graph)
-
-input = torch.Tensor(3, 10):uniform()
-
-output = g:updateOutput(input)
-
-print(output[1])
-print(output[2])