Chainer 网络可视化

本文介绍如何使用Chainer框架进行神经网络的可视化,通过调用build_computational_graph()生成计算图,并利用pydotplus将dot格式转换为更直观的PDF文件。

今天需要对chainer框架的网络进行可视化,在官网文档找到如下的办法

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())

 但是生成的dot文件,还不是很直观进行查看,于是就利用pydotplus(安装pip install pydotplus)解析然后生成pdf文件就好查看多了,全部的代码如下:

import chainer.computational_graph as c
import numpy as np
from chainer.variable import Variable
import pydotplus

x_data = np.zeros([1, 3, 300, 300], dtype=np.float32)
x = Variable(x_data)

model = Defined_Model()

vs = model(x)

g = c.build_computational_graph(vs)

dot_format = g._to_dot()

graph=pydotplus.graph_from_dot_data(dot_format)

#生成 pdf文件
graph.write_pdf('visualization.pdf')

#生成visualization.gv dot 文件
with open('visualization.gv', 'w') as o:
    o.write(g.dump()) 

就这样,有问题留言

转载于:https://my.oschina.net/zhangwenwen/blog/3005863

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值