pytorch 模型可视化的例子
作者:我~ 时间:2023-06-13 08:24:34
如下所示:
一. visualize.py
from graphviz import Digraph
import torch
from torch.autograd import Variable
def make_dot(var, params=None):
""" Produces Graphviz representation of PyTorch autograd graph
Blue nodes are the Variables that require grad, orange are Tensors
saved for backward in torch.autograd.Function
Args:
var: output Variable
params: dict of (name, Variable) to add names to node that
require grad (TODO: make optional)
"""
if params is not None:
assert isinstance(params.values()[0], Variable)
param_map = {id(v): k for k, v in params.items()}
node_attr = dict(style='filled',
shape='box',
align='left',
fontsize='12',
ranksep='0.1',
height='0.2')
dot = Digraph(node_attr=node_attr, graph_attr=dict(size="12,12"))
seen = set()
def size_to_str(size):
return '('+(', ').join(['%d' % v for v in size])+')'
def add_nodes(var):
if var not in seen:
if torch.is_tensor(var):
dot.node(str(id(var)), size_to_str(var.size()), fillcolor='orange')
elif hasattr(var, 'variable'):
u = var.variable
name = param_map[id(u)] if params is not None else ''
node_name = '%s\n %s' % (name, size_to_str(u.size()))
dot.node(str(id(var)), node_name, fillcolor='lightblue')
else:
dot.node(str(id(var)), str(type(var).__name__))
seen.add(var)
if hasattr(var, 'next_functions'):
for u in var.next_functions:
if u[0] is not None:
dot.edge(str(id(u[0])), str(id(var)))
add_nodes(u[0])
if hasattr(var, 'saved_tensors'):
for t in var.saved_tensors:
dot.edge(str(id(t)), str(id(var)))
add_nodes(t)
add_nodes(var.grad_fn)
return dot
二. 使用步骤
import torch
from torch.autograd import Variable
from models import *
from visualize import make_dot
x = Variable(torch.rand(1, 3, 256, 256))
model = GeneratorUNet()
y = model(x)
g = make_dot(y)
g.view()
三. 效果展示
来源:https://blog.csdn.net/weixin_42445501/article/details/81221362
标签:pytorch,模型,可视化
0
投稿
猜你喜欢
MySQL中进行跨库查询的方法示例
2024-01-26 10:27:39
python opencv将图片转为灰度图的方法示例
2021-03-24 22:22:54
python中如何设置list步长
2022-11-04 14:24:02
Python统计文件中去重后uuid个数的方法
2023-07-11 17:20:06
MySQL Redo与Undo日志详细解析
2024-01-26 04:40:09
python实点云分割k-means(sklearn)详解
2023-11-06 20:36:03
Python显示进度条的方法
2023-05-25 07:38:30
.NET多种数据库大数据批量插入、更新(支持SqlServer、MySql、PgSql和Oracle)
2024-01-19 07:39:40
交互设计杂七杂八
2010-09-25 18:41:00
Qt Quick QML-500行代码实现合成大西瓜游戏
2023-09-22 22:13:07
Win8.1下安装Python3.6提示0x80240017错误的解决方法
2021-12-24 08:36:51
Python实现PDF转换文本详解
2022-09-04 13:40:52
MySQL 8.0 驱动与阿里druid版本兼容问题解决
2024-01-17 06:25:49
Python实现身份证前六位地区码对照表文件
2022-12-25 10:55:16
Python decorator拦截器代码实例解析
2022-04-07 11:03:47
Google首页的CSS Sprite
2007-09-29 21:36:00
基于js实现抽红包并分配代码实例
2024-04-16 09:30:58
vue实现百度搜索功能
2024-05-05 09:11:34
python实现电子词典
2023-05-14 00:29:01
利用Python多线程实现图片下载器
2022-02-12 06:07:41