Skip to content

Instantly share code, notes, and snippets.

@jamesr2323
Created May 9, 2018 02:40
Show Gist options
  • Save jamesr2323/33c67ba5ac29880171b63d2c7f1acdc5 to your computer and use it in GitHub Desktop.
Save jamesr2323/33c67ba5ac29880171b63d2c7f1acdc5 to your computer and use it in GitHub Desktop.
How to use RMSE loss function in PyTorch
# Thanks https://discuss.pytorch.org/t/rmse-loss-function/16540
class RMSELoss(torch.nn.Module):
def __init__(self):
super(RMSELoss,self).__init__()
def forward(self,x,y):
criterion = nn.MSELoss()
loss = torch.sqrt(criterion(x, y))
return loss
@victordeleau
Copy link

You need to add an epsilone in case of 0, as in backpropagation it will result in nans!
for example sth like this:

eps = 1e-6
loss = torch.sqrt(criterion(x, y) + eps)

That was incredibly useful thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment