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

refactor: wer #714

Merged
merged 5 commits into from
Jan 6, 2022
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 @@ -55,6 +55,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Renamed IoU -> Jaccard Index ([#662](https://github.com/PyTorchLightning/metrics/pull/662))

- Renamed `WER` -> `WordErrorRate` and `wer` -> `word_error_rate` ([#714](https://github.com/PyTorchLightning/metrics/pull/714))


- Renamed correlation coefficient classes: ([#710](https://github.com/PyTorchLightning/metrics/pull/710))
* `MatthewsCorrcoef` -> `MatthewsCorrCoef`
Expand Down
12 changes: 6 additions & 6 deletions tests/text/test_wer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
else:
compute_measures = Callable

from torchmetrics.functional.text.wer import wer
from torchmetrics.text.wer import WER
from torchmetrics.functional.text.wer import word_error_rate
from torchmetrics.text.wer import WordErrorRate


def _compute_wer_metric_jiwer(prediction: Union[str, List[str]], reference: Union[str, List[str]]):
Expand All @@ -36,7 +36,7 @@ def test_wer_class(self, ddp, dist_sync_on_step, preds, targets):
ddp=ddp,
preds=preds,
targets=targets,
metric_class=WER,
metric_class=WordErrorRate,
sk_metric=_compute_wer_metric_jiwer,
dist_sync_on_step=dist_sync_on_step,
)
Expand All @@ -46,7 +46,7 @@ def test_wer_functional(self, preds, targets):
self.run_functional_metric_test(
preds,
targets,
metric_functional=wer,
metric_functional=word_error_rate,
sk_metric=_compute_wer_metric_jiwer,
)

Expand All @@ -55,6 +55,6 @@ def test_wer_differentiability(self, preds, targets):
self.run_differentiability_test(
preds=preds,
targets=targets,
metric_module=WER,
metric_functional=wer,
metric_module=WordErrorRate,
metric_functional=word_error_rate,
)
2 changes: 2 additions & 0 deletions torchmetrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
SacreBLEUScore,
SQuAD,
TranslationEditRate,
WordErrorRate,
WordInfoLost,
WordInfoPreserved,
)
Expand Down Expand Up @@ -154,6 +155,7 @@
"SymmetricMeanAbsolutePercentageError",
"TranslationEditRate",
"WER",
"WordErrorRate",
"CharErrorRate",
"MatchErrorRate",
"WordInfoLost",
Expand Down
3 changes: 2 additions & 1 deletion torchmetrics/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
from torchmetrics.functional.text.sacre_bleu import sacre_bleu_score
from torchmetrics.functional.text.squad import squad
from torchmetrics.functional.text.ter import translation_edit_rate
from torchmetrics.functional.text.wer import wer
from torchmetrics.functional.text.wer import wer, word_error_rate
from torchmetrics.functional.text.wil import word_information_lost
from torchmetrics.functional.text.wip import word_information_preserved

Expand Down Expand Up @@ -140,6 +140,7 @@
"symmetric_mean_absolute_percentage_error",
"translation_edit_rate",
"wer",
"word_error_rate",
"char_error_rate",
"match_error_rate",
"word_information_lost",
Expand Down
2 changes: 1 addition & 1 deletion torchmetrics/functional/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
from torchmetrics.functional.text.sacre_bleu import sacre_bleu_score # noqa: F401
from torchmetrics.functional.text.squad import squad # noqa: F401
from torchmetrics.functional.text.ter import translation_edit_rate # noqa: F401
from torchmetrics.functional.text.wer import wer # noqa: F401
from torchmetrics.functional.text.wer import wer, word_error_rate # noqa: F401
from torchmetrics.functional.text.wil import word_information_lost # noqa: F401
from torchmetrics.functional.text.wip import word_information_preserved # noqa: F401
24 changes: 22 additions & 2 deletions torchmetrics/functional/text/wer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from typing import List, Tuple, Union
from warnings import warn

import torch
from torch import Tensor, tensor
Expand Down Expand Up @@ -61,7 +62,7 @@ def _wer_compute(errors: Tensor, total: Tensor) -> Tensor:
return errors / total


def wer(
def word_error_rate(
predictions: Union[str, List[str]],
references: Union[str, List[str]],
) -> Tensor:
Expand All @@ -79,8 +80,27 @@ def wer(
Examples:
>>> predictions = ["this is the prediction", "there is an other sample"]
>>> references = ["this is the reference", "there is another one"]
>>> wer(predictions=predictions, references=references)
>>> word_error_rate(predictions=predictions, references=references)
tensor(0.5000)
"""
errors, total = _wer_update(predictions, references)
return _wer_compute(errors, total)


def wer(
predictions: Union[str, List[str]],
references: Union[str, List[str]],
) -> Tensor:
"""Word error rate (WER_) is a common metric of the performance of an automatic speech recognition system.

.. deprecated:: v0.7
Use :func:`torchmetrics.fuctional.word_error_rate`. Will be removed in v0.8.

Examples:
>>> predictions = ["this is the prediction", "there is an other sample"]
>>> references = ["this is the reference", "there is another one"]
>>> wer(predictions=predictions, references=references)
tensor(0.5000)
"""
warn("`wer` was renamed to `word_error_rate` in v0.7 and it will be removed in v0.8", DeprecationWarning)
return word_error_rate(predictions, references)
2 changes: 1 addition & 1 deletion torchmetrics/text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
from torchmetrics.text.sacre_bleu import SacreBLEUScore # noqa: F401
from torchmetrics.text.squad import SQuAD # noqa: F401
from torchmetrics.text.ter import TranslationEditRate # noqa: F401
from torchmetrics.text.wer import WER # noqa: F401
from torchmetrics.text.wer import WER, WordErrorRate # noqa: F401
from torchmetrics.text.wil import WordInfoLost # noqa: F401
from torchmetrics.text.wip import WordInfoPreserved # noqa: F401
31 changes: 29 additions & 2 deletions torchmetrics/text/wer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Callable, List, Optional, Union
from warnings import warn

import torch
from torch import Tensor, tensor
Expand All @@ -20,7 +21,7 @@
from torchmetrics.metric import Metric


class WER(Metric):
class WordErrorRate(Metric):
r"""
Word error rate (WER_) is a common metric of the performance of an automatic speech recognition system.
This value indicates the percentage of words that were incorrectly predicted.
Expand Down Expand Up @@ -57,7 +58,7 @@ class WER(Metric):
Examples:
>>> predictions = ["this is the prediction", "there is an other sample"]
>>> references = ["this is the reference", "there is another one"]
>>> metric = WER()
>>> metric = WordErrorRate()
>>> metric(predictions, references)
tensor(0.5000)
"""
Expand Down Expand Up @@ -100,3 +101,29 @@ def compute(self) -> Tensor:
Word error rate score
"""
return _wer_compute(self.errors, self.total)


class WER(WordErrorRate):
r"""
Word error rate (WER_) is a common metric of the performance of an automatic speech recognition system.

.. deprecated:: v0.7
Use :class:`torchmetrics.WordErrorRate`. Will be removed in v0.8.

Examples:
>>> predictions = ["this is the prediction", "there is an other sample"]
>>> references = ["this is the reference", "there is another one"]
>>> metric = WER()
>>> metric(predictions, references)
tensor(0.5000)
"""

def __init__(
self,
compute_on_step: bool = True,
dist_sync_on_step: bool = False,
process_group: Optional[Any] = None,
dist_sync_fn: Callable = None,
):
warn("`WER` was renamed to `WordErrorRate` in v0.7 and it will be removed in v0.8", DeprecationWarning)
super().__init__(compute_on_step, dist_sync_on_step, process_group, dist_sync_fn)