Hi,
Isn’t the problem that imgs is not a leaf tensor? You can check with imgs.is_leaf.
In particular, .grad field is only populated for leaf Tensors. If you want it for other Tensors, you can use the imgs.retain_grad() 10 function to get the .grad field populated for non-leaf Tensors.