Add get_sequence_transform to Accuracy for seq2seq models#3728
Open
joemunene-by wants to merge 1 commit intopytorch:masterfrom
Open
Add get_sequence_transform to Accuracy for seq2seq models#3728joemunene-by wants to merge 1 commit intopytorch:masterfrom
joemunene-by wants to merge 1 commit intopytorch:masterfrom
Conversation
Add a static method `Accuracy.get_sequence_transform(pad_index)` that returns an output_transform for sequence-to-sequence models. Handles flattening (batch, seq_len, num_classes) tensors and masking padding tokens, eliminating the need for custom boilerplate in NLP/Transformer training loops. Closes pytorch#3593
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
Accuracy.get_sequence_transform(pad_index, output_transform)— a static method that returns a pre-built output transform for sequence-to-sequence models, addressing the common NLP/Transformer boilerplate discussed in #3593.Before (manual boilerplate every project):
After (one-liner):
What it does
(batch, seq_len, num_classes)predictions →(batch * seq_len, num_classes)(batch, seq_len)targets →(batch * seq_len,)target == pad_indexoutput_transformcomposition (e.g., extracting from dicts)Design
Follows @vfdev-5's suggestion in #3593 to use a static method returning a transform rather than creating a new metric class. This keeps the API clean and avoids duplication.
Tests
6 new tests covering:
All existing tests pass (140 passed, 292 skipped for CUDA/MPS).
Closes #3593