ValueError: high is out of bounds for int64 (word_swap_change_number._alter_number)
Describe the bug
When using textattack.augmentation.recipes.CheckListAugmenter on the yahoo_answers_topics dataset in Huggingface, I get an error:
...
File "~\textattack\transformations\word_swaps\word_swap_change_number.py", line 108, in _alter_number
num_list = np.random.randint(max(num - change, 1), num + change, self.n, dtype=np.int64)
File "mtrand.pyx", line 765, in numpy.random.mtrand.RandomState.randint
File "_bounded_integers.pyx", line 1245, in numpy.random._bounded_integers._rand_int64
ValueError: high is out of bounds for int64
Whatever number is being found in the text is too large for np.int32. A partial workaround that solved the problem in ag_news was to change change the dtype=np.int64, but its still happening now for yahoo_answers_topics.
To Reproduce
class AugMapper:
def __init__(self, augmenter):
self.augmenter = augmenter # textattack augmenter recipe
def apply_to_batch(self, batch):
new_texts, new_labels = [], []
for text, label in zip(batch['text'], batch['label']):
augmented_text = self.augmenter.augment(text)
new_texts.extend(augmented_text)
new_labels.extend([label] * len(augmented_text))
return {
"text": new_texts,
"label": new_labels,
"idx": list(range(len(new_labels))),
}
if __name__ == "__main__":
import os
import glob
from datasets import load_dataset, load_from_disk
from textattack.augmentation.recipes import (
EasyDataAugmenter,
CheckListAugmenter,
)
# checklist
checklist_augmenter = CheckListAugmenter(
transformations_per_example=3
)
checklist_aug_mapper = AugMapper(checklist_augmenter)
dataset_paths = ["yahoo_answers_topics"]
for dataset_path in dataset_paths:
print(dataset_path)
dataset = load_dataset(dataset_path, split="train")
# augment + save checklist
checklist_dataset = dataset.map(checklist_aug_mapper.apply_to_batch, batched=True, batch_size=10)
Expected behavior The augmentation should not error out
Screenshots or Traceback If applicable, add screenshots to help explain your problem. Also, copy and paste tracebacks produced by the bug.
System Information (please complete the following information):
- OS: Windows
- Textattack version: 0.3.8
Additional context Potential fix that is working so far --> cap the values to the max that numpy can handle for int64
def _alter_number(self, num):
"""helper function of _get_new_number, replace a number with another
random number within the range of self.max_change."""
if num not in [0, 2, 4]:
change = int(num * self.max_change) + 1
if num >= 0:
num_list = np.random.randint(
low=max(num - change, 1),
high=min(num + change, np.iinfo(np.int64).max - 1),
size=self.n,
dtype=np.int64)
else:
num_list = np.random.randint(
low=max(num - change, np.iinfo(np.int64).min + 1),
high=min(0, num + change),
size=self.n,
dtype=np.int64)
return num_list
return []