Thanks for the reply!
I want to apply a loss function to the gradients: in particular, I want to penalize negative gradients with respect to any input. If I got the (N,1) tensor dNetdInp, then I could simply take nn.ReLU(-dNetdInp) and then sum the result.
Is there a way to adapt your solution for this kind of use case? (Applying relu to the sum wouldn’t give me the right loss.)
Could grad_outputs=torch.ones_like(out) work for this?