pytorch中tensor转numpy
cpu tensor转numpy:#假定a为tensora.numpy()gpu tensor转numpy:gpu下的tensor不能直接转numpy,需要先转到cpu tensor后再转为numpya.cpu().numpy()注:若tensor带有梯度,以上述方式转换时会报错:RuntimeError: Can't call numpy() on Tensor that requires gr
·
cpu tensor转numpy:
#假定a为tensor
a.numpy()
gpu tensor转numpy:
gpu下的tensor不能直接转numpy,需要先转到cpu tensor后再转为numpy
a.cpu().numpy()
注:若tensor带有梯度,以上述方式转换时会报错:
RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
可以改用detach().numpy()方式
a.detach().numpy()
a.cpu().detach().numpy()
更多推荐
已为社区贡献1条内容
所有评论(0)