Search code examples
luadeep-learningtorch

Convert indices in longtensor format to binary selection mask in torch


I have a LongTensor which contains all the indices I want from another tensor. How can I convert this LongTensor into a ByteTensor that can be used as a selection mask.

Assume,

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

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

I need a way to access imageLabels using [index] notation so that I can change some values in imageLabels in-place.

Is there any way to do this? As far as I understood from the docs, :index, :narrow operations return a completely new Tensor.


Solution

  • I ended up using indexFill.

    targetTensor:indexFill(1, indices, 0)

    • the first argument is the dimension,
    • indices is the LongTensor containing all the indices we are interested in
    • 0 is the value to fill. Can be any number

    Hope this helps. Its all in the docs. We have to read it patiently.