tensor张量,列表,numpy数组之间的转换

import torch 
my_list = [1,2,3,4,5]
#列表转tensor张量
my_tensor = torch.tensor(my_list)
import numpy as np
my_array = np.array([1,2,3,4,5])
#numpy数组转tensor张量
my_tensor_from_numpy = torch.tensor(my_array)
#tensor张量转numpy数组
array_from_tensor = my_tensor.numpy()
#tensor张量转列表
list_from_tensor = my_tensor.tolist()

你可能感兴趣的:(numpy,python,人工智能,tensor)