# RMSLE loss function

**URL:** <https://discuss.pytorch.org/t/rmsle-loss-function/67281>\
**Category:** Uncategorized\
**Created:** [January 21, 2020, 3:39pm UTC](https://discuss.pytorch.org/t/rmsle-loss-function/67281 "2020-01-21T15:39:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![soadrhcp](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@soadrhcp](https://discuss.pytorch.org/u/soadrhcp)\
**Post date:** [January 21, 2020, 3:39pm UTC](https://discuss.pytorch.org/t/rmsle-loss-function/67281/1 "2020-01-21T15:39:21Z")

</div>

Hello world,

I’m planning to use the Root Means Squared Log Error as a loss function for an image to image regression problem (these are not properly images but fields with variable norms). I did not find this function as part of the torch.nn.modules.loss implemented functions. What do you think would be the best way to implement Root Means Squared Log Error with PyTorch ?

---

<div class="post-metadata">

**Author:** ![Pranaymodukuru](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pranaymodukuru/32/14545_2.png) [@Pranaymodukuru](https://discuss.pytorch.org/u/Pranaymodukuru)\
**Post date:** [April 7, 2020, 8:07am UTC](https://discuss.pytorch.org/t/rmsle-loss-function/67281/2 "2020-04-07T08:07:28Z")

</div>

One way of doing this is to create a `nn.Module` class,

```auto
class RMSLELoss(nn.Module):
    def __init__ (self):
        super(). __init__ ()
        self.mse = nn.MSELoss()
        
    def forward(self, pred, actual):
        return torch.sqrt(self.mse(torch.log(pred + 1), torch.log(actual + 1)))

```

The equation I took as reference here is from a [Kaggle discussion](https://www.kaggle.com/c/ashrae-energy-prediction/discussion/113064).

and using it just like any other loss function provided by pytorch.

```auto
pred = torch.tensor([600.], requires_grad=True)
actual = torch.tensor([1000.])

criterion = RMSLELoss()

rmsle = criterion(pred, actual)
print(rmsle)
# tensor(0.5102, grad_fn=<SqrtBackward>)

```

I hope this helps.

---

<div class="post-metadata">

**Author:** ![soadrhcp](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/ed8c4c/32.png) [@soadrhcp](https://discuss.pytorch.org/u/soadrhcp)\
**Post date:** [April 9, 2020, 7:59am UTC](https://discuss.pytorch.org/t/rmsle-loss-function/67281/3 "2020-04-09T07:59:59Z")

</div>

Thanks mate, indeed this is a direct way to do so.
