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

Fixed bug where in masked_lm transformations only subwords were candidates for top_words #417

Merged
merged 19 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,21 @@ def _get_new_words(self, current_text, indices_to_modify):
top_words = []
for _id in ranked_indices:
_id = _id.item()
token = self._lm_tokenizer.convert_ids_to_tokens(_id)
word = self._lm_tokenizer.convert_ids_to_tokens(_id)
if utils.check_if_subword(
token,
word,
self._language_model.config.model_type,
(masked_index == 1),
):
word = utils.strip_BPE_artifacts(
token, self._language_model.config.model_type
word, self._language_model.config.model_type
)
if (
mask_token_probs[_id] >= self.min_confidence
and utils.is_one_word(word)
and not utils.check_if_punctuations(word)
):
top_words.append(word)
if (
mask_token_probs[_id] >= self.min_confidence
and utils.is_one_word(word)
and not utils.check_if_punctuations(word)
):
top_words.append(word)

if (
len(top_words) >= self.max_candidates
Expand Down
18 changes: 9 additions & 9 deletions textattack/transformations/word_merges/word_merge_masked_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,21 @@ def _get_merged_words(self, current_text, indices_to_modify):
top_words = []
for _id in ranked_indices:
_id = _id.item()
token = self._lm_tokenizer.convert_ids_to_tokens(_id)
word = self._lm_tokenizer.convert_ids_to_tokens(_id)
if utils.check_if_subword(
token,
word,
self._language_model.config.model_type,
(masked_index == 1),
):
word = utils.strip_BPE_artifacts(
token, self._language_model.config.model_type
word, self._language_model.config.model_type
)
if (
mask_token_probs[_id] >= self.min_confidence
and utils.is_one_word(word)
and not utils.check_if_punctuations(word)
):
top_words.append(word)
if (
mask_token_probs[_id] >= self.min_confidence
and utils.is_one_word(word)
and not utils.check_if_punctuations(word)
):
top_words.append(word)

if (
len(top_words) >= self.max_candidates
Expand Down
18 changes: 9 additions & 9 deletions textattack/transformations/word_swaps/word_swap_masked_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,21 @@ def _bae_replacement_words(self, current_text, indices_to_modify):
top_words = []
for _id in ranked_indices:
_id = _id.item()
token = self._lm_tokenizer.convert_ids_to_tokens(_id)
word = self._lm_tokenizer.convert_ids_to_tokens(_id)
if utils.check_if_subword(
token,
word,
self._language_model.config.model_type,
(masked_index == 1),
):
word = utils.strip_BPE_artifacts(
token, self._language_model.config.model_type
word, self._language_model.config.model_type
)
if (
mask_token_probs[_id] >= self.min_confidence
and utils.is_one_word(word)
and not utils.check_if_punctuations(word)
):
top_words.append(word)
if (
mask_token_probs[_id] >= self.min_confidence
and utils.is_one_word(word)
and not utils.check_if_punctuations(word)
):
top_words.append(word)

if (
len(top_words) >= self.max_candidates
Expand Down