How to modify the tensor only in certain positions

I try to modify this tensor that just contain some images from a Dataloader, but it’s seem impossible in PyTorch to modify anything when we use mask for some positions.

import copy
import torch

#Shape data: [50000, 12, 32, 32]
data_2 = copy.copy(data); #to check if it's exactly like before

#Skip position: [0, 4, 8]
index_list = [1,2,3,5,6,7,9,10,11];

#Both have shape: [1,12,1,1]
mean = tensor_norm[no_selected][0][:,index_list];
std = tensor_norm[no_selected][1][:,index_list];

#Manually apply Z normalization because you have already applied another 
#Z normalization on the original RGB images.
data[:,index_list]  = (data[:,index_list] - mean)/std;

#Check if exactly as before
print(torch.all(data == data_2));
#Always True

One solution that I found was simply put mean = 0 and std = 1 where I do not want to change and then do the calculation with all the tensor. However, there should be a mask allowing to modify only certain indices.

After some tests, I see that in fact “copy.copy” always gives something that will also change, but “tensor.clone” seems to be the right way to compare:


data_copy = copy.copy(data);
data_clone = data.clone();

data[:, index_list] = data[:, index_list] + 1;
print(torch.all(data == data_copy));

print(torch.all(data == data_clone));