【问题标题】:PyTorch: How to append tensors into a list using loopsPyTorch:如何使用循环将张量附加到列表中
【发布时间】:2020-11-27 11:20:02
【问题描述】:

我有以下代码在列表中输出 2 个数组:

arr1 = np.array([[1.,2,3], [4,5,6], [7,8,9]])

arr_split = np.array_split(arr1,
                           indices_or_sections = 2,
                           axis = 0)

arr_split

输出:

[array([[1., 2., 3.],
        [4., 5., 6.]]), array([[7., 8., 9.]])]

如何将这 2 个数组转换为 PyTorch 张量并使用 for(或 while)循环将它们放入一个列表中,使它们看起来像这样:

[tensor([[1., 2., 3.],
         [4., 5., 6.]], dtype=torch.float64),
tensor([[7., 8., 9.]], dtype=torch.float64)]

非常感谢!

【问题讨论】:

标签: python list pytorch tensor


【解决方案1】:

最好先将其转换为张量,然后可以使用torch.Tensor.split

arr1 = np.array([[1.,2,3], [4,5,6], [7,8,9]])
t_arr1 = torch.from_numpy(arr1)

t_arr1.split(split_size=2)
(tensor([[1., 2., 3.],
        [4., 5., 6.]], dtype=torch.float64), 
 tensor([[7., 8., 9.]], dtype=torch.float64))

【讨论】:

  • 谢谢,但这对我来说不太适用,因为我需要按照我在上面的问题中提到的顺序执行这些步骤。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 2015-04-27
  • 2021-06-18
  • 2021-05-24
  • 2021-02-22
相关资源
最近更新 更多