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


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

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

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: