【问题标题】:How to convert a tensor into a list of tensors如何将张量转换为张量列表
【发布时间】:2021-11-10 04:04:43
【问题描述】:

如何将张量转换为张量列表。例如:P1 是一个带有 60 值的 torch.Tensor,我想要一个带有 60 张量的张量列表。

【问题讨论】:

  • 您是否正在寻找一个更优雅的没有循环的?因为您可以循环并将它们附加到一个空列表中。

标签: python list pytorch tensor


【解决方案1】:

您可以使用listtorch.Tensor 强制到一个列表

>>> P1 = torch.rand(60)
>>> list(P1)
[tensor(0.5987),
 tensor(0.5321),
 tensor(0.6590),
 ...
 tensor(0.1381)]

这也适用于多维张量:

>>> P1 = torch.rand(60, 2)
>>> list(P1)
[tensor([0.4675, 0.0430]),
 tensor([0.2175, 0.6271]),
 tensor([0.3378, 0.8516]),
 ...,
 tensor([0.5099, 0.3411]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2019-07-29
    • 2018-08-04
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多