RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

将搭建好的模型损失从交叉熵改成focal_loss后报错:

1
2
3
4
5
6
7
File "F:/pytorch_multi_label_classifier/occ_face_focal.py", line 146, in train
    loss.backward()
  File "D:\anaconda\lib\site-packages\torch\tensor.py", line 118, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "D:\anaconda\lib\site-packages\torch\autograd\__init__.py", line 93, in backward
    allow_unreachable=True)  # allow_unreachable flag
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

这个错误源于Pytorch对Tensor进行 backward() 自动求导时,该Tensor的requires_grad为False,requires_grad参数指定是否记录对Tensor的操作以便计算梯度。在创建Tensor时该参数默认为False,需要手动设置如下
loss = torch.zeros(1, requires_grad=True)
也可以通过loss.requires_grad_(True)设置。

1
2
loss = Variable(torch.FloatTensor(1)).zero_()
loss.requires_grad_(True)

参考博客https://blog.csdn.net/huyaoyu/article/details/81059315
描述的更加清晰