如何将 torch 张量转换为 numpy?
请您参考如下方法:
复制自pytorch doc :
a = torch.ones(5)
print(a)
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
[1. 1. 1. 1. 1.]
根据与@John 的以下讨论:
如果张量在(或可以)在 GPU 上,或者它(或它可以)需要梯度,则可以使用
t.detach().cpu().numpy()
我建议只根据需要对代码进行丑化。
