Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RMSLE metric #3188

Merged
merged 6 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Fixed `num_sanity_val_steps` is clipped to `limit_val_batches` ([#2917](https://github.com/PyTorchLightning/pytorch-lightning/pull/2917))

- Fixed RMSLE metric ([#3188](https://github.com/PyTorchLightning/pytorch-lightning/pull/3188))

## [0.9.0] - YYYY-MM-DD

### Added
Expand Down
4 changes: 2 additions & 2 deletions pytorch_lightning/metrics/functional/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@ def rmsle(
>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> rmsle(x, y)
tensor(0.0207)
tensor(0.1438)

"""
rmsle = mse(torch.log(pred + 1), torch.log(target + 1), reduction=reduction)
rmsle = rmse(torch.log(pred + 1), torch.log(target + 1), reduction=reduction)
return rmsle


Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/metrics/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class RMSLE(Metric):
>>> target = torch.tensor([0., 1, 2, 2])
>>> metric = RMSLE()
>>> metric(pred, target)
tensor(0.0207)
tensor(0.1438)

"""

Expand Down
38 changes: 34 additions & 4 deletions tests/metrics/functional/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import numpy as np
import pytest
import torch
from skimage.metrics import peak_signal_noise_ratio as ski_psnr
from skimage.metrics import structural_similarity as ski_ssim
from functools import partial
from math import sqrt
from skimage.metrics import (
peak_signal_noise_ratio as ski_psnr,
structural_similarity as ski_ssim
)
from sklearn.metrics import (
mean_absolute_error as mae_sk,
mean_squared_error as mse_sk,
mean_squared_log_error as msle_sk
)

from pytorch_lightning.metrics.functional import (
mae,
Expand All @@ -14,6 +23,27 @@
)


@pytest.mark.parametrize(['sklearn_metric', 'torch_metric'], [
pytest.param(mae_sk, mae, id='mean_absolute_error'),
pytest.param(mse_sk, mse, id='mean_squared_error'),
pytest.param(partial(mse_sk, squared=False), rmse, id='root_mean_squared_error'),
pytest.param(lambda x, y: sqrt(msle_sk(x, y)), rmsle, id='root_mean_squared_log_error')
])
def test_against_sklearn(sklearn_metric, torch_metric):
"""Compare PL metrics to sklearn version."""
device = 'cuda' if torch.cuda.is_available() else 'cpu'

# iterate over different label counts in predictions and target
pred = torch.rand(300, device=device)
target = torch.rand(300, device=device)

sk_score = sklearn_metric(target.cpu().detach().numpy(),
pred.cpu().detach().numpy())
sk_score = torch.tensor(sk_score, dtype=torch.float, device=device)
pl_score = torch_metric(pred, target)
assert torch.allclose(sk_score, pl_score)


@pytest.mark.parametrize(['pred', 'target', 'expected'], [
pytest.param([0., 1, 2, 3], [0., 1, 2, 2], 0.25),
pytest.param([4., 3, 2, 1], [1., 4, 3, 2], 3.0),
Expand Down Expand Up @@ -45,8 +75,8 @@ def test_mae(pred, target, expected):

@pytest.mark.parametrize(['pred', 'target', 'expected'], [
pytest.param([0., 1, 2, 3], [0., 1, 2, 3], 0.0),
pytest.param([0., 1, 2, 3], [0., 1, 2, 2], 0.0207),
pytest.param([4., 3, 2, 1], [1., 4, 3, 2], 0.2841),
pytest.param([0., 1, 2, 3], [0., 1, 2, 2], 0.1438),
pytest.param([4., 3, 2, 1], [1., 4, 3, 2], 0.5330),
])
def test_rmsle(pred, target, expected):
score = rmsle(torch.tensor(pred), torch.tensor(target))
Expand Down