【问题标题】:How to index tensor by using all but given indices in pytorch?如何通过在pytorch中使用除给定索引之外的所有索引来索引张量?
【发布时间】:2022-01-27 04:35:00
【问题描述】:

假设我有一个张量a

>>> a
tensor([[2, 8],
        [3, 0],
        [4, 2],
        [2, 2],
        [6, 8]])

我还有一个张量 idx,其中包含我不想选择的索引

>>> idx
tensor([0, 3, 4])

我应该如何继续并选择除索引存在于张量 idx 中的行之外的所有行? 对于上面的张量aidx,选择后我应该得到输出张量b

>>> b
tensor([[3, 0],
        [4, 2]])

【问题讨论】:

    标签: python indexing pytorch


    【解决方案1】:

    a[[i for i in range(len(a)) if i not in idx]]

    【讨论】:

    • 感谢您的回答,但我正在寻找不使用 for 循环的解决方案,以便生成的操作可以利用 GPU
    猜你喜欢
    • 2019-09-16
    • 2019-01-13
    • 2020-03-28
    • 2019-11-26
    • 2021-11-04
    • 2021-02-11
    • 2021-12-16
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多