Skip to content

Add WER metric#30

Merged
jshin1394 merged 1 commit intogoogle:mainfrom
nikolasavic3:feat-wer
Mar 14, 2025
Merged

Add WER metric#30
jshin1394 merged 1 commit intogoogle:mainfrom
nikolasavic3:feat-wer

Conversation

@nikolasavic3
Copy link
Contributor

@nikolasavic3 nikolasavic3 commented Mar 7, 2025

Add Word Error Rate (WER) metric
Fixes #28

@nikolasavic3 nikolasavic3 changed the title Feat wer Add WER metric Mar 7, 2025
total_edit_distance = 0
total_reference_length = 0

for pred, ref in zip(predictions, references):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a TODO to use a more involved tokenizer in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great point! To keep things in line with the single responsibility principle, I think we should remove the tokenization logic entirely and just accept pre-tokenized lists of strings as input, leaving the tokenizer implementation to the one who calls the metric.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metric now accepts only lists of strings. Do you think we should accept strings and implement a tokenizer?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Nikola! I agree that single responsibility principle is important. However, considering that it is more common for these types of metrics to take in a whole sentence, (for instance, this torchMetrics WER) let's keep the implementation such that we can take in sentences as input.

@jshin1394
Copy link
Collaborator

Thank you Nikola for your PR! Left some comments. Feel free to sync the workspace as well! :)

@nikolasavic3
Copy link
Contributor Author

Thank you Jiwon for reviewing my PR!

@jshin1394
Copy link
Collaborator

Thank you Nikola! LGTM given all the comments are resolved.

@jshin1394
Copy link
Collaborator

Hi nikola, a bunch of PRs were submitted to split the metrics.py file into multiple modules. I think the WER class can be placed under the new nlp_metrics.pymodule.

@nikolasavic3 nikolasavic3 force-pushed the feat-wer branch 3 times, most recently from a0690cd to 64ea4fe Compare March 13, 2025 14:55
def total_reference_length(self):
return self.count

@classmethod
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to override this function as it already exists in Average


return distance_matrix[m][n]

def merge(self, other: 'WER') -> 'WER':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to override this function anymore as it exists in Average

count=self.count + other.count,
)

def compute(self) -> jax.Array:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to override this function as it exists in Average

total_edit_distance: Sum of edit distances across all samples.
total_reference_length: Sum of reference lengths across all samples.
"""
@property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these can be removed as well since

@jshin1394
Copy link
Collaborator

Thank you so much nikola! Almost there :) I left some comments so that we can 1) remove functions that already exist as part of Average and 2) the metric can take in sentence as input following other popular metric libraries such as torchMetrics.

Thank you again Nikola!

@nikolasavic3 nikolasavic3 force-pushed the feat-wer branch 4 times, most recently from a767587 to 24d287d Compare March 14, 2025 18:57
@nikolasavic3
Copy link
Contributor Author

Thank you for the detailed feedback, Jiwon! I've removed all the redundant function overrides (merge, compute, total_reference_length) and updated the implementation to accept untokenized strings.

@jshin1394 jshin1394 merged commit 7c875d7 into google:main Mar 14, 2025
1 check passed
@jshin1394
Copy link
Collaborator

Thank you so much Nikola! We really appreciate your contribution to the Metrax codebase! :)

@nikolasavic3 nikolasavic3 deleted the feat-wer branch March 15, 2025 09:11
@nikolasavic3
Copy link
Contributor Author

Thank you so much, Jiwon! I really appreciate your detailed feedback and guidance throughout this process. Collaborating with you on adding the WER metric to the Metrax codebase was great. Looking forward to contributing more in the future!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add WER metric

2 participants