torch和numpy的相互转换

"""
pytorch相当于神经网络中的numpy,它是以tensor的形式表示
"""
import torch
import numpy as np

# convert numpy to tensor or vise versa
np_data = np.arange(6).reshape((2, 3))
torch_data = torch.from_numpy(np_data) #numpy转换为torch
tensor2array = torch_data.numpy() #torch转换为numpy
print(
    '\nnumpy array:', np_data,
    '\ntorch tensor:', torch_data,
    '\ntensor to array:', tensor2array,
)

  

posted @ 2019-01-29 11:36  车路历程  阅读(7534)  评论(0编辑  收藏  举报