# Backward pass for this setting

**URL:** <https://discuss.pytorch.org/t/backward-pass-for-this-setting/138067>\
**Category:** Uncategorized\
**Created:** [November 29, 2021, 8:49am UTC](https://discuss.pytorch.org/t/backward-pass-for-this-setting/138067 "2021-11-29T08:49:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![conv8d](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/e5b9ba/32.png) [@conv8d](https://discuss.pytorch.org/u/conv8d)\
**Post date:** [November 29, 2021, 8:49am UTC](https://discuss.pytorch.org/t/backward-pass-for-this-setting/138067/1 "2021-11-29T08:49:56Z")

</div>

I have 2 models `m1` and `m2` and 2 losses `l1` and `l2`  
I want to train both models jointly.  
`m1` needs to be updated using `(l1 + l2)` and `m2` only by `l2`.  
To achieve this, I have 2 optimizers `o1` (for `m1`'s parameters) and `o2` (for `m2`'s parameters)  
Is the following approach correct for achieving what I want?

```auto
y1 = m1(x1)
y2 = m2(x2)
loss1 = l1(y1, y) # y is ground truth
loss2 = l2(y1, y2) # l2 works on the outputs for m1 and m2
loss = loss1 + loss2 
loss.backward()
o1.step()
o2.step()

```

Here, I make the assumption that `d loss / d m2` will be same as `d loss2 / d m2`

---

<div class="post-metadata">

**Author:** ![InnovArul](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/innovarul/32/5282_2.png) [@InnovArul](https://discuss.pytorch.org/u/InnovArul)\
**Post date:** [November 29, 2021, 10:36am UTC](https://discuss.pytorch.org/t/backward-pass-for-this-setting/138067/2 "2021-11-29T10:36:21Z")

</div>

This seems to be correct.
