Anirudhan J
Anirudhan J

Reputation: 2072

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.

Upvotes: 0

Views: 351

Answers (2)

Anirudhan J
Anirudhan J

Reputation: 2072

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.

Upvotes: 0

Ameen Eetemadi
Ameen Eetemadi

Reputation: 1

Correct, :index, narrow return a new tensor, the new tensor uses the same original storage as stated in the doc here: "For methods narrow, select and sub the returned tensor shares the same Storage as the original"

Upvotes: 0

Related Questions