joey1993 / bert-defender

codes for paper "learning to discriminate perturbations for blocking adversarial attacks in text classification" in EMNLP19
15 stars 3 forks source link

UnboundLocalError: local variable 'tok_emb' referenced before assignment in bert_utils.py (methode: attack_word) #1

Open JanMackensen55 opened 4 years ago

JanMackensen55 commented 4 years ago

Hi I have tried training the discriminator with the following command:

python bert_discriminator.py 
--task_name sst-2 
--do_train
--do_lower_case
--data_dir data/SST-2/
--bert_model bert-base-uncased
--max_seq_length 128
--train_batch_size 8
--learning_rate 2e-5
--num_train_epochs 25
--output_dir ./tmp/disc/

and i get the following error message:

Traceback (most recent call last):
  File "bert_discriminator.py", line 474, in <module>
    main()
  File "bert_discriminator.py", line 290, in main
    vocab_list)
  File "C:\Users\janma\Documents\bert-defender\bert_utils.py", line 502, in convert_examples_to_features_flaw
    label, tok_flaw = random_attack(tok, embeddings, emb_index, words) #embeddings
  File "C:\Users\janma\Documents\bert-defender\bert_utils.py", line 786, in random_attack
    tok_flaw = attack_word(tok, p, emb_dict, vocab_list)
  File "C:\Users\janma\Documents\bert-defender\bert_utils.py", line 770, in attack_word
    most_similar_word_id = query_most_similar_word_id_from_embedding(p, tok_emb, 20)
UnboundLocalError: local variable 'tok_emb' referenced before assignment

I used crawl-300d-2M.vec as my embedding dictionary and your provided sst-2 dataset as training data.

I think in the attack_word method, some code is missing, as the case of a not found embedding does not seem to be handled correctly.

Yours sincerely, Jan Mackensen

tydusky commented 4 years ago

I ran into the same problem.