# Confusion about loss.backward() and how it updates gradients

**URL:** <https://discuss.pytorch.org/t/confusion-about-loss-backward-and-how-it-updates-gradients/54355>\
**Category:** autograd\
**Created:** [August 26, 2019, 12:32pm UTC](https://discuss.pytorch.org/t/confusion-about-loss-backward-and-how-it-updates-gradients/54355 "2019-08-26T12:32:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobbinDG](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/f6c823/32.png) [@RobbinDG](https://discuss.pytorch.org/u/RobbinDG)\
**Post date:** [August 26, 2019, 12:32pm UTC](https://discuss.pytorch.org/t/confusion-about-loss-backward-and-how-it-updates-gradients/54355/1 "2019-08-26T12:32:09Z")

</div>

Hello!

I’m an experienced programmer, familiar to neural nets (theoretically) but new to pytorch. I’m trying out pytorch in `C++` and am currently writing the learning stage of my simple network (1 hidden layer).

I have an optimizer and have calculated a loss tensor, all according to the tutorial found [here](https://pytorch.org/tutorials/advanced/cpp_frontend.html). The tutorial then calls `d_loss_fake.backward()`. I did some digging and this apparently updates the gradients of the network. I am confused however about how it achieves this, as I could not find any relations between the loss tensor and the network. Concretely: how does `.backward()` know which weights to update if there is no connection between the loss tensor and the network?

Sorry for the beginner question. I hope this isn’t a duplicate, I’ve been googling for a couple of hours now. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![andreaskoepf](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/andreaskoepf/32/15327_2.png) [@andreaskoepf](https://discuss.pytorch.org/u/andreaskoepf)\
**Post date:** [August 26, 2019, 11:50pm UTC](https://discuss.pytorch.org/t/confusion-about-loss-backward-and-how-it-updates-gradients/54355/2 "2019-08-26T23:50:03Z")

</div>

Maybe a starting point is [https://pytorch.org/docs/stable/notes/autograd.html](https://pytorch.org/docs/stable/notes/autograd.html)

Behind the scenes PyTorch tracks all operations on tensors with `requires_grad == true` and builds a computation graph during the forward pass. It knowns how the loss value was calculated and can automatically back-propagate the gradient step by step from the loss (or any scalar model output) to the model parameters.
