# How to calculate loss related to intermediate feed forward results

**URL:** <https://discuss.pytorch.org/t/how-to-calculate-loss-related-to-intermediate-feed-forward-results/10278>\
**Category:** Uncategorized\
**Created:** [November 23, 2017, 4:25am UTC](https://discuss.pytorch.org/t/how-to-calculate-loss-related-to-intermediate-feed-forward-results/10278 "2017-11-23T04:25:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terry93](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/terry93/32/2401_2.png) [@Terry93](https://discuss.pytorch.org/u/Terry93)\
**Post date:** [November 23, 2017, 4:25am UTC](https://discuss.pytorch.org/t/how-to-calculate-loss-related-to-intermediate-feed-forward-results/10278/1 "2017-11-23T04:25:40Z")

</div>

Hi,

I have the following problem: I have implemented 7 layers neural network. My Loss function contains 2 parts, the 1st part is the cross entropy of classification problem which is easy to implement and calculate the backward process.

However, for the 2nd part, my loss is related to the output of the 6th layer, which is an intermediate feed forward result.

May I ask how to calculate the loss of the 2nd part, and combine with the 1st part to do BP?

thanks for your reading, and, HAPPY THANKSGIVING!

P.Y.

---

<div class="post-metadata">

**Author:** ![colesbury](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/colesbury/32/26_2.png) [@colesbury](https://discuss.pytorch.org/u/colesbury)\
**Post date:** [November 23, 2017, 6:03am UTC](https://discuss.pytorch.org/t/how-to-calculate-loss-related-to-intermediate-feed-forward-results/10278/2 "2017-11-23T06:03:41Z")

</div>

Sum the losses:

```auto
loss = loss1 + loss2
loss.backward()

```

Use the output of the sixth layer directly. Don’t put it in an `nn.Sequential` if that’s what you’re doing:

```auto
def forward(self, x):
  x = self.layer1(x)
  x = self.layer2(x)
  x = self.layer3(x)
  x = self.layer4(x)
  x = self.layer5(x)
  l6 = self.layer6(x)
  l7 = self.layer7(l6)
  loss1 = my_loss_fn(l7)
  loss2 = my_loss_fn(l6)
  return loss1 + loss2

```

---

<div class="post-metadata">

**Author:** ![jdhao](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jdhao/32/2558_2.png) [@jdhao](https://discuss.pytorch.org/u/jdhao)\
**Post date:** [November 23, 2017, 7:42am UTC](https://discuss.pytorch.org/t/how-to-calculate-loss-related-to-intermediate-feed-forward-results/10278/3 "2017-11-23T07:42:12Z")

</div>

I think you can also back prop the two losses separately. After you have calculated the two losses, you can do

```auto
loss1.backward(retain_graph=True) # retain_graph is set to True so that you can back prop second loss
loss2.backward()

```

---

<div class="post-metadata">

**Author:** ![erichhhhho](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/f08c70/32.png) [@erichhhhho](https://discuss.pytorch.org/u/erichhhhho)\
**Post date:** [March 5, 2019, 4:14am UTC](https://discuss.pytorch.org/t/how-to-calculate-loss-related-to-intermediate-feed-forward-results/10278/4 "2019-03-05T04:14:17Z")

</div>

Hi, I tried `loss = loss1 + loss2 loss.backward()` However, the loss1 doesn’t decrease after backprop while loss2 can drop. Could you tell me how to solve this problem?
