1. torch.Tensor和numpy.ndarray相互转换 [url=][/url]
import torchimport numpy as np# <class 'numpy.ndarray'>np_data = np.arange(6).reshape((2,3))# <class 'torch.Tensor'>torch_data = torch.from_numpy(np_data)# <class 'numpy.ndarray'>tensor2array = torch_data.numpy()print('numpy array:\n',np_data,type(np_data), '\ntorch tensor:\n',torch_data,type(torch_data), '\ntensor to array:\n',tensor2array,type(tensor2array))
|