Pytorch中ndarray tensor list互转


1.ndarray->tensor :

b=torch.from_numpy(a)

2.tensor->ndarray:

b=a.numpy()

'''
但这么写会报错……
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.
'''
# 修改为
b=a.detach().numpy()

'''
gpu上的tensor不能直接转为numpy
'''
b=a.cpu().numpy()

3.ndarray->list

b=a.tolist()

4.list->ndarray

b=numpy.array(a)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM