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

Prune metrics: regression 8/n #6636

Merged
merged 8 commits into from
Mar 23, 2021
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 @@ -90,6 +90,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

[#6584](https://github.com/PyTorchLightning/pytorch-lightning/pull/6584),

[#6636](https://github.com/PyTorchLightning/pytorch-lightning/pull/6636),

)


Expand Down
68 changes: 6 additions & 62 deletions pytorch_lightning/metrics/functional/explained_variance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,21 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Sequence, Tuple, Union
from typing import Sequence, Union

import torch
from torchmetrics.utilities.checks import _check_same_shape
from torchmetrics.functional import explained_variance as _explained_variance


def _explained_variance_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
_check_same_shape(preds, target)
return preds, target


def _explained_variance_compute(
preds: torch.Tensor,
target: torch.Tensor,
multioutput: str = 'uniform_average',
) -> Union[torch.Tensor, Sequence[torch.Tensor]]:
diff_avg = torch.mean(target - preds, dim=0)
numerator = torch.mean((target - preds - diff_avg)**2, dim=0)

target_avg = torch.mean(target, dim=0)
denominator = torch.mean((target - target_avg)**2, dim=0)

# Take care of division by zero
nonzero_numerator = numerator != 0
nonzero_denominator = denominator != 0
valid_score = nonzero_numerator & nonzero_denominator
output_scores = torch.ones_like(diff_avg)
output_scores[valid_score] = 1.0 - (numerator[valid_score] / denominator[valid_score])
output_scores[nonzero_numerator & ~nonzero_denominator] = 0.

# Decide what to do in multioutput case
# Todo: allow user to pass in tensor with weights
if multioutput == 'raw_values':
return output_scores
if multioutput == 'uniform_average':
return torch.mean(output_scores)
if multioutput == 'variance_weighted':
denom_sum = torch.sum(denominator)
return torch.sum(denominator / denom_sum * output_scores)
from pytorch_lightning.utilities.deprecation import deprecated


@deprecated(target=_explained_variance, ver_deprecate="1.3.0", ver_remove="1.5.0")
def explained_variance(
preds: torch.Tensor,
target: torch.Tensor,
multioutput: str = 'uniform_average',
) -> Union[torch.Tensor, Sequence[torch.Tensor]]:
"""
Computes explained variance.

Args:
preds: estimated labels
target: ground truth labels
multioutput: Defines aggregation in the case of multiple output scores. Can be one
of the following strings (default is `'uniform_average'`.):

* `'raw_values'` returns full set of scores
* `'uniform_average'` scores are uniformly averaged
* `'variance_weighted'` scores are weighted by their individual variances

Example:

>>> from pytorch_lightning.metrics.functional import explained_variance
>>> target = torch.tensor([3, -0.5, 2, 7])
>>> preds = torch.tensor([2.5, 0.0, 2, 8])
>>> explained_variance(preds, target)
tensor(0.9572)

>>> target = torch.tensor([[0.5, 1], [-1, 1], [7, -6]])
>>> preds = torch.tensor([[0, 2], [-1, 2], [8, -5]])
>>> explained_variance(preds, target, multioutput='raw_values')
tensor([0.9677, 1.0000])
.. deprecated::
Use :func:`torchmetrics.functional.explained_variance`. Will be removed in v1.5.0.
"""
preds, target = _explained_variance_update(preds, target)
return _explained_variance_compute(preds, target, multioutput)
34 changes: 5 additions & 29 deletions pytorch_lightning/metrics/functional/mean_absolute_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple

import torch
from torchmetrics.utilities.checks import _check_same_shape
from torchmetrics.functional import mean_absolute_error as _mean_absolute_error


def _mean_absolute_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:
_check_same_shape(preds, target)
sum_abs_error = torch.sum(torch.abs(preds - target))
n_obs = target.numel()
return sum_abs_error, n_obs


def _mean_absolute_error_compute(sum_abs_error: torch.Tensor, n_obs: int) -> torch.Tensor:
return sum_abs_error / n_obs
from pytorch_lightning.utilities.deprecation import deprecated


@deprecated(target=_mean_absolute_error, ver_deprecate="1.3.0", ver_remove="1.5.0")
def mean_absolute_error(preds: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
Computes mean absolute error

Args:
pred: estimated labels
target: ground truth labels

Return:
Tensor with MAE

Example:
>>> from pytorch_lightning.metrics.functional import mean_absolute_error
>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> mean_absolute_error(x, y)
tensor(0.2500)
.. deprecated::
Use :func:`torchmetrics.functional.mean_absolute_error`. Will be removed in v1.5.0.
"""
sum_abs_error, n_obs = _mean_absolute_error_update(preds, target)
return _mean_absolute_error_compute(sum_abs_error, n_obs)
37 changes: 5 additions & 32 deletions pytorch_lightning/metrics/functional/mean_relative_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple

import torch
from torchmetrics.utilities.checks import _check_same_shape
from torchmetrics.functional.regression.mean_relative_error import mean_relative_error as _mean_relative_error


def _mean_relative_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:
_check_same_shape(preds, target)
target_nz = target.clone()
target_nz[target == 0] = 1
sum_rltv_error = torch.sum(torch.abs((preds - target) / target_nz))
n_obs = target.numel()
return sum_rltv_error, n_obs


def _mean_relative_error_compute(sum_rltv_error: torch.Tensor, n_obs: int) -> torch.Tensor:
return sum_rltv_error / n_obs
from pytorch_lightning.utilities.deprecation import deprecated


@deprecated(target=_mean_relative_error, ver_deprecate="1.3.0", ver_remove="1.5.0")
def mean_relative_error(preds: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
Computes mean relative error

Args:
pred: estimated labels
target: ground truth labels

Return:
Tensor with mean relative error

Example:

>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> mean_relative_error(x, y)
tensor(0.1250)

.. deprecated::
Use :func:`torchmetrics.functional.regression.mean_relative_error`. Will be removed in v1.5.0.
"""
sum_rltv_error, n_obs = _mean_relative_error_update(preds, target)
return _mean_relative_error_compute(sum_rltv_error, n_obs)
34 changes: 5 additions & 29 deletions pytorch_lightning/metrics/functional/mean_squared_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple

import torch
from torchmetrics.utilities.checks import _check_same_shape
from torchmetrics.functional import mean_squared_error as _mean_squared_error


def _mean_squared_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:
_check_same_shape(preds, target)
sum_squared_error = torch.sum(torch.pow(preds - target, 2))
n_obs = target.numel()
return sum_squared_error, n_obs


def _mean_squared_error_compute(sum_squared_error: torch.Tensor, n_obs: int) -> torch.Tensor:
return sum_squared_error / n_obs
from pytorch_lightning.utilities.deprecation import deprecated


@deprecated(target=_mean_squared_error, ver_deprecate="1.3.0", ver_remove="1.5.0")
def mean_squared_error(preds: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
Computes mean squared error

Args:
preds: estimated labels
target: ground truth labels

Return:
Tensor with MSE

Example:
>>> from pytorch_lightning.metrics.functional import mean_squared_error
>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> mean_squared_error(x, y)
tensor(0.2500)
.. deprecated::
Use :func:`torchmetrics.functional.mean_squared_error`. Will be removed in v1.5.0.
"""
sum_squared_error, n_obs = _mean_squared_error_update(preds, target)
return _mean_squared_error_compute(sum_squared_error, n_obs)
34 changes: 5 additions & 29 deletions pytorch_lightning/metrics/functional/mean_squared_log_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,16 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Tuple

import torch
from torchmetrics.utilities.checks import _check_same_shape
from torchmetrics.functional import mean_squared_log_error as _mean_squared_log_error


def _mean_squared_log_error_update(preds: torch.Tensor, target: torch.Tensor) -> Tuple[torch.Tensor, int]:
_check_same_shape(preds, target)
sum_squared_log_error = torch.sum(torch.pow(torch.log1p(preds) - torch.log1p(target), 2))
n_obs = target.numel()
return sum_squared_log_error, n_obs


def _mean_squared_log_error_compute(sum_squared_log_error: torch.Tensor, n_obs: int) -> torch.Tensor:
return sum_squared_log_error / n_obs
from pytorch_lightning.utilities.deprecation import deprecated


@deprecated(target=_mean_squared_log_error, ver_deprecate="1.3.0", ver_remove="1.5.0")
def mean_squared_log_error(preds: torch.Tensor, target: torch.Tensor) -> torch.Tensor:
"""
Computes mean squared log error

Args:
preds: estimated labels
target: ground truth labels

Return:
Tensor with RMSLE

Example:
>>> from pytorch_lightning.metrics.functional import mean_squared_log_error
>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> mean_squared_log_error(x, y)
tensor(0.0207)
.. deprecated::
Use :func:`torchmetrics.functional.mean_squared_log_error`. Will be removed in v1.5.0.
"""
sum_squared_log_error, n_obs = _mean_squared_log_error_update(preds, target)
return _mean_squared_log_error_compute(sum_squared_log_error, n_obs)
Loading