# \[HELP\] FedProx loss implementation

**URL:** <https://discuss.pytorch.org/t/help-fedprox-loss-implementation/93375>\
**Category:** Uncategorized\
**Created:** [August 19, 2020, 2:21pm UTC](https://discuss.pytorch.org/t/help-fedprox-loss-implementation/93375 "2020-08-19T14:21:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefano\_Setti](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/stefano_setti/32/27968_2.png) [@Stefano\_Setti](https://discuss.pytorch.org/u/Stefano_Setti)\
**Post date:** [August 19, 2020, 2:21pm UTC](https://discuss.pytorch.org/t/help-fedprox-loss-implementation/93375/1 "2020-08-19T14:21:10Z")

</div>

Hi, so I’m trying to implement the FedProx loss from this [paper](https://arxiv.org/abs/1812.06127), my idea is to take the BCE loss and edit like this:

```auto
def fedprox_loss(output, target, new_weights, original_weights):
    rss = nn.BCELoss(output, target)
    omega = torch.sum(torch.pow(new_weights - old_weights, 2))
    return rss + omega

```

But i don’t know if this works properly and i really don’t know if I need to implement the backward method and how to do it.

---

<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 22, 2020, 4:35am UTC](https://discuss.pytorch.org/t/help-fedprox-loss-implementation/93375/2 "2020-08-22T04:35:15Z")

</div>

If all your inputs are PyTorch tensors, you won’t have to implement the backward pass manually and Autograd will do it for you.  
Are you seeing any errors or unexpected behavior?

---

<div class="post-metadata">

**Author:** ![omarfoq](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/omarfoq/32/27473_2.png) [@omarfoq](https://discuss.pytorch.org/u/omarfoq)\
**Post date:** [January 10, 2021, 8:04am UTC](https://discuss.pytorch.org/t/help-fedprox-loss-implementation/93375/3 "2021-01-10T08:04:55Z")

</div>

You forgot to multiply by mu.
