Lua 手电筒相当于 np.where()?

Lua Torch equivalent of np.where()?

我有一个 ByteTensor 并且想获取有 1 的索引。在 numpy 中,我可以做类似

a = np.array([1,0,1,0,1])
return np.where(a)

这会 return (array([0, 2, 4]),)。此功能是否在 Torch 中定义?

(在我的特定情况下,我想使用这些索引来索引几个不同的 Tensor 对象,但如果知道一般情况下如何执行此操作会很好。)

您可以使用torch.nonzero,例如:

> a = torch.ByteTensor{1,0,1,0,1}
> print(torch.nonzero(a))                                                                                         
 1                                                                                                                  
 3                                                                                                                  
 5                                                                                                                  
[torch.LongTensor of size 3x1]

如果你真的需要找到 1-s only 你可以链接一个逻辑运算符:

> a = torch.ByteTensor{1,2,1,6,1}
> a:eq(1):nonzero()