参考网址:https://blog.csdn.net/qq_38159643/article/details/122043108

1.问题:可视化网络没有显示尺寸的大小

import torch
import hiddenlayer as h
from torchvision.models import alexnet  # 以 alexnet 为例

myNet = alexnet()  # 实例化 alexnet
x = torch.randn(1, 3, 224, 224)  # 随机生成一个输入
myNetGraph = h.build_graph(myNet, x)  # 建立网络模型图
# myNetGraph.theme = h.graph.THEMES['blue']  # blue 和 basic 两种颜色,可以不要
myNetGraph.save(path='./demoModel.png', format='png')  # 保存网络模型图,可以设置 png 和 pdf 等

 2.解决方案:修改底层源码

找到 pytorch_builder.py 文件,修改 get_shape(torch_node) 函数如下:

def get_shape(torch_node):
    """Return the output shape of the given Pytorch node."""
    # Extract node output shape from the node string representation
    # This is a hack because there doesn't seem to be an official way to do it.
    # See my quesiton in the PyTorch forum:
    # https://discuss.pytorch.org/t/node-output-shape-from-trace-graph/24351/2
    # TODO: find a better way to extract output shape
    # TODO: Assuming the node has one output. Update if we encounter a multi-output node.
    shape = torch_node.output().type().sizes()
    return shape

 3.处理后的结果:

4.安装hiddenlayer库

方法一、通过命令行cmd,然后输入pip install hiddenlayer(我已经安装过,所以下面显示已经安装)

 方法二、进入官网,进行下载源码,运行py文件实现

Logo

永洪科技,致力于打造全球领先的数据技术厂商,具备从数据应用方案咨询、BI、AIGC智能分析、数字孪生、数据资产、数据治理、数据实施的端到端大数据价值服务能力。

更多推荐