I think you can also back prop the two losses separately. After you have calculated the two losses, you can do
loss1.backward(retain_graph=True) # retain_graph is set to True so that you can back prop second loss
loss2.backward()