# Calculating loss twice but return two different values

**URL:** <https://discuss.huggingface.co/t/calculating-loss-twice-but-return-two-different-values/113186>\
**Category:** 🤗Transformers\
**Created:** [October 21, 2024, 2:10pm UTC](https://discuss.huggingface.co/t/calculating-loss-twice-but-return-two-different-values/113186 "2024-10-21T14:10:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zianer](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/zianer/32/33216_2.png) [@Zianer](https://discuss.huggingface.co/u/Zianer)\
**Post date:** [October 21, 2024, 2:10pm UTC](https://discuss.huggingface.co/t/calculating-loss-twice-but-return-two-different-values/113186/1 "2024-10-21T14:10:12Z")

</div>

i am modifying the training\_step function of Trainer  
while i encounter following problems  
with self.compute\_loss\_context\_manager():  
loss = self.compute\_loss(model, inputs)  
loss2 = self.compute\_loss(model, inputs)  
my test suggest that the loss and loss2 are distinct from each other although i did not update params or change inputs, even with a another context\_manager, the losses are still different

 ![截屏2024-10-21 22.07.16](https://us1.discourse-cdn.com/hellohellohello/original/3X/e/e/ee43ec05fab2eff8cb7e3fc56ad0ee60544f31d4.png)  
transformers==4.28.1  
and i found that after the call of decoder forward function, it returns distinct outputs for the two loss calculation

---

<div class="post-metadata">

**Author:** ![Zianer](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/zianer/32/33216_2.png) [@Zianer](https://discuss.huggingface.co/u/Zianer)\
**Post date:** [October 21, 2024, 2:10pm UTC](https://discuss.huggingface.co/t/calculating-loss-twice-but-return-two-different-values/113186/2 "2024-10-21T14:10:47Z")

</div>

![截屏2024-10-21 22.08.52](https://us1.discourse-cdn.com/hellohellohello/original/3X/f/7/f7c1a7d50696088a283e44b7303a7bde5ad2721f.png)
