# Intentional constant loss

**URL:** <https://discuss.pytorch.org/t/intentional-constant-loss/29837>\
**Category:** Uncategorized\
**Created:** [November 17, 2018, 11:49am UTC](https://discuss.pytorch.org/t/intentional-constant-loss/29837 "2018-11-17T11:49:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gt\_tugsuu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gt_tugsuu/32/11678_2.png) [@gt\_tugsuu](https://discuss.pytorch.org/u/gt_tugsuu)\
**Post date:** [November 17, 2018, 11:49am UTC](https://discuss.pytorch.org/t/intentional-constant-loss/29837/1 "2018-11-17T11:49:25Z")

</div>

Is there a way to fix the training loss to a certain value?

---

<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 17, 2018, 12:39pm UTC](https://discuss.pytorch.org/t/intentional-constant-loss/29837/2 "2018-11-17T12:39:28Z")

</div>

Do you mean at the beginning of the training or during the training?  
Could you explain your use case as I’m not sure what you’re trying to do exactly.

---

<div class="post-metadata">

**Author:** ![gt\_tugsuu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gt_tugsuu/32/11678_2.png) [@gt\_tugsuu](https://discuss.pytorch.org/u/gt_tugsuu)\
**Post date:** [November 18, 2018, 2:41pm UTC](https://discuss.pytorch.org/t/intentional-constant-loss/29837/3 "2018-11-18T14:41:20Z")

</div>

Umm, something like this during training:

```auto
...
loss = 6000
loss.backward()
...

```

---

<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 18, 2018, 8:35pm UTC](https://discuss.pytorch.org/t/intentional-constant-loss/29837/4 "2018-11-18T20:35:47Z")

</div>

That’s not really possible, as in your code the loss is completely detached from any other operation, besides not being a `torch.tensor`.

I’m not sure, if that’s what you are looking for, but you could try to scale your loss to a specific value:

```python
x = torch.randn(1, 1)
w = torch.randn(1, 1, requires_grad=True)
output = x * w
target = torch.randn(1, 1)
loss = (output - target)**2
factor = 6000. / loss.item()
loss = loss * factor
loss.backward()
print(w.grad)

```

---

<div class="post-metadata">

**Author:** ![gt\_tugsuu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/gt_tugsuu/32/11678_2.png) [@gt\_tugsuu](https://discuss.pytorch.org/u/gt_tugsuu)\
**Post date:** [November 19, 2018, 6:13am UTC](https://discuss.pytorch.org/t/intentional-constant-loss/29837/5 "2018-11-19T06:13:55Z")

</div>

Yes, that’s what i was looking for. I didn’t know how to attach a specific value as loss.
