66import keras .backend as k
77import numpy as np
88import tensorflow as tf
9+ # import torch.nn as nn
10+ # import torch.nn.functional as f
11+ # import torch.optim as optim
912from keras .layers import Embedding , Conv1D , LeakyReLU , MaxPooling1D , Dense , Flatten
1013from keras .models import Sequential
1114
2831# self.fc = nn.Linear(2304, 10)
2932#
3033# def forward(self, x):
31- # x = self.pool(F .relu(self.conv(x)))
34+ # x = self.pool(f .relu(self.conv(x)))
3235# x = x.view(-1, 2304)
3336# logit_output = self.fc(x)
3437#
3538# return logit_output
36-
37-
39+ #
40+ #
3841# class PtFlatten(nn.Module):
3942# def forward(self, x):
4043# n = x.size()
@@ -45,7 +48,7 @@ class TestConfigurableTextAttack(unittest.TestCase):
4548 @classmethod
4649 def setUpClass (cls ):
4750 from art .utils import load_imdb , to_categorical
48- from art .classifiers import KerasTextClassifier , TFTextClassifier
51+ from art .classifiers import KerasTextClassifier , TFTextClassifier , PyTorchTextClassifier
4952
5053 k .clear_session ()
5154 k .set_learning_phase (1 )
@@ -129,8 +132,8 @@ def test_imdb_fgsm(self):
129132 from art .attacks .configurable_text_attack import TextFGSM , loss_gradient_score
130133
131134 (_ , _ ), (x_test , y_test ) = self .imdb
132- models = { 'Keras' : self .classifier_k , 'TF' : self .classifier_tf }
133- for backend , model in models . items () :
135+ models = [( 'TF' , self .classifier_tf ), ( 'Keras' , self .classifier_k )]
136+ for backend , model in models :
134137 logger .info ('Text FGSM attack on %s' , backend )
135138 attack = ConfigurableTextAttack (classifier = model , nb_changes = 2 , transform = TextFGSM (eps = 200 ),
136139 score = loss_gradient_score , stop_condition = check_prediction_change )
@@ -141,10 +144,10 @@ def test_imdb_head(self):
141144 from art .attacks .configurable_text_attack import TextFGSM , TemporalHeadScore
142145
143146 (_ , _ ), (x_test , y_test ) = self .imdb
144- models = { 'Keras' : self .classifier_k , 'TF' : self .classifier_tf }
147+ models = [( 'TF' , self .classifier_tf ), ( 'Keras' , self .classifier_k )]
145148 scorer = TemporalHeadScore ()
146149
147- for backend , model in models . items () :
150+ for backend , model in models :
148151 logger .info ('Text head score attack on %s' , backend )
149152
150153 attack = ConfigurableTextAttack (classifier = model , nb_changes = 2 , score = scorer , transform = TextFGSM (eps = 200 ),
@@ -156,10 +159,10 @@ def test_imdb_combined(self):
156159 from art .attacks .configurable_text_attack import CombinedScore , TextFGSM
157160
158161 (_ , _ ), (x_test , y_test ) = self .imdb
159- models = { 'Keras' : self .classifier_k , 'TF' : self .classifier_tf }
162+ models = [( 'TF' , self .classifier_tf ), ( 'Keras' , self .classifier_k )]
160163 scorer = CombinedScore ()
161164
162- for backend , model in models . items () :
165+ for backend , model in models :
163166 logger .info ('Text combined score attack on %s' , backend )
164167 attack = ConfigurableTextAttack (classifier = model , nb_changes = 2 , score = scorer , transform = TextFGSM (eps = 1000 ),
165168 stop_condition = check_prediction_change )
0 commit comments