索引张量列表
index a list of tensors
我有一个名为“imgs”的张量列表对象(50 张图像)。
我有一个长度为 29 的索引 (indi) 数组。
如何使用索引数组索引张量列表?
当我执行以下操作时,我得到:
imgs[indi]
TypeError: only integer scalar arrays can be converted to a scalar index
谢谢
假设这些是正常的 python 列表,那么您可以使用列表理解
result = [imgs[i] for i in indi]
这将给出一个张量列表。
如果您还想将其设为包含图像的单个张量,您可以使用 torch.stack
result = torch.stack([imgs[i] for i in indi], dim=0)
我有一个名为“imgs”的张量列表对象(50 张图像)。 我有一个长度为 29 的索引 (indi) 数组。
如何使用索引数组索引张量列表?
当我执行以下操作时,我得到:
imgs[indi]
TypeError: only integer scalar arrays can be converted to a scalar index
谢谢
假设这些是正常的 python 列表,那么您可以使用列表理解
result = [imgs[i] for i in indi]
这将给出一个张量列表。
如果您还想将其设为包含图像的单个张量,您可以使用 torch.stack
result = torch.stack([imgs[i] for i in indi], dim=0)