nesl / nlp_adversarial_examples

Implementation code for the paper "Generating Natural Language Adversarial Examples"
MIT License
167 stars 49 forks source link

Iteration indexing bug in attacks.py #3

Closed LC-John closed 5 years ago

LC-John commented 5 years ago

In attack method, the iteration index "i" is used when generating "childs", which I believe may cause some iteration bugs... `for i in range(self.max_iters):

print(i)

        pop_preds = self.batch_model.predict(self.sess, np.array(pop))
        pop_scores = pop_preds[:, target]
        print('\t\t', i, ' -- ', np.max(pop_scores))
        pop_ranks = np.argsort(pop_scores)[::-1]
        top_attack = pop_ranks[0]
        logits = np.exp(pop_scores / self.temp)
        select_probs = logits / np.sum(logits)
        if np.argmax(pop_preds[top_attack, :]) == target:
            return pop[top_attack]
        elite = [pop[top_attack]] # elite
        #print(select_probs.shape)
        parent1_idx = np.random.choice(self.pop_size, size=self.pop_size-1, p=select_probs)
        parent2_idx = np.random.choice(self.pop_size, size=self.pop_size-1, p=select_probs)  
        childs = [self.crossover(pop[parent1_idx[i]],
                                 pop[parent2_idx[i]])
                  for i in range(self.pop_size-1)]
        childs = [self.perturb(x, x_orig, neigbhours_list, neighbours_dist, w_select_probs, target) for x in childs]
        pop = elite + childs `
malzantot commented 5 years ago

Thanks @LC-John for your notice. However, I believe this does not produce any problem. In Python 3, the variables created within the list comprehension will be distinct from the loop iteration variable even though they have the same name. ex:

for i in range(5):
    print(i,[i*i for i in range(2*i+1)])

will print: 0 [0] 1 [0, 1, 4] 2 [0, 1, 4, 9, 16] 3 [0, 1, 4, 9, 16, 25, 36] 4 [0, 1, 4, 9, 16, 25, 36, 49, 64]