I have a tensor size of 16x16. I only want to compute the loss and update gradient in a region of interesting (ROI) of the tensor such as ROI[4:12,4:12]=1. I used the below code but it shows an error. How to do it? Thanks
import torch

input = torch.rand((16,16),requires_grad=True)
print (input)
mask = torch.zeros_like(input)
mask[4:12,4:12]=1.0
input = mask* input

d = torch.mean(input)
d.backward()
print (input.grad.data)

Error is
AttributeError                            Traceback (most recent call last)
<ipython-input-35-9826ca6fcef5> in <module>()
     10 d = torch.mean(input)
     11 d.backward()
---> 12 print (input.grad.data)

AttributeError: 'NoneType' object has no attribute 'data'

It looks that the input.grad is None