代码:
x=torch.ones(1)
w=torch.full([1],2)
w.requires_grad_()
# ##RuntimeError: Only Tensors of floating point and complex dtype can require gradients
问题:
遇到“RuntimeError: Only Tensors of floating point and complex dtype can require gradients”
解决:
1.之后添加'.float()'
2.输入数据做成浮点型
如下所示:
x=torch.ones(1)
w=torch.full([1],2).float()
w.requires_grad_()
# w=torch.full([1],2.,requires_grad=True)
# ##RuntimeError: Only Tensors of floating point and complex dtype can require gradients