CPU tensor转numpy:
a.numpy() #a为tensor
GPU tensor转numpy:
a.cpu().numpy()
注:若tensor带有梯度,以上述方式转换时会报错:
RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
分别改用:
a.detach().numpy()
a.cpu().detach().numpy()