How to update weight in mask region of a tensor?

I have a tensor size of 2x4x64x64x64 (bxcxdxhxw)

I only want to compute grad ( update weight in certain region of the tensor). For example, the region from 16 to 48 in both d,h,w. So, I made a mask such as

mask=torch.zeros_like(input_tensor)
mask [:,:,16:d-16, 16:h-16,16:w-16]=1.0
input_tensor=input_tensor*mask

However, my updated weight always zero. What is happen in my code? Thanks

Hi,

I used your code and checked. I initialized input_tensor with random values. Only the values in the given region is non zero. Others are zero.

import torch

d = h = w = 64
input_tensor = torch.randn((2, 4, d, h, w))
print(input_tensor[0,0, 35, 35, 35])
print(input_tensor[0, 0, 0, 0, 0])
mask=torch.zeros_like(input_tensor)
mask [:,:,16:d-16, 16:h-16,16:w-16]=1.0
print(mask[0,0, 35, 35, 35])
print(mask[0, 0, 0, 0, 0])
input_tensor=input_tensor*mask

print(input_tensor[0,0, 35, 35, 35])
print(input_tensor[0, 0, 0, 0, 0])

If you are getting zero for all the entries, probably you may be passing a zero input_tensor as input (or at least zeros in the given region)

Thanks

1 Like