在Pytorch中可以使用tensorboardX画出网络结构图,但总觉得不太好看。pytorchviz 似乎是一个不错的选择。
使用方法:
from torchviz import make_dot
from torch.autograd import Variable
net = Model()
x = Variable(torch.randn(3,32,32))
vis_graph = make_dot(net(x), params=dict(net.named_parameters()))
vis_graph.directory = "Net_Structure" # 设置可视化文件的导出路径
vis_graph.format = "png" # 如果不加这一句,生成的则是pdf
vis_graph.view()
如果单纯地pip install torchviz,会报以下错误:
graphviz.backend.ExecutableNotFound: failed to execute ['dot', '-Tpdf', '-O', 'Digraph.gv'], make sure the Graphviz executables are on your systems' PATH
正确的方法:
- 下载package并解压到本地,例如
D:/test. 我下载的是:这个 - Add
D:\test\Graphviz\binto User path - Add
D:\test\Graphviz\bin\dot.exeto System Path - 重启计算机
再运行上面的程序后就会发现网络结构图会出现在一个pdf或png中
zhuanlan.zhihu.com/p/220403674… stackoverflow.com/questions/3…