This repository was archived by the owner on Sep 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 814
add max_tokens kwarg to vocab factory. #1525
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
86bee50
add max_tokens kwarg to vocab and vocab factory.
erip e41074b
fix flake.
erip dc2119f
make docstring more clear about the interaction between special and n…
erip 4123122
remove max_tokens from vocab and force builders to handle that logic.
erip 457747f
revert potential breakage where lexicographical sorting is done. Simp…
erip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ def vocab(ordered_dict: Dict, min_freq: int = 1, | |
| ordered_dict.pop(token, None) | ||
|
|
||
| tokens = [] | ||
| # Save room for special tokens | ||
| for token, freq in ordered_dict.items(): | ||
| if freq >= min_freq: | ||
| tokens.append(token) | ||
|
|
@@ -61,7 +62,7 @@ def vocab(ordered_dict: Dict, min_freq: int = 1, | |
| return Vocab(VocabPybind(tokens, None)) | ||
|
|
||
|
|
||
| def build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: Optional[List[str]] = None, special_first: bool = True) -> Vocab: | ||
| def build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: Optional[List[str]] = None, special_first: bool = True, max_tokens: Optional[int] = None) -> Vocab: | ||
| """ | ||
| Build a Vocab from an iterator. | ||
|
|
||
|
|
@@ -70,6 +71,7 @@ def build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: O | |
| min_freq: The minimum frequency needed to include a token in the vocabulary. | ||
| specials: Special symbols to add. The order of supplied tokens will be preserved. | ||
| special_first: Indicates whether to insert symbols at the beginning or at the end. | ||
| max_tokens: If provided, creates the vocab from the `max_tokens - len(specials)` most frequent tokens. | ||
|
|
||
|
|
||
| Returns: | ||
|
|
@@ -90,10 +92,16 @@ def build_vocab_from_iterator(iterator: Iterable, min_freq: int = 1, specials: O | |
| for tokens in iterator: | ||
| counter.update(tokens) | ||
|
|
||
| sorted_by_freq_tuples = sorted(counter.items(), key=lambda x: x[0]) | ||
| sorted_by_freq_tuples.sort(key=lambda x: x[1], reverse=True) | ||
| ordered_dict = OrderedDict(sorted_by_freq_tuples) | ||
| specials = specials or [] | ||
|
|
||
| # First sort by descending frequency, then lexicographically | ||
| sorted_by_freq_tuples = sorted(counter.items(), key=lambda x: (-x[1], x[0])) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, so you combined the two separate sorting operations in 1 :). |
||
|
|
||
| if max_tokens is None: | ||
| ordered_dict = OrderedDict(sorted_by_freq_tuples) | ||
| else: | ||
| assert len(specials) < max_tokens, "len(specials) >= max_tokens, so the vocab will be entirely special tokens." | ||
| ordered_dict = OrderedDict(sorted_by_freq_tuples[:max_tokens - len(specials)]) | ||
|
|
||
| word_vocab = vocab(ordered_dict, min_freq=min_freq, specials=specials or [], | ||
| special_first=special_first) | ||
| word_vocab = vocab(ordered_dict, min_freq=min_freq, specials=specials, special_first=special_first) | ||
| return word_vocab | ||
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.
Uh oh!
There was an error while loading. Please reload this page.