Code Monkey home page Code Monkey logo

nlp_adversarial_examples's Introduction

  1. Download the Imdb dataset
./download_dataset.sh
  1. Download the glove vector embeddings (used by the model)
 ./download_glove.sh 
  1. Download the counter-fitted vectors (used by our attack)
./download_counterfitted_vectors.sh 
  1. Build the vocabulary and embeddings matrix.
python build_embeddings.py

That will take like a minute, and it will tokenize the dataset and save it to a pickle file. It will also compute some auxiliary files like the matrix of the vector embeddings for words in our dictionary. All files will be saved under aux_files directory created by this script.

  1. Train the sentiment analysis model.
python train_model.py

6)Download the Google language model.

./download_googlm.sh
  1. Pre-compute the distances between embeddings of different words (required to do the attack) and save the distance matrix.
python compute_dist_mat.py 

  1. Now, we are ready to try some attacks ! You can do so by running the IMDB_AttackDemo.ipynb Jupyter notebook !

Attacking Textual Entailment model

The model we are using for our experiment is the SNLI model of Keras SNLI Model .

First, Download the dataset using

bash download_snli_data.sh

Download the Glove and Counter-fitted Glove embedding vectors

bash ./download_glove.sh
bash ./download_counterfitted_vectors.sh

Train the NLI model

python sni_rnn.py

Pre-compute the embedding matrix

python nli_compute_dist_matrix.py

Now, you are ready to run the attack using example code provided in NLI_AttackDemo.ipynb Jupyter notebook.

nlp_adversarial_examples's People

Contributors

malzantot avatar ysharma1126 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nlp_adversarial_examples's Issues

Iteration indexing bug in attacks.py

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

Memory error in compute_dist_mat.py

The size of embedding_matrix in 30050001 due to which there is a memory error while doing -2np.dot(embedding_matrix.T , embedding_matrix).
How do I fix this?

Cannot download counter-fitted vectors

Hi!
When I try to run download_counterfitted_vectors.sh, it shows a error (403 forbidden) and I cannot download the counter-filterd vectors.
Can you check this out?

Segmentation fault in compute_dist_mat.py

Hi,

I keep getting a segmentation fault when I run compute_dist_mat.py:

$ python compute_dist_mat.py 
Using TensorFlow backend.
Segmentation fault

All the previous steps in the README seem to complete without error. I've tried this on two different computers with python 2.7 and tensorflow 1.4 and 1.9, respectively. Any ideas on how to get around this? Or if someone could share the output of this script, that would also be appreciated. Thanks!

Confused about baseline attack

Hi there,

I am highly interested in your fantastic work. However, I am a little bit confused about the baseline attack you used in your code and your paper. In BaselineDemo.ipynb, what is the NewBaseline attack that you imported? I couldn't find it in Attacks.py but PerturbSentBaseline(). Also, you mentioned in your paper that you use a baseline method to greedily find adversarial texts, could you please be more specific on the baseline attack you use? Thank you so much.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.