1.1 list 转 numpy
ndarray = np.array(list)
1.2 numpy 转 list
list = ndarray.tolist()
2.1 list 转 torch.Tensor
tensor=torch.Tensor(list)
2.2 torch.Tensor 转 list
先转numpy,后转list
list = tensor.numpy().tolist()
3.1 torch.Tensor 转 numpy
ndarray = tensor_name.numpy()
*gpu上的tensor不能直接转为numpy, 需要通过cpu()转化
ndarray = tensor_name.cpu().numpy()
3.2 numpy 转 torch.Tensor
tensor = torch.from_numpy(ndarray)
或
tensor = torch.tensor(ndarray)
注:实践得出结论,数组nd.array类型转tensor, 方式tensor = torch.from_numpy(ndarray) 相比torch.tensor(nd.array)方法更快。
————————————————
版权声明:本文为CSDN博主「像阳光,像春天」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35037684/article/details/116525638