Thanks for the suggestion. Just few questions
(1) loss = task_loss + loss_weighting * penalty_loss and then loss.backward(). Why do I backward on penalty_loss ? Since torch.max(-gX2, 0).sum()  already gives my the derivative of X2; Isn’t  loss.backward() gonna compute the second derivative of X2 ?