Hi Had!



 hadaev8:

I want to reverse the gradient sign from the output of model net1 but calculate net0(inp0) as usual.


As an alternative to using a hook, you could write a custom Function
whose forward() simply passes through the tensor(s) unchanged, but
whose backward() flips the sign of the gradient(s).
You would then insert it at the desired place in your network, e.g.:
out1 = net0 (GradientReversalFunction.apply (net1 (inp1)))

I don’t really have an opinion about which method is cleaner.
Best.
K. Frank