# Why by adding second loss and applying the back.ward(), generated gradient is as same as before?

**URL:** <https://discuss.pytorch.org/t/why-by-adding-second-loss-and-applying-the-back-ward-generated-gradient-is-as-same-as-before/94625>\
**Category:** autograd\
**Created:** [August 31, 2020, 12:38am UTC](https://discuss.pytorch.org/t/why-by-adding-second-loss-and-applying-the-back-ward-generated-gradient-is-as-same-as-before/94625 "2020-08-31T00:38:32Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [August 31, 2020, 1:18am UTC](https://discuss.pytorch.org/t/why-by-adding-second-loss-and-applying-the-back-ward-generated-gradient-is-as-same-as-before/94625/2 "2020-08-31T01:18:33Z")

</div>

You are detaching the computation graph by using numpy functions for `loss`:

```python
hist_rGau= np.histogram(Gaussy.squeeze(1).view(-1).detach().numpy(), bins=FFBins,range=[0 ,1])
count_r11 = hist_rGau[0]      
PGAUSSY=count_r11/(count_r11.sum())        
hist_rGAN = np.histogram(fake.squeeze(1).view(-1).detach().numpy(), bins=FFBins,range=[0 ,1])
count_r22 = hist_rGAN[0]       
PGAN=count_r22/(count_r22.sum())
        
loss2=abs(PGAUSSY-PGAN).sum()

```

If you need to use numpy operations, you would need to implement the `backward` function manually via a [custom `autograd.Function`](https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html) or use PyTorch operations.

Double post from:  
[here](https://discuss.pytorch.org/t/what-does-the-backward-function-do/9944/18), [here](https://discuss.pytorch.org/t/how-are-optimizer-step-and-loss-backward-related/7350/15), [here](https://discuss.pytorch.org/t/optimizer-doesnt-update-weights/30420/7), [here](https://discuss.pytorch.org/t/why-loss-backward-didnt-update-parameters-gradient/19515/13), [here](https://discuss.pytorch.org/t/custom-loss-functions/29387/47).  
Please don’t repost the same question in 6 different threads, as this will only waste the time of community members, which might be looking into your issue.

---

_[View the full topic](https://discuss.pytorch.org/t/why-by-adding-second-loss-and-applying-the-back-ward-generated-gradient-is-as-same-as-before/94625)._
