import torch
import numpy as np
from torch.autograd import Variable
list_1 = [1, 2, 3, 4, 5]
# list to ndarray
ndarray_1 = np.array(list_1)
# ndarray to list
list_2 = ndarray_1.tolist()
ndarray_2 = np.array([1, 2, 3, 4, 5])
# ndarray to tensor
tensor_1 = torch.from_numpy(ndarray_2)
# tensor to ndarray
ndarray_3 = tensor_1.numpy()
# numpy to Variable
variable_1 = Variable(torch.from_numpy(ndarray_3))
# variable to numpy
ndarray_4 = variable_1.numpy()
# 访问修改variable.data
variable_1.data = torch.from_numpy(np.random.uniform(2, 3, size=(5, 1)))
ndarray 与 pytorch tensor , variable 相互转化
最新推荐文章于 2022-07-23 14:10:49 发布