Skip to content

Commit c7d1bc7

Browse files
Irina NicolaeIrina Nicolae
authored andcommitted
Fix Keras and TF resource alloc problem in text tests
1 parent 3252ef1 commit c7d1bc7

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

art/attacks/configurable_text_attack_unittest.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import keras.backend as k
77
import numpy as np
88
import tensorflow as tf
9+
# import torch.nn as nn
10+
# import torch.nn.functional as f
11+
# import torch.optim as optim
912
from keras.layers import Embedding, Conv1D, LeakyReLU, MaxPooling1D, Dense, Flatten
1013
from keras.models import Sequential
1114

@@ -28,13 +31,13 @@
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

Comments
 (0)