欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

Pytorch转ONNX采坑记:Converting a tensor to a Python boolean might cause the trace to be incorrect. We...

发布时间:2025/4/16 python 62 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Pytorch转ONNX采坑记:Converting a tensor to a Python boolean might cause the trace to be incorrect. We... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

如果你用Pytorch定义的网络结构太过于灵活,那么转成ONNX的时候很有可能出错。这个报错通常情况下是你的网络结构中出现if else 语句。比如

if cfg.CUDA:eps = torch.cuda.FloatTensor(std.size()).normal_()else:eps = torch.FloatTensor(std.size()).normal_()

最好不要这样写,能避免的话尽量避免。不要写if else判断语句。
比如写成这样:

eps = torch.normal(torch.zeros_like(mu))

如果避免不了的话,(⊙o⊙)…,可能转出来的网络就有问题。

参考资料

Pytorch 1.0 Tracer Warning: Converting a tensor to a Python index might cause the trace to be incorrect

总结

以上是生活随笔为你收集整理的Pytorch转ONNX采坑记:Converting a tensor to a Python boolean might cause the trace to be incorrect. We...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。