RMSLE loss function

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 ?

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

class RMSLELoss(nn.Module):
    def __init__(self):
        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.

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

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

criterion = RMSLELoss()

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

I hope this helps.


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

1 Like