|
| 1 | +# Copyright The PyTorch Lightning team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import Tuple |
| 15 | + |
| 16 | +import torch |
| 17 | +from torch import Tensor |
| 18 | + |
| 19 | +from torchmetrics.functional.regression.utils import _check_data_shape_to_num_outputs |
| 20 | +from torchmetrics.utilities.checks import _check_same_shape |
| 21 | + |
| 22 | + |
| 23 | +def _unsqueeze_tensors(preds: Tensor, target: Tensor) -> Tuple[Tensor, Tensor]: |
| 24 | + if preds.ndim == 2: |
| 25 | + return preds, target |
| 26 | + return preds.unsqueeze(1), target.unsqueeze(1) |
| 27 | + |
| 28 | + |
| 29 | +def _log_cosh_error_update(preds: Tensor, target: Tensor, num_outputs: int) -> Tuple[Tensor, Tensor]: |
| 30 | + """Updates and returns variables required to compute LogCosh error. |
| 31 | +
|
| 32 | + Checks for same shape of input tensors. |
| 33 | +
|
| 34 | + Args: |
| 35 | + preds: Predicted tensor |
| 36 | + target: Ground truth tensor |
| 37 | +
|
| 38 | + Return: |
| 39 | + Sum of LogCosh error over examples, and total number of examples |
| 40 | + """ |
| 41 | + _check_same_shape(preds, target) |
| 42 | + _check_data_shape_to_num_outputs(preds, target, num_outputs) |
| 43 | + |
| 44 | + preds, target = _unsqueeze_tensors(preds, target) |
| 45 | + diff = preds - target |
| 46 | + sum_log_cosh_error = torch.log((torch.exp(diff) + torch.exp(-diff)) / 2).sum(0).squeeze() |
| 47 | + n_obs = torch.tensor(target.shape[0], device=preds.device) |
| 48 | + return sum_log_cosh_error, n_obs |
| 49 | + |
| 50 | + |
| 51 | +def _log_cosh_error_compute(sum_log_cosh_error: Tensor, n_obs: Tensor) -> Tensor: |
| 52 | + """Computes Mean Squared Error. |
| 53 | +
|
| 54 | + Args: |
| 55 | + sum_squared_error: Sum of LogCosh errors over all observations |
| 56 | + n_obs: Number of predictions or observations |
| 57 | + """ |
| 58 | + return (sum_log_cosh_error / n_obs).squeeze() |
| 59 | + |
| 60 | + |
| 61 | +def log_cosh_error(preds: Tensor, target: Tensor) -> Tensor: |
| 62 | + r"""Compute the `LogCosh Error`_. |
| 63 | +
|
| 64 | + .. math:: \text{LogCoshError} = \log\left(\frac{\exp(\hat{y} - y) + \exp(\hat{y - y})}{2}\right) |
| 65 | +
|
| 66 | + Where :math:`y` is a tensor of target values, and :math:`\hat{y}` is a tensor of predictions. |
| 67 | +
|
| 68 | + Args: |
| 69 | + preds: estimated labels with shape ``(batch_size,)`` or `(batch_size, num_outputs)`` |
| 70 | + target: ground truth labels with shape ``(batch_size,)`` or `(batch_size, num_outputs)`` |
| 71 | +
|
| 72 | + Return: |
| 73 | + Tensor with LogCosh error |
| 74 | +
|
| 75 | + Example (single output regression):: |
| 76 | + >>> from torchmetrics.functional import log_cosh_error |
| 77 | + >>> preds = torch.tensor([3.0, 5.0, 2.5, 7.0]) |
| 78 | + >>> target = torch.tensor([2.5, 5.0, 4.0, 8.0]) |
| 79 | + >>> log_cosh_error(preds, target) |
| 80 | + tensor(0.3523) |
| 81 | +
|
| 82 | + Example (multi output regression):: |
| 83 | + >>> from torchmetrics.functional import log_cosh_error |
| 84 | + >>> preds = torch.tensor([[3.0, 5.0, 1.2], [-2.1, 2.5, 7.0]]) |
| 85 | + >>> target = torch.tensor([[2.5, 5.0, 1.3], [0.3, 4.0, 8.0]]) |
| 86 | + >>> log_cosh_error(preds, target) |
| 87 | + tensor([0.9176, 0.4277, 0.2194]) |
| 88 | + """ |
| 89 | + sum_log_cosh_error, n_obs = _log_cosh_error_update( |
| 90 | + preds, target, num_outputs=1 if preds.ndim == 1 else preds.shape[-1] |
| 91 | + ) |
| 92 | + return _log_cosh_error_compute(sum_log_cosh_error, n_obs) |
0 commit comments