hyperactve:

However, for such a network these are 4 different samples so it should be able to compute 4 different gradients. Is there a way to get this effect?



If you only need to gradients w.r.t. the inputs (and don’t have bad things like batch norm), summing the outputs and then calling .backward will get you gradients. For weights it is more tricky to avoid gradients being accumulated.

inp.requires_grad_() before passing it into the network will tell PyTorch that you will ask to differentiate by inp.

Best regards
Thomas