python的array和tensor相互转化
import torch
import numpy as np
np_data=np.arange(6).reshape((2,3))
torch_data=torch.from_numpy(np_data)
tensor2array=torch_data.numpy()
print(
'\nnumpy\n',np_data,
'\ntorch\n',torch_data,
'\ntensor2array\n',tensor2array,
)
结果如下
numpy
[[0 1 2]
[3 4 5]]
torch
tensor([[0, 1, 2],
[3, 4, 5]], dtype=torch.int32)
tensor2array
[[0 1 2]
[3 4 5]]