繁体   English   中英

PyTorch:如何使用循环将 append 张量放入列表

[英]PyTorch: How to append tensors into a list using loops

我有以下代码在列表中输出 2 arrays:

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

Output:

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

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

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

提前谢谢了!

最好先将其转换为张量,然后可以使用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))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM