If I have multiple losses that I sum together, do I have to declare separate loss functions?
I'm trying to train a model that uses multiple losses. I've noticed that in some implementations a single loss function is declared as loss_fct = nn.LossFunction() and used multiple times to calculate separate losses, and those losses are summed and backpropagated. For example:
loss_fct = nn.LossFunction()

loss1 = loss_fct(pred1, labels1)
loss2 = loss_fct(pred2, labels2)

total_loss = loss1 + loss2
total_loss.backward()
optimizer.step()

Is this okay or should I be declaring separate loss function objects for each loss? If this is okay, why is it okay? It just seems counterintuitive to me because they're separate losses that we're optimizing with different purposes. Summing and backpropagating once makes somewhat sense, but I can't wrap my head around using a single loss object.

The reason you have to create an instance of some loss function at all is that some loss functions have optional parameters that affect how they're computed.
For example MSELoss supports different reduction modes.
If you're computing the same loss function (with the same parametrization of the loss function itself) on each of your outputs then there is no need to instantiate multiple instances of the loss function.
