Creating big tensors and insert ones at specific dimensions

Hello,

I’ve got problem to which I can’t find the proper answer.

I need to create very large tensors (with zeros) - my_tensor.shape = [256, 3, 500000] and assign to specific indexes “1”. I have a tensor with .shape = [256, 3] containing indexes at which should I assign “1” to every tensor inside my_tensor.

So I was doing it in the loop one by one and concatenate to create 256 sized batch (which is time consuming), and now I’m thinking if there is possibility to make it faster in that way:

my_tensor = torch.zeros([256, 3, 500000])
my_tensor[#here some magic indexes] = 1

Could you suggest me some efficient way to solve my problem?

Best regards

I think that I found a “bug”.
To reproduce, try first this:

import torch
t1 = torch.zeros([3, 3, 10])
t2 = torch.tensor([[0,1,2],[3,4,5],[6,7,8]])
t1[range(t2.shape[0]), torch.tensor([range(t2.shape[1])] * t2.shape[0]).T, t2.T] = 1
Output:
tensor([[[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
         [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]],

        [[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
         [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]],

        [[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
         [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.]]])

This works, and now with one small modification (type of the tensor modified):

import torch
t1 = torch.zeros([3, 3, 10])
t2 = torch.tensor([[0,1,2],[3,4,5],[6,7,8]], dtype=torch.uint8)
t1[range(t2.shape[0]), torch.tensor([range(t2.shape[1])] * t2.shape[0]).T, t2.T] = 1
Output:
IndexError: too many indices for tensor of dimension 3

Do you have some explanation for this?

Best regards

torch.uint8 was used as a bool index in past PyTorch versions and you should also get a warning:

UserWarning: indexing with dtype torch.uint8 is now deprecated, please use a dtype torch.bool instead. 

To index a tensor use LongTensors or a mask via BoolTensors.