在pytorch代码进行loss.backward的时候出现下面报错:
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
Exception ignored in: <function tqdm.__del__ at 0x000001EF82E38EA0>
原因是在pytorch对Tensor进行backward自动求导时,代码中的
loss.requires_grad 设置为了 False
因此我们只需要在代码中加入:
loss.requires_grad = True