# Does loss.item() affect loss.backward()?

**URL:** <https://discuss.pytorch.org/t/does-loss-item-affect-loss-backward/212601>\
**Category:** autograd\
**Created:** [November 6, 2024, 7:44am UTC](https://discuss.pytorch.org/t/does-loss-item-affect-loss-backward/212601 "2024-11-06T07:44:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Zakhary](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/michael_zakhary/32/72533_2.png) [@Michael\_Zakhary](https://discuss.pytorch.org/u/Michael_Zakhary)\
**Post date:** [November 6, 2024, 7:44am UTC](https://discuss.pytorch.org/t/does-loss-item-affect-loss-backward/212601/1 "2024-11-06T07:44:08Z")

</div>

Hello, just wanted to ask what is the correct order to call loss.item() and loss.backward(). I read a forum post saying that if you call item() after backward() execution is slower, however I also read that if you use .item() before backward() then the gradient does not flow. Can someone help me find a concrete answer for this?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [November 6, 2024, 1:34pm UTC](https://discuss.pytorch.org/t/does-loss-item-affect-loss-backward/212601/2 "2024-11-06T13:34:57Z")

</div>

Most likely the order won’t matter.  
Calling `item()` on a `CUDATensor` will synchronize your code and will thus potentially slow it down (e.g. if it wasn’t synchronized before anyway). I wouldn’t expect to see a large difference in speed if you synchronize the code before or after the `backward` call.

Calling `loss.item()` does not have any effect on the `loss.backward()` call unless you chain them `loss.item().backward()` which will raise an error.

---

<div class="post-metadata">

**Author:** ![Michael\_Zakhary](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/michael_zakhary/32/72533_2.png) [@Michael\_Zakhary](https://discuss.pytorch.org/u/Michael_Zakhary)\
**Post date:** [November 11, 2024, 7:51am UTC](https://discuss.pytorch.org/t/does-loss-item-affect-loss-backward/212601/3 "2024-11-11T07:51:14Z")

</div>

So to make sure I understand correctly, there is no need to take care of their order, and if possible, removing .item() will speed up my training code? I don’t think I synchronize my code elsewhere, I have a very basic training loop, although I am not completely aware of what synchronizing your code is exactly.
