Solved by Tudor_Berariu in post #2 
              
              
                If f(x) is a scalar you can just scale the loss function with its value before performing backpropagation. Detach the variable if f uses any tensors that require gradients. 
(loss * f(x).detach()).bacward()