# Adding a float variable as an additional loss term works?

**URL:** <https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363>\
**Category:** autograd\
**Created:** [March 5, 2018, 2:55am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363 "2018-03-05T02:55:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheIllusion](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/theillusion/32/3741_2.png) [@TheIllusion](https://discuss.pytorch.org/u/TheIllusion)\
**Post date:** [March 5, 2018, 2:55am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/1 "2018-03-05T02:55:45Z")

</div>

Suppose I have a loss term as shown below:

```
G_sample = G(z)
D_fake = D(G_sample)

# Generator loss
G_loss = 0.5 * torch.mean((D_fake - 1)**2)

G_loss.backward()
G_solver.step()
reset_grad()

```

If I add any ordinary float variable to G\_loss before .backward(),  
for example,  
G\_loss = G\_loss + avg\_pixel\_value

Does avg\_pixel\_value has any effect when doing back prop?  
Or it is completely ignored?

Since avg\_pixel\_value itself cannot use .backward() api, I have a doubt of its effectiveness.

My question is can I add ordinary float terms to PyTorch Loss?

Many thanks.

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 5, 2018, 6:33am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/2 "2018-03-05T06:33:30Z")

</div>

To differentiate a sum of two terms, you differentiate each term separately. Hence adding a constant value to your loss has no effect whatsoever on the gradients produced by backpropagation.

---

<div class="post-metadata">

**Author:** ![TheIllusion](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/theillusion/32/3741_2.png) [@TheIllusion](https://discuss.pytorch.org/u/TheIllusion)\
**Post date:** [March 5, 2018, 6:42am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/3 "2018-03-05T06:42:38Z")

</div>

Thanks for the reply.

Even though the additional term I mentioned doesn’t support .backward(), it’s not a constant but a variable(python float).

In my sample code,  
I think avg\_pixel\_value might affect the value of G\_loss.  
(avg\_pixel\_value changes every iteration)

G\_loss = G\_loss + avg\_pixel\_value

Is there any faults on my understanding?

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 5, 2018, 6:59am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/4 "2018-03-05T06:59:43Z")

</div>

avg\_pixel\_value is calculated from the input image without using any of the model parameters or outputs, so for backpropagation with respect to the model parameters, it is ignored.

Might I suggest a [lecture on backpropagation by Andrej Karpathy](https://youtu.be/GZTvxoSHZIo?list=PLlJy-eBtNFt6EuMxFYRiNRS07MCWN5UIA&t=192).

---

<div class="post-metadata">

**Author:** ![TheIllusion](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/theillusion/32/3741_2.png) [@TheIllusion](https://discuss.pytorch.org/u/TheIllusion)\
**Post date:** [March 5, 2018, 7:06am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/5 "2018-03-05T07:06:56Z")

</div>

Thanks for the answer.

What if I use model’s output as blow:  
G\_loss = G\_loss + mean\_output\_pixel\_value

But mean\_output\_pixel\_value is merely a float variable that doesn’t have graph information.

Can mean\_output\_pixel\_value affect back propagation?

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 5, 2018, 7:27am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/6 "2018-03-05T07:27:38Z")

</div>

Well, when you do G\_loss.backward() pytorch first looks at what operation produced G\_loss, in this case

```auto
G_loss = G_loss + mean_output_pixel_value

```

so pytorch retrieves the values that were summed and basically runs backward on both of them.  
But `mean_output_pixel_value` is either a float value which has no `.backward()` method, or it is a Variable calculated from `input_image` which is a Variable with requires\_grad=False. Therefore `mean_output_pixel_value.backward()` does nothing.

I shall try another explanation… The reason for running `G_loss.backward()` is to calculate a measure of how each parameter affected the value of G\_loss, this measure is stored in `param.grad` for each parameter.

Now obviously, the model parameters have no effect whatsoever on the `mean_output_pixel_value`, so adding it to `G_loss` will not change the calculated `param.grad` values in any way at all.

---

<div class="post-metadata">

**Author:** ![TheIllusion](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/theillusion/32/3741_2.png) [@TheIllusion](https://discuss.pytorch.org/u/TheIllusion)\
**Post date:** [March 6, 2018, 3:36am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/7 "2018-03-06T03:36:53Z")

</div>

I’m still confused.

After run  
G\_loss = G\_loss + mean\_output\_pixel\_value,

G\_loss will be updated.

Back propagation will be computed via multiplying G\_loss and the input values of each later layers.

That’s why I thought that the updated G\_loss will have some effect during backprop.

---

<div class="post-metadata">

**Author:** ![jpeg729](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jpeg729/32/3464_2.png) [@jpeg729](https://discuss.pytorch.org/u/jpeg729)\
**Post date:** [March 6, 2018, 8:03am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/8 "2018-03-06T08:03:44Z")

</div>

When you write `G_loss = G_loss + mean_output_pixel_value` python first calculates `G_loss + mean_output_pixel_value` and stores the result in a _new_ tensor, then python updates the name G\_loss to point to this result.

That line does not update the data stored in G\_loss. It basically acts like this: `new_G_loss = old_G_loss + mean_output_pixel_value`.

If you wanted to do an inplace addition, you could do

```auto
G_loss += mean_output_pixel_value

```

but that would either be non-differentiable, or pytorch would assume you meant `new_G_loss = old_G_loss + mean_output_pixel_value`

---

<div class="post-metadata">

**Author:** ![TheIllusion](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/theillusion/32/3741_2.png) [@TheIllusion](https://discuss.pytorch.org/u/TheIllusion)\
**Post date:** [March 6, 2018, 9:46am UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/9 "2018-03-06T09:46:17Z")

</div>

Thanks for the great answer

---

<div class="post-metadata">

**Author:** ![Surya\_Narayanan](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/surya_narayanan/32/20147_2.png) [@Surya\_Narayanan](https://discuss.pytorch.org/u/Surya_Narayanan)\
**Post date:** [June 8, 2023, 6:37pm UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/10 "2023-06-08T18:37:02Z")

</div>

I have a question along these lines

I have a loss and a penalty term

so something like `loss = function(inputs) + lambda * penalty(inputs)` where function and penalty are both functions of my inputs and `lambda` is a constant

However, I notice that my loss term behaves independently of lambda, i.e. it doesn’t change the learning trajectory despite me changing lambda.

Any thoughts why?

---

<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:** [June 8, 2023, 10:54pm UTC](https://discuss.pytorch.org/t/adding-a-float-variable-as-an-additional-loss-term-works/14363/11 "2023-06-08T22:54:01Z")

</div>

Are you able to call `loss.backward()` if only `penalty(inputs)` is used?  
If not, this would indicate that `penalty` is detaching the `inputs` and might create a constant which will not influence the gradients.  
On the other hand, if `loss.backward()` still works and creates valid gradients in the parameters of the model, could you post a minimal and executable code snippet to reproduce the issue?
