今天给大家做一个pytorch和numpy数据之间的相互转换,代码比较简单,这里不再多说,直接上代码:
import numpy as np
import torch
#create a numpy array
x=np.array([[1,2],[3,4]])
print('x_numpy:',x)
#convert the numpy array to a torch tensor.
y=torch.from_numpy(x)
print('y_tensor:',y)
#convert the torch tensor to a numpy array
z=y.numpy()
print('z_numpy:',z)
输出结果: