# Total loss of multi-task model

**URL:** <https://discuss.pytorch.org/t/total-loss-of-multi-task-model/148445>\
**Category:** Uncategorized\
**Created:** [April 6, 2022, 10:34am UTC](https://discuss.pytorch.org/t/total-loss-of-multi-task-model/148445 "2022-04-06T10:34:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikiguo93](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/nikiguo93/32/30791_2.png) [@nikiguo93](https://discuss.pytorch.org/u/nikiguo93)\
**Post date:** [April 6, 2022, 10:34am UTC](https://discuss.pytorch.org/t/total-loss-of-multi-task-model/148445/1 "2022-04-06T10:34:13Z")

</div>

Hi guys, I have a multi-task model which would do regression and classfication together. I use mse loss and cross entropy loss for regression problem and classification problem, respectively. Then the total loss is defined according to [custom\_losses\_pytorch/multi\_task\_loss.py at master · ywatanabe1989/custom\_losses\_pytorch · GitHub](https://github.com/ywatanabe1989/custom_losses_pytorch/blob/master/multi_task_loss.py)  
import torch

class MultiTaskLoss(torch.nn.Module):  
def **init** (self, is\_regression, reduction=‘none’):  
super(MultiTaskLoss, self). **init** ()  
self.is\_regression = is\_regression  
self.n\_tasks = len(is\_regression)  
self.log\_vars = torch.nn.Parameter(torch.zeros(self.n\_tasks))  
self.reduction = reduction

def forward(self, losses):  
dtype = losses.dtype  
device = losses.device  
stds = (torch.exp(self.log\_vars)**(1/2)).to(device).to(dtype)  
self.is\_regression = self.is\_regression.to(device).to(dtype)  
coeffs = 1 / ( (self.is\_regression+1)\*(stds**2) )  
multi\_task\_losses = coeffs\*losses + torch.log(stds)

```
if self.reduction == 'sum':
  multi_task_losses = multi_task_losses.sum()

```

With the total loss decreasing, the mse loss went up and the cross entropy loss declined. And the accuracy is not satisfactory. Can anybody give me some clues to improve that?  
total loss:

 ![1649240879(1)](https://discuss.pytorch.org/uploads/default/original/3X/4/3/4334329514c59155a9881a7b1bdea827e785fbfd.png)  
classification accuracy:  
 ![1649240946(1)](https://discuss.pytorch.org/uploads/default/original/3X/b/f/bf4e3d0fb072906ea4b277cfb9868bfd1b051c22.png)  
mse loss for regression problem:  
 ![1649241139(1)](https://discuss.pytorch.org/uploads/default/original/3X/7/9/7911666f6418d41ac8bdc6536a43587313a66763.png)  
cross entropy loss for classification problem:  
 ![1649241170(1)](https://discuss.pytorch.org/uploads/default/original/3X/d/5/d595b01435adc3e0922c71505dc94bb69519907d.png)

---

<div class="post-metadata">

**Author:** ![KFrank](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/k/ecb155/32.png) [@KFrank](https://discuss.pytorch.org/u/KFrank)\
**Post date:** [April 7, 2022, 11:53pm UTC](https://discuss.pytorch.org/t/total-loss-of-multi-task-model/148445/2 "2022-04-07T23:53:09Z")

</div>

Hi Nikiguo!

> [@nikiguo93](#):
>
> ```python
> class MultiTaskLoss(torch.nn.Module):
> ...
> self.log_vars = torch.nn.Parameter(torch.zeros(self.n_tasks))
> ...
> 
> ```
> 
> With the total loss decreasing, the mse loss went up and the cross entropy loss declined. And the accuracy is not satisfactory.

Your `MultiTaskLoss` is a scheme to “learn” the appropriate relative  
weighting for the two losses for the two dissimilar tasks – regression  
and classification – that you are training your network to perform at  
the same time.

I have three comments:

`self.log_vars` is a “learnable” parameter that is to be trained by your  
optimizer. Make sure that you have added `self.log_vars` to whatever  
optimizer you are using. Otherwise it won’t get updated, and you’ll just  
be summing your two losses together, which could easily not be giving  
enough weight to your “mse loss.”

It’s possible that your use case isn’t well suited to the way that  
`MultiTaskLoss` works. I generally advise trying the “simple” approach  
first, even if the fancier approach should (and maybe even does) work  
better. Try leaving out `MultiTaskLoss` and just sum your two losses  
together with a weighting factor. Can you get your network to train with  
both losses going down reasonably by adjusting your weighting factor  
appropriately? (In essence, you would be doing by hand what  
`MultiTaskLoss` is supposed to do for you automatically.)

Your training looks fine. I don’t see any evidence of your cross-entropy  
loss or accuracy plateauing or of overfitting. The only problem is that  
your mse loss starts going up after falling initially. This suggest that your  
mse loss is not weighted heavily enough in your total loss. This could  
happen if you failed to include `self.log_vars` in your optimizer (or if  
`MultiTaskLoss` just doesn’t work for your use case). But it could also  
happen if it takes a while for `self.log_vars` itself to train properly.  
Your charts run for about 650 “somethings,” which I imagine might be  
epochs, For many substantive problems, this isn’t very much training  
at all, so you might try training significantly longer (after verifying that  
`self.log_vars` is being updated by your optimizer). Perhaps the  
relative loss weight will start to be learned properly, your mse loss  
will be weighted more heavily, and mse loss will start going down.  
(And even if your regression problem doesn’t train well, maybe your  
classification problem will continue to train effectively and your  
classification accuracy will become “satisfactory.”)

Best.

K. Frank
