Change tensor in list inplace

Hi,
i want to do the same operations on tensors which are not always the exact dimensions and would like to use a list (for instance [4,1,100,100] and [4,3,100,100] as in monochrome vs. RGB images).

what i would like to do is something like this:

def crop_tensors_list(tensors_list):
for current_tensor in tensors_list:
current_tensor = current_tensor[:,0:50,0:50]
return tensors_list

if __name__ = = "__main__" :
tensors1 = torch.randn(4,3,100,100)
tensors2 = torch.randn(4,1,100,100)
tensors_list = [tensor1,tensor2]
tensors_list = crop_tensors_list(tensors_list)

however! - when i look:
tensors_list[0] is tensor1 --> it returns False.

anybody?

Hi @dudy_karl ,

You declare tensors1 and tensors2 but you reference tensor1 and tensor2 (with no s) when creating tensors_list. Otherwise what you wrote should work, as such:

def crop_tensors_list(tensors_list):
    for current_tensor in tensors_list:
        current_tensor = current_tensor[:,0:50,0:50]
return tensors_list


tensors1 = torch.randn(4,3,100,100)
tensors2 = torch.randn(4,1,100,100)
tensors_list = [tensors1,tensors2]
tensors_list = crop_tensors_list(tensors_list)

You cannot slice a Tensor inplace, so you won’t be able to do exactly this. But you can reassign the slice to the list:

for i in range(len(tensors_list)):
    tensors_list[i] = tensors_list[i][:,0:50,0:50]