[Solved] Pytorch Tensor to numpy error: RuntimeError: Can‘t call numpy() on Tensor that requires grad.报错

Solution:

Use tensor. Detach(). Numpy() when turning numpy:

a = torch.ones(5)
b = a.detach().numpy()
print(b)

Problem analysis

When the tensor conversion in calculation, because it has gradient value, it cannot be directly converted to numpy format, so it is better to call . Detach(). Numpy() no matter how


Read More: