文章目录

  • 前言
  • dot画神经网络图
    • 简单神经网络
    • 大型神经网络(伪)
  • Python 画神经网络图
    • 大型神经网络

前言

做了神经网络这么久,偶尔想画一下自己模型的架构图,但是又无从下手,因为网络一般都比较复杂,如果自己手动利用Visio画的画又比较麻烦,而我又比较懒,网上搜了一下Graphviz
画图工具,学了会便开始打算利用它来画模型结构图。
关于Graphviz的介绍,可以参考我的前两篇博客:

  • Graphviz 画图教程
  • Graphviz 画图教程(Python)

dot画神经网络图

简单神经网络

digraph G {rankdir=LR;splines=line;nodesep=.1;node [label=""];subgraph cluster_0 {color=white;node [style=solid,color=green,shape=circle];i;label = "Input Layer";}subgraph cluster_1 {color=white;node [style=solid,color=blue, shape=circle];h11, h12, h13, h14;label = "Hidden Layer 1";}subgraph cluster_2 {color=white;node [style=solid,color=blue, shape=circle];h21, h22;label = "Hidden Layer 2";}subgraph cluster_3 {color=white;node [style=solid,color=red, shape=circle];o;label="Output Layer";}i -> h11i -> h12i -> h13i -> h14h11 -> h21h11 -> h22h12 -> h21h12 -> h22h13 -> h21h13 -> h22h14 -> h21h14 -> h22h21 -> oh22 -> o}

这里我们定义了一个图G,及四个子图cluster_0cluster_1cluster_2cluster_ 3,并为每个子图定义了结点类node及其各自属性,由于神经网络是全连接,因此我们需要为每一个结点连上边(十分繁琐)。
将其保存为demo.dot文件,通过命令:
dot -Tpng demo.dot -o demo.png
将其转化为png图片,如下所示:

好了,这样就画好了一个简单的神经网络模型图了,但是当你遇上结点较多的情况怎么办?
你可能会说,我又不傻,难道一个个在dot脚本上写上去?我可以用省略号代替啊!好的,那么来试试省略号代替的“伪大型神经网络”样子。

大型神经网络(伪)

digraph G {rankdir=LR;splines=line;nodesep=.1;node [label=""];compound=truesubgraph cluster_0 {color=white;node [style=solid,color=green,shape=circle];i;label = "Input Layer";}subgraph cluster_1 {color=white;node [style=solid,color=blue, shape=circle];h11, h12, h13, h14;label = "Hidden Layer 1 (4 nodes)";}subgraph cluster_2 {color=white;{node [style=solid,color=blue,shape=circle];h21;h22;}node [style=solid,shape=point];p1; p2; p3;{node [style=solid,color=blue,shape=circle]; h23;h24;    }label = "Hidden Layer 2 (100 nodes)";}subgraph cluster_3 {color=white;node [style=solid,color=red, shape=circle];o;label="Output Layer";}i -> h11i -> h12i -> h13i -> h14h11 -> h21h11 -> h22h12 -> h21h12 -> h22h13 -> h21h13 -> h22h14 -> h21h14 -> h22h11 -> h23h11 -> h24h12 -> h23h12 -> h24h13 -> h23h13 -> h24h14 -> h23h14 -> h24h21 -> oh22 -> oh23 -> oh24 -> o}


这里我通过增加了几个形状为point的结点,使得整个网络看起来像是一个大型的网络的缩略版。注意,画图的时候顺序是按照结点定义的顺序来画的,所以三个小黑点要定义在中间。


Python 画神经网络图

大型神经网络

虽然按照上面的做法能够“投机取巧”地画出一个伪大型神经网络结构图,但是当你要真正画一个大型神经网络并且观察它的连接细节呢?这种时候就要利用Python的力量了。
代码如下,相关解释已经写在代码注释中:

from graphviz import Digraphdef Neural_Network_Graph(input_layer, hidden_layers, output_layer, filename="demo"):g = Digraph('g', filename=filename)  # 定义一个有向图n = 0  # 所有结点的数量,用其来作为结点的名字(代号)g.graph_attr.update(splines="false", nodesep='0.8',ranksep='2', rankdir="LR")  # 设置下图的属性: 线类型,结点间隔,每一级的间隔# Input Layerwith g.subgraph(name='cluster_input') as c:the_label = 'Input Layer'c.attr(color='white')for i in range(input_layer):n += 1c.node(str(n))c.attr(label=the_label, rank='same')c.node_attr.update(color="#2ecc71", style="filled",fontcolor="#2ecc71", shape="circle")last_layer_nodes = input_layer  # 最后一层的结点数量nodes_up = input_layer  # 总结点数量# Hidden Layershidden_layers_nr = len(hidden_layers)  # 隐藏层层数for i in range(hidden_layers_nr):with g.subgraph(name="cluster_" + str(i + 1)) as c:c.attr(color='white')c.attr(rank='same')the_label = "Hidden Layer" + str(i+1)c.attr(label=the_label)for j in range(hidden_layers[i]):n += 1c.node(str(n), shape="circle", style="filled",color="#3498db", fontcolor="#3498db")for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1):g.edge(str(h), str(n))  # 定义好上一层到下一层的连接线last_layer_nodes = hidden_layers[i]nodes_up += hidden_layers[i]# Output Layerwith g.subgraph(name='cluster_output') as c:c.attr(color='white')c.attr(rank='same')for i in range(1, output_layer + 1):n += 1c.node(str(n), shape="circle", style="filled",color="#e74c3c", fontcolor="#e74c3c")for h in range(nodes_up - last_layer_nodes + 1, nodes_up + 1):g.edge(str(h), str(n))c.attr(label='Output Layer')c.node_attr.update(color="#2ecc71", style="filled",fontcolor="#2ecc71", shape="circle")g.attr(arrowShape="none")g.edge_attr.update(arrowhead="none", color="#707070")g.render(filename, format="png")

