Beispiel #1
0
 def test_mlp(self):
     m = models.Mlp()
     f = th.function(m.inputs, m.outputs)
     pdf = PyDotFormatter()
     graph = pdf(f)
     self.assertEqual(len(graph.get_nodes()), 11)
     nc = self.node_counts(graph)
     assert nc['apply'] == 5
     assert nc['output'] == 1
Beispiel #2
0
 def test_ofg(self):
     m = models.Ofg()
     f = th.function(m.inputs, m.outputs)
     pdf = PyDotFormatter()
     graph = pdf(f)
     assert len(graph.get_nodes()) == 10
     sub_graphs = graph.get_subgraph_list()
     assert len(sub_graphs) == 2
     ofg1, ofg2 = sub_graphs
     assert len(ofg1.get_nodes()) == 5
     assert len(ofg1.get_nodes()) == len(ofg2.get_nodes())
Beispiel #3
0
 def test_ofg_nested(self):
     m = models.OfgNested()
     f = th.function(m.inputs, m.outputs)
     pdf = PyDotFormatter()
     graph = pdf(f)
     assert len(graph.get_nodes()) == 7
     assert len(graph.get_subgraph_list()) == 1
     ofg1 = graph.get_subgraph_list()[0]
     assert len(ofg1.get_nodes()) == 6
     assert len(ofg1.get_subgraph_list()) == 1
     ofg2 = ofg1.get_subgraph_list()[0]
     assert len(ofg2.get_nodes()) == 4
Beispiel #4
0
    def test_mlp(self):
        m = models.Mlp()
        f = th.function(m.inputs, m.outputs)
        pdf = PyDotFormatter()
        graph = pdf(f)
        expected = 11
        if th.config.mode == "FAST_COMPILE":
            expected = 12
        assert len(graph.get_nodes()) == expected
        nc = self.node_counts(graph)

        if th.config.mode == "FAST_COMPILE":
            assert nc["apply"] == 6
        else:
            assert nc["apply"] == 5
        assert nc["output"] == 1
Beispiel #5
0
def d3write(fct, path, *args, **kwargs):
    """Convert Theano graph to pydot graph and write to dot file.

    Parameters
    ----------
    fct : theano.compile.function_module.Function
        A compiled Theano function, variable, apply or a list of variables.
    path: str
        Path to output file
    *args : tuple, optional
        Arguments passed to PyDotFormatter.
    *kwargs : dict, optional
        Arguments passed to PyDotFormatter.
    """

    formatter = PyDotFormatter(*args, **kwargs)
    graph = formatter(fct)
    graph.write_dot(path)
Beispiel #6
0
def d3write(fct, path, *args, **kwargs):
    """Convert Theano graph to pydot graph and write to dot file.

    Parameters
    ----------
    fct : theano.compile.function_module.Function
        A compiled Theano function, variable, apply or a list of variables.
    path: str
        Path to output file

    Notes
    -----
    This function accepts extra parameters which will be forwarded to
    :class:`theano.d3viz.formatting.PyDotFormatter`.

    """

    formatter = PyDotFormatter(*args, **kwargs)
    graph = formatter(fct)
    graph.write_dot(path)
Beispiel #7
0
def d3viz(fct, outfile, copy_deps=True, *args, **kwargs):
    """Create HTML file with dynamic visualizing of a Theano function graph.

    In the HTML file, the whole graph or single nodes can be moved by drag and
    drop. Zooming is possible via the mouse wheel. Detailed information about
    nodes and edges are displayed via mouse-over events. Node labels can be
    edited by selecting Edit from the context menu.

    Input nodes are colored in green, output nodes in blue. Apply nodes are
    ellipses, and colored depending on the type of operation they perform. Red
    ellipses are transfers from/to the GPU (ops with names GpuFromHost,
    HostFromGpu).

    Edges are black by default. If a node returns a view of an
    input, the input edge will be blue. If it returns a destroyed input, the
    edge will be red.

    Parameters
    ----------
    fct : theano.compile.function_module.Function
        A compiled Theano function, variable, apply or a list of variables.
    outfile : str
        Path to output HTML file.
    copy_deps : bool, optional
        Copy javascript and CSS dependencies to output directory.

    Notes
    -----
    This function accepts extra parameters which will be forwarded to
    :class:`theano.d3viz.formatting.PyDotFormatter`.

    """

    # Create DOT graph
    formatter = PyDotFormatter(*args, **kwargs)
    graph = formatter(fct)
    dot_graph = escape_quotes(graph.create_dot()).replace('\n', '').replace(
        '\r', '')

    # Create output directory if not existing
    outdir = os.path.dirname(outfile)
    if not outdir == '' and not os.path.exists(outdir):
        os.makedirs(outdir)

    # Read template HTML file
    template_file = os.path.join(__path__, 'html', 'template.html')
    with open(template_file) as f:
        template = f.read()

    # Copy dependencies to output directory
    src_deps = __path__
    if copy_deps:
        dst_deps = 'd3viz'
        for d in ['js', 'css']:
            dep = os.path.join(outdir, dst_deps, d)
            if not os.path.exists(dep):
                shutil.copytree(os.path.join(src_deps, d), dep)
    else:
        dst_deps = src_deps

    # Replace patterns in template
    replace = {
        '%% JS_DIR %%': os.path.join(dst_deps, 'js'),
        '%% CSS_DIR %%': os.path.join(dst_deps, 'css'),
        '%% DOT_GRAPH %%': dot_graph,
    }
    html = replace_patterns(template, replace)

    # Write HTML file
    with open(outfile, 'w') as f:
        f.write(html)