【问题标题】:Is there any diffrence between index_select and tensor[sequence] in PyTorch?PyTorch 中的 index_select 和 tensor[sequence] 之间有什么区别吗?
【发布时间】:2020-04-08 05:10:38
【问题描述】:

每个人。我是 PyTorch 的新手。现在我正在学习张量的索引。我注意到我们可以通过tensor.index_select()tensor[sequence] 来索引张量。

In [1]: x = torch.randn(3, 4)

In [2]: indices = torch.tensor([0, 2])

In [3]: x.index_select(0, indices)
Out[3]:
tensor([[ 0.2760, -0.9543, -1.0499,  0.7828],
        [ 1.3514, -1.1289,  0.5052, -0.0547]])

In [4]: x[[0,2]]
Out[4]:
tensor([[ 0.2760, -0.9543, -1.0499,  0.7828],
        [ 1.3514, -1.1289,  0.5052, -0.0547]])

我对这两种方法感到困惑并寻找一些文档。但我失败了。谁能告诉我它们之间有什么区别?这些区别是什么?

【问题讨论】:

    标签: python pytorch


    【解决方案1】:

    这看起来像是旧(较慢)索引的残余。

    this pull request

    我还认为您过去无法对张量进行二进制逻辑索引。

    a = torch.randn((1,3,4,4))
    dim = 2
    indices = [0,1]
    
    %timeit a.index_select(dim, torch.tensor(indices))
    
    12.7 µs ± 1.28 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    
    %timeit a[:,:,indices,:]
    
    16.7 µs ± 640 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-13
      • 2017-04-05
      • 2014-09-18
      相关资源
      最近更新 更多