diff --git a/docs/2notebook/0_End_to_End.ipynb b/docs/2notebook/0_End_to_End.ipynb index d4388e156..d42d01405 100644 --- a/docs/2notebook/0_End_to_End.ipynb +++ b/docs/2notebook/0_End_to_End.ipynb @@ -1430,7 +1430,7 @@ "metadata": {}, "outputs": [], "source": [ - "!textattack attack --model cnn-yelp --num-examples 3 --search-method greedy-word-wir --transformation word-swap-wordnet --constraints cola^max_diff=0.1 bert-score^min_bert_score=0.7 --enable-advance-metrics \n" + "!textattack attack --model cnn-yelp --num-examples 3 --search-method greedy-word-wir --transformation word-swap-wordnet --constraints cola^max_diff=0.1 bert-score^min_bert_score=0.7 --enable-advance-metrics" ] }, { @@ -1439,7 +1439,7 @@ "metadata": {}, "outputs": [], "source": [ - "!textattack attack --model lstm-mr --recipe deepwordbug --num-examples 2 --attack-n --enable-advance-metrics \n" + "!textattack attack --model lstm-mr --recipe deepwordbug --num-examples 2 --attack-n --enable-advance-metrics" ] }, { @@ -1448,7 +1448,7 @@ "metadata": {}, "outputs": [], "source": [ - "!textattack attack --model lstm-mr --recipe hotflip --num-examples 4 --num-examples-offset 3 --enable-advance-metrics " + "!textattack attack --model lstm-mr --recipe hotflip --num-examples 4 --num-examples-offset 3 --enable-advance-metrics" ] }, { @@ -1457,7 +1457,7 @@ "metadata": {}, "outputs": [], "source": [ - "!textattack attack --model-from-huggingface distilbert-base-uncased-finetuned-sst-2-english --dataset-from-huggingface glue^sst2^train --recipe deepwordbug --num-examples 3 --enable-advance-metrics\n" + "!textattack attack --model-from-huggingface distilbert-base-uncased-finetuned-sst-2-english --dataset-from-huggingface glue^sst2^train --recipe deepwordbug --num-examples 3 --enable-advance-metrics" ] }, { @@ -1466,7 +1466,7 @@ "metadata": {}, "outputs": [], "source": [ - "! textattack attack --model cnn-imdb --attack-from-file tests/sample_inputs/attack_from_file.py^Attack --num-examples 2 --num-examples-offset 18 --attack-n " + "! textattack attack --model cnn-imdb --attack-from-file tests/sample_inputs/attack_from_file.py^Attack --num-examples 2 --num-examples-offset 18 --attack-n" ] } ], diff --git a/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb b/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb index b9639346f..0afbb0707 100644 --- a/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb +++ b/docs/2notebook/4_Custom_Datasets_Word_Embedding.ipynb @@ -537,7 +537,7 @@ "\n", "attack = Attack(goal_function, constraints, transformation, search_method)\n", "\n", - "# here is a legacy code piece showing how the attack runs in details \n", + "# here is a legacy code piece showing how the attack runs in details\n", "for example, label in custom_dataset:\n", " result = attack.attack(example, label)\n", " print(result.__str__(color_method=\"ansi\"))" diff --git a/docs/2notebook/Example_3_Keras.ipynb b/docs/2notebook/Example_3_Keras.ipynb index af5a4e709..25bb73a9d 100644 --- a/docs/2notebook/Example_3_Keras.ipynb +++ b/docs/2notebook/Example_3_Keras.ipynb @@ -259,7 +259,6 @@ " self.model = model\n", "\n", " def __call__(self, text_input_list):\n", - "\n", " x_transform = []\n", " for i, review in enumerate(text_input_list):\n", " tokens = [x.strip(\",\") for x in review.split()]\n", diff --git a/docs/2notebook/Example_5_Explain_BERT.ipynb b/docs/2notebook/Example_5_Explain_BERT.ipynb index a6316fdca..2e2110778 100644 --- a/docs/2notebook/Example_5_Explain_BERT.ipynb +++ b/docs/2notebook/Example_5_Explain_BERT.ipynb @@ -273,19 +273,9 @@ " list_of_text = []\n", " number = input_ids.size()[0]\n", " for i in range(number):\n", - " ii = (\n", - " input_ids[\n", - " i,\n", - " ]\n", - " .cpu()\n", - " .numpy()\n", - " )\n", - " tt = token_type_ids[\n", - " i,\n", - " ]\n", - " am = attention_mask[\n", - " i,\n", - " ]\n", + " ii = input_ids[i,].cpu().numpy()\n", + " tt = token_type_ids[i,]\n", + " am = attention_mask[i,]\n", " txt = tokenizer.decode(ii, skip_special_tokens=True)\n", " list_of_text.append(txt)\n", " return list_of_text\n", diff --git a/tests/test_attacked_text.py b/tests/test_attacked_text.py index 50bdf86b4..6aff12fbc 100644 --- a/tests/test_attacked_text.py +++ b/tests/test_attacked_text.py @@ -70,7 +70,7 @@ def test_window_around_index(self, attacked_text): def test_big_window_around_index(self, attacked_text): assert ( - attacked_text.text_window_around_index(0, 10 ** 5) + "." + attacked_text.text_window_around_index(0, 10**5) + "." ) == attacked_text.text def test_window_around_index_start(self, attacked_text): diff --git a/tests/test_word_embedding.py b/tests/test_word_embedding.py index 5232e8fa1..4772c27dd 100644 --- a/tests/test_word_embedding.py +++ b/tests/test_word_embedding.py @@ -10,7 +10,7 @@ def test_embedding_paragramcf(): word_embedding = WordEmbedding.counterfitted_GLOVE_embedding() assert pytest.approx(word_embedding[0][0]) == -0.022007 assert pytest.approx(word_embedding["fawn"][0]) == -0.022007 - assert word_embedding[10 ** 9] is None + assert word_embedding[10**9] is None def test_embedding_gensim(): @@ -37,7 +37,7 @@ def test_embedding_gensim(): word_embedding = GensimWordEmbedding(keyed_vectors) assert pytest.approx(word_embedding[0][0]) == 1 assert pytest.approx(word_embedding["bye-bye"][0]) == -1 / np.sqrt(2) - assert word_embedding[10 ** 9] is None + assert word_embedding[10**9] is None # test query functionality assert pytest.approx(word_embedding.get_cos_sim(1, 3)) == 0 diff --git a/textattack/attack.py b/textattack/attack.py index 7743817ab..47537d1b0 100644 --- a/textattack/attack.py +++ b/textattack/attack.py @@ -83,8 +83,8 @@ def __init__( constraints: List[Union[Constraint, PreTransformationConstraint]], transformation: Transformation, search_method: SearchMethod, - transformation_cache_size=2 ** 15, - constraint_cache_size=2 ** 15, + transformation_cache_size=2**15, + constraint_cache_size=2**15, ): """Initialize an attack object. diff --git a/textattack/attack_args.py b/textattack/attack_args.py index 495e1006b..0491ea17e 100644 --- a/textattack/attack_args.py +++ b/textattack/attack_args.py @@ -504,8 +504,8 @@ class _CommandLineAttackArgs: interactive: bool = False parallel: bool = False model_batch_size: int = 32 - model_cache_size: int = 2 ** 18 - constraint_cache_size: int = 2 ** 18 + model_cache_size: int = 2**18 + constraint_cache_size: int = 2**18 @classmethod def _add_parser_args(cls, parser): diff --git a/textattack/constraints/grammaticality/cola.py b/textattack/constraints/grammaticality/cola.py index beb7c30a6..190bad25c 100644 --- a/textattack/constraints/grammaticality/cola.py +++ b/textattack/constraints/grammaticality/cola.py @@ -43,7 +43,7 @@ def __init__( self.max_diff = max_diff self.model_name = model_name - self._reference_score_cache = lru.LRU(2 ** 10) + self._reference_score_cache = lru.LRU(2**10) model = AutoModelForSequenceClassification.from_pretrained(model_name) tokenizer = AutoTokenizer.from_pretrained(model_name) self.model = HuggingFaceModelWrapper(model, tokenizer) diff --git a/textattack/constraints/grammaticality/language_models/google_language_model/alzantot_goog_lm.py b/textattack/constraints/grammaticality/language_models/google_language_model/alzantot_goog_lm.py index d47bfd6ec..005dda55e 100644 --- a/textattack/constraints/grammaticality/language_models/google_language_model/alzantot_goog_lm.py +++ b/textattack/constraints/grammaticality/language_models/google_language_model/alzantot_goog_lm.py @@ -49,7 +49,7 @@ def __init__(self): self.sess, self.graph, self.PBTXT_PATH, self.CKPT_PATH ) - self.lm_cache = lru.LRU(2 ** 18) + self.lm_cache = lru.LRU(2**18) def clear_cache(self): self.lm_cache.clear() diff --git a/textattack/constraints/grammaticality/part_of_speech.py b/textattack/constraints/grammaticality/part_of_speech.py index e686efdb1..f531f33c7 100644 --- a/textattack/constraints/grammaticality/part_of_speech.py +++ b/textattack/constraints/grammaticality/part_of_speech.py @@ -56,7 +56,7 @@ def __init__( self.language_nltk = language_nltk self.language_stanza = language_stanza - self._pos_tag_cache = lru.LRU(2 ** 14) + self._pos_tag_cache = lru.LRU(2**14) if tagger_type == "flair": if tagset == "universal": self._flair_pos_tagger = SequenceTagger.load("upos-fast") diff --git a/textattack/constraints/semantics/sentence_encoders/thought_vector.py b/textattack/constraints/semantics/sentence_encoders/thought_vector.py index 60bac23ba..4a7978b01 100644 --- a/textattack/constraints/semantics/sentence_encoders/thought_vector.py +++ b/textattack/constraints/semantics/sentence_encoders/thought_vector.py @@ -32,7 +32,7 @@ def __init__(self, embedding=None, **kwargs): def clear_cache(self): self._get_thought_vector.cache_clear() - @functools.lru_cache(maxsize=2 ** 10) + @functools.lru_cache(maxsize=2**10) def _get_thought_vector(self, text): """Sums the embeddings of all the words in ``text`` into a "thought vector".""" diff --git a/textattack/goal_functions/goal_function.py b/textattack/goal_functions/goal_function.py index 7fa6a7c3e..5d51bdf05 100644 --- a/textattack/goal_functions/goal_function.py +++ b/textattack/goal_functions/goal_function.py @@ -40,7 +40,7 @@ def __init__( use_cache=True, query_budget=float("inf"), model_batch_size=32, - model_cache_size=2 ** 20, + model_cache_size=2**20, ): validators.validate_model_goal_function_compatibility( self.__class__, model_wrapper.model.__class__ diff --git a/textattack/goal_functions/text/minimize_bleu.py b/textattack/goal_functions/text/minimize_bleu.py index 339995772..92613be5a 100644 --- a/textattack/goal_functions/text/minimize_bleu.py +++ b/textattack/goal_functions/text/minimize_bleu.py @@ -59,7 +59,7 @@ def extra_repr_keys(self): return ["maximizable", "target_bleu"] -@functools.lru_cache(maxsize=2 ** 12) +@functools.lru_cache(maxsize=2**12) def get_bleu(a, b): ref = a.words hyp = b.words diff --git a/textattack/goal_functions/text/non_overlapping_output.py b/textattack/goal_functions/text/non_overlapping_output.py index 443aa2366..e2cb49820 100644 --- a/textattack/goal_functions/text/non_overlapping_output.py +++ b/textattack/goal_functions/text/non_overlapping_output.py @@ -38,12 +38,12 @@ def _get_score(self, model_output, _): return num_words_diff / len(get_words_cached(self.ground_truth_output)) -@functools.lru_cache(maxsize=2 ** 12) +@functools.lru_cache(maxsize=2**12) def get_words_cached(s): return np.array(words_from_text(s)) -@functools.lru_cache(maxsize=2 ** 12) +@functools.lru_cache(maxsize=2**12) def word_difference_score(s1, s2): """Returns the number of words that are non-overlapping between s1 and s2.""" diff --git a/textattack/metrics/attack_metrics/words_perturbed.py b/textattack/metrics/attack_metrics/words_perturbed.py index d4b128241..6104de1b3 100644 --- a/textattack/metrics/attack_metrics/words_perturbed.py +++ b/textattack/metrics/attack_metrics/words_perturbed.py @@ -31,7 +31,7 @@ def calculate(self, results): self.total_attacks = len(self.results) self.all_num_words = np.zeros(len(self.results)) self.perturbed_word_percentages = np.zeros(len(self.results)) - self.num_words_changed_until_success = np.zeros(2 ** 16) + self.num_words_changed_until_success = np.zeros(2**16) self.max_words_changed = 0 for i, result in enumerate(self.results): diff --git a/textattack/shared/validators.py b/textattack/shared/validators.py index 55f4ed08c..45513a2a3 100644 --- a/textattack/shared/validators.py +++ b/textattack/shared/validators.py @@ -25,7 +25,10 @@ r"^textattack.models.helpers.word_cnn_for_classification.*", r"^transformers.modeling_\w*\.\w*ForSequenceClassification$", ], - (NonOverlappingOutput, MinimizeBleu,): [ + ( + NonOverlappingOutput, + MinimizeBleu, + ): [ r"^textattack.models.helpers.t5_for_text_to_text.*", ], }