# Two different loss function within a batch

**URL:** <https://discuss.pytorch.org/t/two-different-loss-function-within-a-batch/67287>\
**Category:** Uncategorized\
**Created:** [January 21, 2020, 4:36pm UTC](https://discuss.pytorch.org/t/two-different-loss-function-within-a-batch/67287 "2020-01-21T16:36:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![a-parida12](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/a-parida12/32/10028_2.png) [@a-parida12](https://discuss.pytorch.org/u/a-parida12)\
**Post date:** [January 21, 2020, 4:36pm UTC](https://discuss.pytorch.org/t/two-different-loss-function-within-a-batch/67287/1 "2020-01-21T16:36:32Z")

</div>

I have concatenated two different dataloaders in pytorch. My aim is to minimize `loss1` for samples coming from `dataloader1` and minimize `loss2` for samples coming from `dataloader2`.

What would be the ideal/ best methods to do it in pytorch?

---

<div class="post-metadata">

**Author:** ![richard](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/richard/32/1861_2.png) [@richard](https://discuss.pytorch.org/u/richard)\
**Post date:** [January 27, 2020, 7:01pm UTC](https://discuss.pytorch.org/t/two-different-loss-function-within-a-batch/67287/2 "2020-01-27T19:01:28Z")

</div>

What methods have you tried so far? Are you trying to minimize the losses at the same time? If so you might want to define some combined loss, say, `loss1 + loss2`.

---

<div class="post-metadata">

**Author:** ![a-parida12](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/a-parida12/32/10028_2.png) [@a-parida12](https://discuss.pytorch.org/u/a-parida12)\
**Post date:** [January 27, 2020, 8:24pm UTC](https://discuss.pytorch.org/t/two-different-loss-function-within-a-batch/67287/3 "2020-01-27T20:24:20Z")

</div>

My ultimate aim is to minimise `loss1` for some samples and `loss2` for other samples.

I tried minimising `a*loss1+b*loss2` for samples from dataloader 1 `a=1,b=0` and samples from dataloader 2 `a=0,b=1`. I think this is an hacky solution and wanted know if some special provisions in pytorch exist.
