How to mask and assign a value to tensor

What i wanted to is from a mask update a value of a tensor using a mask.
x = torch.randint(3, 10, (100, 3), dtype=torch.long)
print(x[50]) # prints a random tensor of size (3)
mask = torch.zeros(100).byte() # generate a random mask
mask[50] = 1
print(x[mask], x[mask][0]) # prints tensor of size (1,3), tensor of size (3,)
x[mask][0][0] = 2 # assign a value to
print(x[mask][0][0]) # Doesn’t change change the value in tensor x

You can assign a value directly to x[mask], but when you index into it, x[mask][0] creates a copy of x[mask].

1 Like

Why? Does mask get a view and index will create a copy?

Yeah, why it is designed like this way?

43%20AM

It is the same behaviour as numpy’s , legit