📌  相关文章
📜  如何使用 Graphviz 在Python中可视化神经网络?

📅  最后修改于: 2022-05-13 01:54:22.520000             🧑  作者: Mango

如何使用 Graphviz 在Python中可视化神经网络?

在本文中,我们将了解如何使用 Graphviz 在Python中绘制(可视化)神经网络。 Graphviz 是一个开源图形可视化软件的Python模块。在研究人员中广泛流行进行可视化。它将结构信息表示为抽象图和网络图,这意味着您只需要提供有关其拓扑结构的图的唯一文本描述,这将自动读取和创建图像。

安装:

对于窗口终端:

pip install graphviz

对于蟒蛇终端:

conda install -c anaconda graphviz

使用 Graphviz 绘制简单图形

方法:

  • 导入模块。
  • 创建一个新的 Diagraph 对象。
  • node()edge()添加到图形对象中。
  • 使用 render() 对象保存源代码。

下面是实现:

Python3
# import module
from graphviz import Digraph
  
# instantiating object
dot = Digraph(comment='A Round Graph')
  
# Adding nodes
dot.node('A', 'Alex')
dot.node('B', 'Rishu')
dot.node('C', 'Mohe')
dot.node('D', 'Satyam')
  
# Adding edges
dot.edges(['AB', 'AC', 'AD'])
dot.edge('B', 'C', constraint = 'false')
dot.edge('C', 'D', constraint = 'false')
  
# saving source code
dot.format = 'png'
dot.render('Graph', view = True)


Python3
print(dot.source)


输出:

Graph.png

我们可以使用 dot.source 方法检查生成的源代码:

蟒蛇3

print(dot.source)

输出:

// A Round Graph
digraph {
    A [label=Alex]
    B [label=Rishu]
    C [label=Mohe]
    D [label=Satyam]
    A -> B
    A -> C
    A -> D
    B -> C [constraint=false]
    C -> D [constraint=false]
}

使用 Graphviz 绘制(可视化)神经网络

这里我们使用源代码来实现,我们在上面的例子中看到:

让我们讨论一下方法:

  • 创建一个有向图对象。
  • 使用rankdir 定义图形的方向。
  • 使用以下内容创建子图:
    • 设置颜色。
    • 设置节点属性。
    • 设置子图的级别
  • 使用 ( -> ) 创建对象之间的边缘。

此源代码必须保存在 .txt 文件 (myfile.txt) 中,然后从命令行运行 `dot -Tpng -O myfile.txt`以获取带有图表的 .png 图。

示例 1:

digraph G {

        rankdir=LR
    splines=line
        
        node [fixedsize=true, label=""];

        subgraph cluster_0 {
        color=white;
        node [style=solid,color=blue4, shape=circle];
        x1 x2 x3 x4;
        label = "layer 1 (Input layer)";
    }

    subgraph cluster_1 {
        color=white;
        node [style=solid,color=red2, shape=circle];
        a12 a22 a32;
        label = "layer 2 (hidden layer)";
    }

    subgraph cluster_2 {
        color=white;
        node [style=solid,color=seagreen2, shape=circle];
        O;
        label="layer 3 (output layer)";
    }

        x1 -> a12;
        x1 -> a22;
        x1 -> a32;
        x2 -> a12;
        x2 -> a22;
        x2 -> a32;
        x3 -> a12;
        x3 -> a22;
        x3 -> a32;
    x4 -> a12;
        x4 -> a22;
        x4 -> a32;
    

        a12 -> O
        a22 -> O
        a32 -> O

}

在终端中运行它:

dot -Tpng -O myfile.txt

输出:

示例 2:

digraph G {

        rankdir=LR
    splines=line
        nodesep=.05;
        
        node [label=""];
        
        subgraph cluster_0 {
        color=white;
                node [style=solid,color=blue4, shape=circle];
        x1 x2 x3;
        label = "layer 1";
    }

    subgraph cluster_1 {
        color=white;
        node [style=solid,color=red2, shape=circle];
        a12 a22 a32 a42 a52;
        label = "layer 2";
    }

    subgraph cluster_2 {
        color=white;
        node [style=solid,color=red2, shape=circle];
        a13 a23 a33 a43 a53;
        label = "layer 3";
    }

    subgraph cluster_3 {
        color=white;
        node [style=solid,color=seagreen2, shape=circle];
        O1 O2 O3 O4;
        label="layer 4";
    }

        x1 -> a12;
        x1 -> a22;
        x1 -> a32;
        x1 -> a42;
        x1 -> a52;

        x2 -> a12;
        x2 -> a22;
        x2 -> a32;
        x2 -> a42;
        x2 -> a52;
 
        x3 -> a12;
        x3 -> a22;
        x3 -> a32;
        x3 -> a42;
        x3 -> a52;

        a12 -> a13
        a22 -> a13
        a32 -> a13
        a42 -> a13
        a52 -> a13

        a12 -> a23
        a22 -> a23
        a32 -> a23
        a42 -> a23
        a52 -> a23

        a12 -> a33
        a22 -> a33
        a32 -> a33
        a42 -> a33
        a52 -> a33

        a12 -> a43
        a22 -> a43
        a32 -> a43
        a42 -> a43
        a52 -> a43

        a12 -> a53
        a22 -> a53
        a32 -> a53
        a42 -> a53
        a52 -> a53

        a13 -> O1
    a23 -> O2
    a33 -> O3
    a43 -> O4
        a53 -> O4

}

输出: