Visualization of Computational Graph

As neural networks get larger and complicated, it gets much harder to confirm if their architectures are constructed properly. Chainer supports visualization of computational graphs. Users can generate computational graphs by invoking build_computational_graph(). Generated computational graphs are dumped to specified format (Currently Dot Language is supported).

Basic usage is as follows:

import chainer.computational_graph as c
...
g = c.build_computational_graph(vs)
with open('path/to/output/file', 'w') as o:
    o.write(g.dump())

where vs is list of Variable instances and g is an instance of ComputationalGraph. This code generates the computational graph that are backward-reachable (i.e. reachable by repetition of steps backward) from at least one of vs.

Here is an example of (a part of) the generated graph (inception(3a) in GoogLeNet). This example is from example/imagenet.

../_images/googlenet.png
chainer.computational_graph.build_computational_graph(outputs, remove_split=True, variable_style=None, function_style=None, rankdir='TB')[source]

Builds a graph of functions and variables backward-reachable from outputs.

Parameters:
  • outputs (list) – nodes from which the graph is constructed. Each element of outputs must be either Variable object or Function object.
  • remove_split (bool) – It must be True. This argument is left for backward compatibility.
  • variable_style (dict) – Dot node style for variable. Possible keys are ‘shape’, ‘color’, ‘fillcolor’, ‘style’, and etc.
  • function_style (dict) – Dot node style for function.
  • rankdir (str) – Direction of the graph that must be TB (top to bottom), BT (bottom to top), LR (left to right) or RL (right to left).
Returns:

A graph consisting of nodes and edges that are backward-reachable from at least one of outputs.

If unchain_backward was called in some variable in the computational graph before this function, backward step is stopped at this variable.

For example, suppose that computational graph is as follows:

    |--> f ---> y
x --+
    |--> g ---> z

Let outputs = [y, z]. Then the full graph is emitted.

Next, let outputs = [y]. Note that z and g are not backward-reachable from y. The resulting graph would be following:

x ---> f ---> y

See TestGraphBuilder for details.

Return type:

ComputationalGraph

class chainer.computational_graph.ComputationalGraph(nodes, edges, variable_style=None, function_style=None, rankdir='TB')[source]

Class that represents computational graph.

Note

We assume that the computational graph is directed and acyclic.

dump(format='dot')[source]

Dumps graph as a text.

Parameters:
  • format (str) – The graph language name of the output.
  • it must be 'dot'. (Currently,) –
Returns:

The graph in specified format.

Return type:

str