将 longtensor 格式的索引转换为火炬中的二进制选择掩码

Convert indices in longtensor format to binary selection mask in torch

我有一个 LongTensor,它包含我想要的另一个张量的所有索引。如何将此 LongTensor 转换为可用作选择掩码的 ByteTensor。

假设,

th> imageLabels:size()
 17549
     3
[torch.LongStorage of size 2]

                                                                      [0.0001s]
th> indices
  1
 22
 32
[torch.LongTensor of size 3]

我需要一种使用 [index] 表示法访问 imageLabel 的方法,以便我可以就地更改 imageLabel 中的一些值。

有什么办法吗?据我从文档中了解到,:index, :narrow operations return 一个全新的张量。

Correct, :index, narrow return 一个新张量,新张量使用与文档中所述相同的原始存储 here: "For methods narrow, select and sub the returned tensor shares the same Storage as the original"

我最终使用了 indexFill。

targetTensor:indexFill(1, indices, 0)

  • 第一个参数是维度,
  • indices 是包含我们感兴趣的所有索引的 LongTensor
  • 0 是要填充的值。可以是任意数字

希望这对您有所帮助。全部在文档中。我们要耐心看完。