Pytorch can't optimize a non-leaf Tensor

2022-08-05 13:41:57 浏览数 (2)

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

错误

运行优化时报错

代码语言:javascript复制
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。

代码语言:javascript复制
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://blog.csdn.net/nkhgl/article/details/100047276

0 人点赞