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

       在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

猜你喜欢

转载自blog.csdn.net/qq_37925923/article/details/127082395
今日推荐