I’m trying to create a mask based on an index tensor.
The mask size is [6, 1, 25]
The index size is [6, 1, 12]
First I have an index tensor indices:
print(indices)
tensor([[[ 0,  1,  2,  5,  6,  7, 12, 17, 18, 22, 23, 21]],

        [[ 2,  3,  4,  7,  8,  9, 15, 16, 20, 21, 22, 13]],

        [[ 0,  1,  5,  6, 10, 11, 15, 16, 17, 20, 21, 12]],

        [[ 1, 10, 15, 16, 17, 18, 20, 21, 22, 23, 24,  2]],

        [[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 13]],

        [[ 3,  4,  8,  9, 13, 14, 18, 19, 22, 23, 24, 17]]], device='cuda:0')

Then I allocate a mask tensor
mask = torch.cuda.ByteTensor(6, 1, 25).zero_()

Now I want to set only [0, 0, 0], [0, 0, 1], …, [0, 0, 23], [0, 0, 25], etc. in tensor mask to 1
I have tried:
mask[indices] = 1,
I have also tried adapting solutions given in

  
    
    
    Generating a meshgrid or itertools.product in pytorch 
  
  
    Hi all, 
I’m trying to generate a meshgrid directly in pytorch… 
The result I’m looking for, given two arrays of [0,1], is this: 
[[0,0],
 [0,1],
 [1,0],
 [1,1]]

You can do this with np.meshgrid: 
    grid = np.meshgrid(range(2), range(2), indexing='ij')
    grid = np.stack(grid, axis=-1)
    grid = grid.reshape(-1, 2)

and with itertools.product : 
grid = list(itertools.product(range(2),range(2)))

But does anyone reckon how you’d do that directly in pytorch? 
Thanks! 
EDIT: Hm, this seems to …
  



  
    
    
    How to select values using 2 index tensor from a 3D tensor? 
  
  
    In that case, you can just index t: 
out = t[x[:, None], y]
print(out)
> tensor([[  1,   3,   5,   7,   9,  11,  13,  15],
          [ 33,  35,  37,  39,  41,  43,  45,  47],
          [ 65,  67,  69,  71,  73,  75,  77,  79],
          [ 97,  99, 101, 103, 105, 107, 109, 111],
          [129, 131, 133, 135, 137, 139, 141, 143],
          [161, 163, 165, 167, 169, 171, 173, 175],
          [193, 195, 197, 199, 201, 203, 205, 207],
          [225, 227, 229, 231, 233, 235, 237, 239]])

The last ro…
  


to this problem unsuccessfully.
Any help would be appreciated, thank you.