本文最后更新于:2023年12月5日 下午

本文记录 pytorch: can’t optimize a non-leaf Tensor 解决方案。

错误

运行优化时报错

1
2
3
4
5
6
7
from torch import optim

weights = torch.rand(2,1,128,416)
weights.requires_grad = True
weights = weights.cuda()
optimizer = optim.Adam([weights], lr=0.01)

  • 报错: can’t optimize a non-leaf Tensor

解决方案

需要先把Tensor放入到GPU中,然后再设置Tensor.requires_grad=True。

1
2
3
4
5
6
7
from torch import optim

weights = torch.rand(2,1,128,416)
weights = weights.cuda()
weights.requires_grad = True
optimizer = optim.Adam([weights], lr=0.01)

参考资料



文章链接:
https://www.zywvvd.com/notes/study/deep-learning/pytorch/none-leaf-tensor/none-leaf-tensor/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Pytorch can't optimize a non-leaf Tensor
https://www.zywvvd.com/notes/study/deep-learning/pytorch/none-leaf-tensor/none-leaf-tensor/
作者
Yiwei Zhang
发布于
2021年3月2日
许可协议