【问题标题】:Torch tensor equivalent function to matlab's "find"?火炬张量等效函数到matlab的“查找”?
【发布时间】:2015-07-29 01:44:03
【问题描述】:

简而言之,我想知道torch中是否有一个张量命令,它可以为我提供张量中满足特定条件的元素的索引。

这里是 matlab 代码,说明了我希望在 Torch 中能够做什么:

my_mat = magic(3); % returns a 3 by 3 matrix with the numbers 1 through 9
greater_than_fives = find(my_mat > 5); % find indices of all values greater than 5, the " > 5" is a logical elementwise operator that returns a matrix of all 0's and 1's and finally the "find" command picks out the indices with a "1" in them
my_mat(greater_than_fives) = 0;  % set all values greater than 5 equal to 0 

我知道我可以在 Torch 中使用 for 循环来执行此操作,但是是否有一些等效于 matlab 的 find 命令可以让我更紧凑地执行此操作?

【问题讨论】:

    标签: lua torch


    【解决方案1】:
    x[x:gt(5)] = 0
    

    一般有x:gt :lt :ge :le :eq

    还有一个通用的 :apply 函数,它接受一个匿名函数并将其应用于每个元素。

    【讨论】:

    • 感谢您的回复!但是,我对如何使用索引来索引到我的数组(这是您提供的)以及如何找到索引本身(除了索引到原始张量之外,这些索引通常对其他事情有用。)都很感兴趣。
    • 找到索引本身可以通过一个非常丑陋的黑客来完成:indices = torch.linspace(1, x:nElement(), x:nElement())[x:gt(5)]
    • 布尔索引似乎不适用于仅对一个张量维度进行切片。所以,有一个 find() / np.where() 的对应物将不胜感激。
    • 另外,值得指出的是,torch.linspace(1,x:nElement(), x:nElement()) 并不总是给你数字 1 到 x:nElement(),这是我刚刚学会的。相反,torch.range(1,x:nElement()) 是更好的方法。
    猜你喜欢
    • 1970-01-01
    • 2022-07-20
    • 2017-02-23
    • 1970-01-01
    • 2020-11-24
    • 1970-01-01
    • 2021-12-29
    • 2021-11-30
    • 2015-09-16
    相关资源
    最近更新 更多