惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Troy Hunt's Blog
P
Palo Alto Networks Blog
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
Threatpost
C
Cyber Attacks, Cyber Crime and Cyber Security
S
Schneier on Security
Google Online Security Blog
Google Online Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
量子位
L
Lohrmann on Cybersecurity
酷 壳 – CoolShell
酷 壳 – CoolShell
Attack and Defense Labs
Attack and Defense Labs
Y
Y Combinator Blog
Project Zero
Project Zero
AWS News Blog
AWS News Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园 - 聂微东
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Securelist
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
B
Blog RSS Feed
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
V2EX - 技术
V2EX - 技术
Schneier on Security
Schneier on Security
F
Full Disclosure
Apple Machine Learning Research
Apple Machine Learning Research
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
P
Proofpoint News Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
月光博客
月光博客
L
LINUX DO - 最新话题
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Heimdal Security Blog
F
Fortinet All Blogs
博客园_首页
N
News | PayPal Newsroom
P
Proofpoint News Feed

Yunfeng's Simple Blog

2025年终总结 lyrichroma-一键将语音转换为视频的Python命令行工具 JiT论文阅读Back to Basics-Let Denoising Generative Models Denoise Lepton AI后续 llm-code-scorer wavlm-large模型onnx和mnn版本的导出与使用 解决Manus Blog自动跳转无法访问的问题 用MOSS-TTSD生成相声 张小珺明超平访谈观点总结 Qwen VLo 效果实测 美团 NoCode 简单使用体验 AI时代的产品文本化 Comma v0.1 -全开源数据训练的可复现大模型 用gradio部署mcp server repetition_penality的作用与实现 git lfs pointer 报错解决 bitnet-b1.58-2b-4t Neovim conceal机制导致markdown语法隐藏的问题 Quotation Armin Ronacher's Reflecting on Life
Pytorch转ONNX报错-Cannot insert a Tensor that requires grad as a constant
Yunfeng Wang · 2025-07-09 · via Yunfeng's Simple Blog

pytorch模型转换onnx的时候,遇到了下面的报错信息:

1
RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient 

翻译过来就是不能将一个需要梯度的tensor转换为constant。

定位到报错的层,是一个Conv2D,看起来是它对应的weight设置了requires_grad为True。本以为直接修改requires_grad = False 就可以了,但比较诡异的是,实际试下来并不行。

具体来说,尝试了下面的方案,都不work:

  • 给forward 函数增加torch.inference_mode() 装饰符
  • 在报错的层前面加torch.no_grad() context
  • 给输入增加.detach() 函数,去掉梯度
  • 给层对应的weight设置requires_grad 为False

最后还是在网上发现了解决方案,尝试之后是work的。具体来说,就是将对应模型的所有层的参数都设置为requires_grad = False:

1
2
for param in model.parameters():
param.requires_grad = False

大功告成。