I wanted to slice and change the values for certain conditions. Look at the toy code I wrote.
a = torch.randn(3, 3)
a[:, :][a > 0] = 0
This works, but if you sliced like this
a [:, (0, 1, 2)] [a > 0] = 0
It does not work.
Why? Do they work differently?
I tired to explan it in storage view. I used
.data_ptr() to check whether tensors have the same storage.
In the first case,
a.data_ptr() == a[:,:].data_ptr(), so they share the same storage, value changing will also perform on original
In the second case,
a.data_ptr() != a[:, (0,1,2)].data_ptr(), they donot use the same base.
Im not sure whether it is a proper way to explan it, your use case may related to indexing operations, i will google it further. If I am wrong, correct me please.
EDIT: here is the answer.