4 This package implements a new module nn.DAG for the torch framework,
5 which inherits from nn.Container and allows to combine modules in an
6 arbitrary graph without cycle.
10 A typical use would be:
15 a = nn.Linear(100, 10)
22 model:connect(b, nn.Linear(10, 15), nn.ReLU(), d)
25 model:connect(c, nn.Mul(-1), e)
28 model:setOutput({ d, e })
30 input = torch.Tensor(30, 100):uniform()
31 output = model:updateOutput(input)
35 which would encode the following graph
37 +- Linear(10, 10) -> ReLU ---> d -->
40 --> a --> b -----------> c --------------+
45 and run a forward pass with a random batch of 30 samples.
47 Note that DAG:connect allows to add a bunch of edges at once. This is
48 particularly useful to add anonymous modules which have a single
49 predecessor and successor.
53 ## Input and output ##
55 The DAG can deal with modules which take as input and produce as
56 output tensors and nested tables of tensors.
58 If a node has a single predecessor, the output of the latter is taken
59 as-is as the input of the former. If it has multiple predecessors, all
60 the outputs are collected into a table, and the table is used as
61 input. The indexes of the outputs in that table reflects the order in
62 which the edges where created in the DAG:connect() commands.
64 The input to the DAG (respectively the produced output) is a nested
65 table of inputs reflecting the structure of the nested table of
66 modules provided to DAG:setInput (respectively DAG:setOutput)
68 So for instance, in the example above, the model expects a tensor as
69 input, since it is the input to the module a, and its output will is a
70 table composed of two tensors, corresponding to the outputs of d and e
77 Create a new empty DAG, which inherits from nn.Container.
79 ### nn.DAG:connect([module1 [, module2 [, ...]]]) ###
81 Add new nodes corresponding to the modules passed as arguments if they
82 are not already existing. Add edges between every two nodes
83 corresponding to a pair of successive modules in the arguments.
85 Calling it with n > 2 arguments is strictly equivalent to calling it
86 n-1 times on the pairs of successive arguments.
88 ### nn.DAG:setInput(i) ###
90 Defines the content and structure of the input. The argument should be
91 either a module, or a (nested) table of modules. The input to the DAG
92 should be a (nested) table of inputs, with the corresponding structure.
94 ### nn.DAG:setOutput(o) ###
96 Similar to DAG:setInput().
98 ### nn.DAG:print() ###
100 Prints the list of nodes.
102 ### nn.DAG:saveDot(filename) ###
104 Save a dot file to be used by the Graphviz set of tools for graph
105 visualization. This dot file can than be used for instance to produce
109 dot graph.dot -T pdf -o graph.pdf
113 *Francois Fleuret, Jan 13th, 2017*