RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn (from pytorch)

Pytorch中, tensor需要定义成require_grad的类型才能求梯度。

可以在定义变量的时候加上“requires_grad=True”

或者,某tensor, tensor_X = tensor_X.requires_grad_()

你可能感兴趣的:(pytorch)