In Pytorch can you use 2 error functions at the same time?

I am currently making this dataset which looks something like this:

[ [x, y], # ground truth for a coordinate
  [0], # 1-hot encodings for class identification, there are 4 of these
  [0] ]

For example, I am going to input some images and I want a vector output like above. I consider predicting coordinate as a regression problem and the 1 hot-encoding as a classification problem. Can I use MSEloss or L1loss on the first value and use cross-entropy for the bottom 4 values? I am self-taught so there is a lot that I don’t know sorry if this is a stupid question. Code example welcome.


I found this while doing some research,

b = nn.MSELoss() 
a = nn.CrossEntropyLoss() 
loss a = a(output_x, x_labels) 
loss_b = b(output_y, y_labels) 
loss = loss_a + loss_b loss.backward()

So in theory can I make a prediction with my network, eg y_hat and slice off the coordinates prediction and call it output_x and do the same thing for output_y for classification? Will this work with my problem?

For sure you can use multiple loss functions. For example PSPNet uses that extensively (called auxiliary output/loss). The below is such an example:

        out_seg, out_cls = model(x)
        seg_loss = seg_loss(out_seg, y_seg)
        cls_loss = cls_loss(out_cls, y_cls)
        loss = seg_loss + alpha * cls_loss

So the model has two outputs (typically from an earlier layer and the final layer). In this case, one is trained on classification and the other one on segmentation. The idea is that having a loss function directly on an earlier layer helps to train those earlier layers better.