我编写了一个Neural_Network_Graph函数,接收输入为:

  • input_layer:输入层神经元数量
  • hidden_layers:列表类型,隐藏层层数及神经元数量
  • output_layer:输出层神经元数量
    通过定义三个参数,调用Neural_Network_Graph函数:
# -------------------------------------------
input_layer = 5  # 输入层的神经元数量
hidden_layers = [10, 6]  # 隐藏层层数和数量
output_layer = 1  # 输出层神经元数量
# -----------------------------------------------Neural_Network_Graph(input_layer, hidden_layers, output_layer)

很容易得到一张png格式的神经网络图:

利用Graphviz画神经网络框架图相关推荐

  1. boxplot用法 python,[Python画图笔记]利用Python画箱型图boxplot

    [Python画图笔记]利用Python画箱型图boxplot [Python画图笔记]利用Python画箱型图boxplot 最近在学习使用Python画图,想用subplot画两幅箱型图,分别用来 ...

  2. 如何从椭圆度 matlab,如何利用matlab画出如图潮流椭圆

    clc;clear   %先对分量进行调和分析,然后利用elli_para2程序计算椭圆要素,ap2ep程序画潮流椭圆 %% xlsend = [3477 3897 3477 3478]; lat_p ...

  3. python画折线图详解-利用python画出折线图

    本文实例为大家分享了python画折线图的具体代码,供大家参考,具体内容如下 # encoding=utf-8 import matplotlib.pyplot as plt from pylab i ...

  4. python画折线图-利用python画出折线图

    本文实例为大家分享了python画折线图的具体代码,供大家参考,具体内容如下 # encoding=utf-8 import matplotlib.pyplot as plt from pylab i ...

  5. 利用visio 画思维导图

    一直有个想法,用图片的形式来展示所学的知识,用了各种脑图,都是以一幅幅图片的形式保存下来的.今天发现visio 也可以画脑图,用visio 有个好处,兼容word,使用起来非常的方便. Visio绘制 ...

  6. 利用python画外汇蜡烛图

    导入本地数据集,进行数据处理,再进行画图 import numpy as np import matplotlib.pyplot as plt import pandas as pd import d ...

  7. 利用Graphviz 画结构图

    Windows下用法 官网:http://www.graphviz.org/ 详实用法参考:http://www.cnblogs.com/sld666666/archive/2010/06/25/17 ...

  8. 使用Keras画神经网络准确性图

    1.在搭建网络开始时,会调用到 keras.models的Sequential()方法,返回一个model参数表示模型 2.model参数里面有个fit()方法,用于把训练集传进网络.fit()返回一 ...

  9. python如何画神经网络特征图

    1.构造绘制特征图的函数 width, height为特征图的宽和高,x为数据,savename为保存的图片路径: def draw_features(width, height, x, savena ...

最新文章

  1. 第四百六十八天 how can I 坚持
  2. 电脑知识:关于电脑的十大误区,原来是这样!
  3. java sql server 2016_SQL server 2016 安装步骤
  4. 【Python】pdf2image模块+poppler将PDF转换为图片
  5. 高速建成Android开发环境ADT-Bundle和Hello World
  6. 关于CSS中定位的个人理解
  7. OpenGL学习(一)OpenGL基本介绍
  8. Github的README中插入图片,Github仓库项目主页显示图片
  9. UVa101 - The Blocks Problem
  10. 【读书笔记】iOS-简单的数据驱动程序
  11. 蓝桥杯2019年第十届C/C++省赛C组第二题-矩形切割
  12. iOS xcode The certificate used to sign Administrator has either expired or has been revoked. An u
  13. 音频3A测试 AGC自动增益测试
  14. 新手十分钟玩转淘宝客推广攻略
  15. 等保2.0详解(附3级检查表)
  16. 证券投资基金和股票、债券的区别和联系
  17. 给计算机专业新生的一些学习建议
  18. 【SEO优化】SEO应该是我们现在理解的这样吗?
  19. 读JQuery 有感
  20. 微信小程序云开发 操作数据库-数据的批量更新

热门文章

  1. 树莓派实现远程控制空调
  2. 联想-IBM PC并购案
  3. 字典树01字典树算法笔记
  4. 今天终于下决心删除了McAfee,受不了了!
  5. 我那个在华为过得很好的朋友
  6. bzoj4011[HNOI2015]落忆枫音
  7. Ganymed远程连接SSH
  8. java 对音频文件降噪_如何有效的对录音文件进行降噪处理?
  9. 电脑基本故障与解决方案
  10. 如何禁用和恢复任务管理器