You can implement it just like that:
def custom_loss(loss1, loss2, w1, w2):
   return loss1 * exp(-w1) + w1 + loss2 * exp(-w2) + w2

and make sure you define the tensors w1, w2 with requires_grad=True and pass them to your optimizer so that they can be optimized.