You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyTorch HiddenLayer绘制模型图失败:torch.onnx无_optimize_trace属性

HiddenLayer配合PyTorch 1.13可视化模型报错解决

问题代码

运行以下代码时出现报错:

import torch
import torchvision.models
import hiddenlayer as hl
# VGG16 with BatchNorm
model = torchvision.models.vgg16()
# Build HiddenLayer graph
# Jupyter Notebook renders it automatically
hl.build_graph(model, torch.zeros([1, 3, 224, 224]))

环境版本

  • hiddenlayer 0.3
  • pytorch 1.13.0+cu117
  • python 3.10.6

问题原因与解决办法

HiddenLayer 0.3是旧版本,对PyTorch 1.13及以上版本的API兼容性不足——PyTorch 1.13调整了部分模块的追踪逻辑、张量操作接口,导致HiddenLayer的模型解析代码无法正常工作。

可选解决路径:

  1. 降级PyTorch:将PyTorch版本降到1.12.x,此版本与HiddenLayer 0.3兼容性良好。
  2. 安装HiddenLayer开发版:官方0.3之后无正式新版本,但GitHub上有适配高版本PyTorch的更新代码,执行以下命令安装:
    pip install git+https://github.com/waleedka/hiddenlayer.git
    
  3. 更换可视化工具:改用PyTorch官方的torch.utils.tensorboard或torchviz,这两个工具对高版本PyTorch支持更完善,能实现相同的模型可视化需求。

内容的提问来源于stack exchange,提问作者illan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 06:10:36