-
Notifications
You must be signed in to change notification settings - Fork 0
/
HyGloadAttack.py
1317 lines (1186 loc) · 60.5 KB
/
HyGloadAttack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import os
import numpy as np
from pathlib import Path
from scipy.special import softmax
np.random.seed(1234)
import pickle
import dataloader
from train_classifier import Model
from itertools import zip_longest
import criteria
import random
random.seed(0)
import csv
import time
import math
import json
import joblib
import sys
csv.field_size_limit(sys.maxsize)
import tensorflow_hub as hub
import tensorflow.compat.v1 as tf
import copy
import tensorflow as my_tf2
from tqdm import trange
tf.disable_v2_behavior()
from collections import defaultdict
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, SequentialSampler, TensorDataset
from sklearn.cluster import KMeans
from BERT.tokenization import BertTokenizer
from BERT.modeling import BertForSequenceClassification, BertConfig, BertForSequenceClassification_embed
tf.compat.v1.disable_eager_execution()
class USE(object):
def __init__(self, cache_path):
super(USE, self).__init__()
os.environ['TFHUB_CACHE_DIR'] = cache_path
module_url = "https://tfhub.dev/google/universal-sentence-encoder-large/5"
print("embed")
# self.embed = hub.Module(module_url)
# 原语句加载模型失效。解决加载失效问题。 这里加载的v5版本。原语句加载的v4版本。
self.embed = my_tf2.saved_model.load("[path to HyGloadAttack dir]/dependencies/others/USE_cache/usel5")
print("embed ok")
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
self.sess = tf.Session(config=config)
self.build_graph()
self.sess.run([tf.global_variables_initializer(), tf.tables_initializer()])
def build_graph(self):
self.sts_input1 = tf.placeholder(tf.string, shape=(None))
self.sts_input2 = tf.placeholder(tf.string, shape=(None))
sts_encode1 = tf.nn.l2_normalize(self.embed(self.sts_input1), axis=1)
sts_encode2 = tf.nn.l2_normalize(self.embed(self.sts_input2), axis=1)
self.cosine_similarities = tf.reduce_sum(tf.multiply(sts_encode1, sts_encode2), axis=1)
clip_cosine_similarities = tf.clip_by_value(self.cosine_similarities, -1.0, 1.0)
self.sim_scores = 1.0 - tf.acos(clip_cosine_similarities)
# 下面是OpenAttack的相似度计算方式,所有baseline采用上方的cosine_similarity计算相似度
# self.sim_scores = 1.0 - tf.acos(clip_cosine_similarities) / math.pi
def semantic_sim(self, sents1, sents2):
scores = self.sess.run(
[self.sim_scores],
feed_dict={
self.sts_input1: sents1,
self.sts_input2: sents2,
})
return scores
class NLI_infer_BERT(nn.Module):
def __init__(self,
pretrained_dir,
nclasses,
max_seq_length=128,
batch_size=32):
super(NLI_infer_BERT, self).__init__()
if torch.cuda.is_available():
self.model = BertForSequenceClassification.from_pretrained(pretrained_dir, num_labels=nclasses).cuda()
else:
self.model = BertForSequenceClassification.from_pretrained(pretrained_dir, num_labels=nclasses)
self.dataset = NLIDataset_BERT(pretrained_dir, max_seq_length=max_seq_length, batch_size=batch_size)
def text_pred(self, text_data, batch_size=32):
self.model.eval()
dataloader = self.dataset.transform_text(text_data, batch_size=batch_size)
probs_all = []
for input_ids, input_mask, segment_ids in dataloader:
if torch.cuda.is_available():
input_ids = input_ids.cuda()
input_mask = input_mask.cuda()
segment_ids = segment_ids.cuda()
with torch.no_grad():
logits = self.model(input_ids, segment_ids, input_mask)
probs = nn.functional.softmax(logits, dim=-1)
probs_all.append(probs)
return torch.cat(probs_all, dim=0)
class NLI_infer_embed_BERT(nn.Module):
def __init__(self,
pretrained_dir,
nclasses,
max_seq_length=128,
batch_size=32):
super(NLI_infer_embed_BERT, self).__init__()
if torch.cuda.is_available():
self.model = BertForSequenceClassification_embed.from_pretrained(pretrained_dir, num_labels=nclasses).cuda()
else:
self.model = BertForSequenceClassification_embed.from_pretrained(pretrained_dir, num_labels=nclasses)
self.dataset = NLIDataset_BERT(pretrained_dir, max_seq_length=max_seq_length, batch_size=batch_size)
def text_pred(self, text_data, batch_size=32):
self.model.eval()
dataloader = self.dataset.transform_text(text_data, batch_size=batch_size)
probs_all = []
for input_ids, input_mask, segment_ids in dataloader:
if torch.cuda.is_available():
input_ids = input_ids.cuda()
input_mask = input_mask.cuda()
segment_ids = segment_ids.cuda()
with torch.no_grad():
pooled_output,logits = self.model(input_ids, segment_ids, input_mask)
probs = nn.functional.softmax(logits, dim=-1)
probs_all.append(probs)
return pooled_output,torch.cat(probs_all, dim=0)
class InputFeatures(object):
def __init__(self, input_ids, input_mask, segment_ids):
self.input_ids = input_ids
self.input_mask = input_mask
self.segment_ids = segment_ids
class NLIDataset_BERT(Dataset):
def __init__(self,
pretrained_dir,
max_seq_length=128,
batch_size=32):
self.tokenizer = BertTokenizer.from_pretrained(pretrained_dir, do_lower_case=True)
self.max_seq_length = max_seq_length
self.batch_size = batch_size
def convert_examples_to_features(self, examples, max_seq_length, tokenizer):
features = []
for (ex_index, text_a) in enumerate(examples):
tokens_a = tokenizer.tokenize(' '.join(text_a))
if len(tokens_a) > max_seq_length - 2:
tokens_a = tokens_a[:(max_seq_length - 2)]
tokens = ["[CLS]"] + tokens_a + ["[SEP]"]
segment_ids = [0] * len(tokens)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
input_mask = [1] * len(input_ids)
padding = [0] * (max_seq_length - len(input_ids))
input_ids += padding
input_mask += padding
segment_ids += padding
assert len(input_ids) == max_seq_length
assert len(input_mask) == max_seq_length
assert len(segment_ids) == max_seq_length
features.append(
InputFeatures(input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids))
return features
def transform_text(self, data, batch_size=32):
eval_features = self.convert_examples_to_features(data,
self.max_seq_length, self.tokenizer)
all_input_ids = torch.tensor([f.input_ids for f in eval_features], dtype=torch.long)
all_input_mask = torch.tensor([f.input_mask for f in eval_features], dtype=torch.long)
all_segment_ids = torch.tensor([f.segment_ids for f in eval_features], dtype=torch.long)
eval_data = TensorDataset(all_input_ids, all_input_mask, all_segment_ids)
eval_sampler = SequentialSampler(eval_data)
eval_dataloader = DataLoader(eval_data, sampler=eval_sampler, batch_size=batch_size)
return eval_dataloader
def calc_sim(text_ls, new_texts, idx, sim_score_window, sim_predictor):
len_text = len(text_ls)
half_sim_score_window = (sim_score_window - 1) // 2
if idx >= half_sim_score_window and len_text - idx - 1 >= half_sim_score_window:
text_range_min = idx - half_sim_score_window
text_range_max = idx + half_sim_score_window + 1
elif idx < half_sim_score_window and len_text - idx - 1 >= half_sim_score_window:
text_range_min = 0
text_range_max = sim_score_window
elif idx >= half_sim_score_window and len_text - idx - 1 < half_sim_score_window:
text_range_min = len_text - sim_score_window
text_range_max = len_text
else:
text_range_min = 0
text_range_max = len_text
if text_range_min < 0:
text_range_min = 0
if text_range_max > len_text:
text_range_max = len_text
if idx == -1:
text_rang_min = 0
text_range_max = len_text
batch_size = 16
total_semantic_sims = np.array([])
for i in range(0, len(new_texts), batch_size):
batch = new_texts[i:i+batch_size]
semantic_sims = \
sim_predictor.semantic_sim([' '.join(text_ls[text_range_min:text_range_max])],
list(map(lambda x: ' '.join(x[text_range_min:text_range_max]), batch)))[0]
total_semantic_sims = np.concatenate((total_semantic_sims, semantic_sims))
return total_semantic_sims
def get_attack_result(new_text, predictor, orig_label, batch_size):
'''
查看attack是否成功
return: true 攻击成功
false 攻击失败
'''
new_probs = predictor(new_text, batch_size=batch_size)
pr=(orig_label!= torch.argmax(new_probs, dim=-1)).data.cpu().numpy()
return pr
def soft_threshold(alpha, beta):
'''
lammda l1
gamma[i][0] γ
软阈值 lammda l1 ,gamma[i][0] γ 让gradient_2 小于 -0.1则+0.1 大于0.1则-0.1
'''
if beta > alpha:
return beta - alpha
elif beta < -alpha:
return beta + alpha
else:
return 0
def get_word_embed(words_perturb_doc_idx,text,word_idx_dict,embed_content):
text_embed = []
for idx in words_perturb_doc_idx:
text_embed.append(
[float(num) for num in embed_content[word_idx_dict[text[idx]]].strip().split()[1:]])
text_embed_matrix = np.asarray(text_embed)
return text_embed_matrix
def normalize_min_max(distance, min_value, max_value):
return (distance - min_value) / (max_value - min_value)
def combined_similarity(vector, vectors, k=3):
vector_reshape = vector.reshape(-1)
vectors_reshape = vectors.reshape(vectors.shape[0],vector_reshape.shape[0])
cosine_similarities = np.dot(vectors_reshape, vector_reshape) / (np.linalg.norm(vector_reshape) * np.linalg.norm(vectors_reshape, axis=1))
cosine_similarities = 0.5 * (cosine_similarities + 1)
# 耗时特短
combined_scores = cosine_similarities
sorted_score = np.argsort(combined_scores)
if len(sorted_score) == 1:
most_similar_index = sorted_score[-1]
else:
# most_similar_index = sorted_score[-1-k:-1]
most_similar_index = sorted_score[-1-k:-1]
return most_similar_index, combined_scores
def remove_duplicate_lists(input_list):
# 使用集合来存储唯一的子列表
unique_list_set = set(tuple(sublist) for sublist in input_list)
# 将集合转换回列表,并返回结果
unique_list = [list(sublist_tuple) for sublist_tuple in unique_list_set]
return unique_list
def get_shortest_text(predictor,text,orig_label,batch_size,best_changed_num,text_ls):
replaced_text = []
for i in range(len(text_ls)):
tmp_text = copy.deepcopy(text)
if text_ls[i] != text[i]:
tmp_text[i] = text_ls[i]
replaced_text.append(tmp_text)
prs = get_attack_result(replaced_text, predictor, orig_label, batch_size)
adv_text = []
for i in range(len(prs)):
if np.sum(prs[i]) >= 0:
adv_text.append(replaced_text[i])
def HyGload_attack(
fuzz_val, top_k_words, sample_index, text_ls,
true_label, predictor, stop_words_set, word2idx, idx2word,
cos_sim, sim_predictor=None, import_score_threshold=-1.,
sim_score_threshold=0.5, sim_score_window=15, synonym_num=50,
batch_size=32,embed_func = '',budget=1000,myargs=None):
'''
HyGload_attack(
top_k_words, 选取topk words
text_ls, text 真实的需要攻击的text
true_label,
predictor,
word2idx,
idx2word,
cos_sim,
sim_predictor=None,
sim_score_window=15,
batch_size=32,
embed_func:embed function
budget:查询预算)
predictor: 预测器
@return:
' '.join(best_attack), max_changes, len(changed_indices), \
orig_label, torch.argmax(predictor([best_attack])), qrs, sim, random_sim
new_text, num_changed, random_changed, \
orig_label, new_label, num_queries, sim, random_sim
HyGload_attack(
args.fuzz, args.top_k_words, idx, text,
true_label, predictor, stop_words_set, word2idx, idx2word,
sim_lis , sim_predictor=use, sim_score_threshold=args.sim_score_threshold,
import_score_threshold=args.import_score_threshold,
sim_score_window=args.sim_score_window,
synonym_num=args.synonym_num,
batch_size=args.batch_size,
embed_func = args.counter_fitting_embeddings_path,
budget=args.budget)
'''
# print("myargs:",myargs.budget)
orig_probs = predictor([text_ls]).squeeze()
orig_label = torch.argmax(orig_probs)
orig_prob = orig_probs.max()
if true_label != orig_label:
return '', 0, 0, orig_label, orig_label, 0, 0, 0
else:
# word2idx 构建
word_idx_dict={}
with open(embed_func, 'r') as ifile:
for index, line in enumerate(ifile):
word = line.strip().split()[0]
word_idx_dict[word] = index
# word:[embed -0.022007 -0.05519 0.02872 0.068785 xxxx]
embed_file=open(embed_func)
embed_content=embed_file.readlines()
# 获取单词的词性pos_ls = ['NOUN','VERB','NOUN','VERB']
pos_ls = criteria.get_pos(text_ls)
len_text = len(text_ls)
if len_text < sim_score_window:
sim_score_threshold = 0.1
# find ["ADJ", "ADV", "VERB", "NOUN"] in text
words_perturb = []
pos_ls = criteria.get_pos(text_ls)
pos_pref = ["ADJ", "ADV", "VERB", "NOUN"]
# for pos in pos_pref:
# for i in range(len(pos_ls)):
# if pos_ls[i] == pos and len(text_ls[i]) > 2:
# words_perturb.append((i, text_ls[i]))
for i in range(len(pos_ls)):
if pos_ls[i] in pos_pref and len(text_ls[i]) > 2:
words_perturb.append((i, text_ls[i]))
random.shuffle(words_perturb)
words_perturb = words_perturb[:top_k_words]
# get words perturbed idx embed doc_idx.find synonyms and make a dict of synonyms of each word.
words_perturb_idx= []
words_perturb_embed = []
words_perturb_doc_idx = []
for idx, word in words_perturb:
if word in word_idx_dict:
words_perturb_doc_idx.append(idx)
words_perturb_idx.append(word2idx[word])
words_perturb_embed.append([float(num) for num in embed_content[ word_idx_dict[word] ].strip().split()[1:]])
words_perturb_embed_matrix = np.asarray(words_perturb_embed)
# 干扰的同义词选取
synonym_words,synonym_values=[],[]
for idx in words_perturb_idx:
res = list(zip(*(cos_sim[idx])))
temp=[]
for ii in res[1]:
temp.append(idx2word[ii])
synonym_words.append(temp)
temp=[]
for ii in res[0]:
temp.append(ii)
synonym_values.append(temp)
synonyms_all = []
synonyms_dict = defaultdict(list)
for idx, word in words_perturb:
if word in word2idx:
synonyms = synonym_words.pop(0)
if synonyms:
synonyms_all.append((idx, synonyms))
synonyms_dict[word] = synonyms
qrs = 0
num_changed = 0
flag = 0
th = 0 # thershold
# Initialization
# random initialize result to attack model
# TODO: 直接拉满pert 而后search reduction。再并集
# 拉满pert
random_sample_adv_n = 1 # 设置初始化有几个advsample
random_adv_sampled_n = 0
random_adv_samples = []
for _ in range(2500):
random_text = text_ls[:]
for j in range(len(synonyms_all)):
idx = synonyms_all[j][0]
syn = synonyms_all[j][1]
random_text[idx] = random.choice(syn)
if j >= len_text:
break
pr = get_attack_result([random_text], predictor, orig_label, batch_size)
qrs+=1
if np.sum(pr)>0:
random_adv_samples.append(random_text)
flag = 1
changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=random_text[i]:
changes+=1
# print("changes",changes,"|sentence:"," ".join(random_text))
random_adv_sampled_n+=1
if random_adv_sampled_n>=random_sample_adv_n:
break
# 如果全量没找到
if not np.sum(pr)>0:
prepared_flag = 0
old_qrs = qrs
while qrs < len(text_ls)+old_qrs:
# for i in range(len(text_ls)*2):
prepared_text = text_ls[:]
for i in range(len(synonyms_all)):
idx = synonyms_all[i][0]
syn = synonyms_all[i][1]
prepared_text[idx] = random.choice(syn[:])
if i >= th:
break
pr = get_attack_result([prepared_text], predictor, orig_label, batch_size)
qrs+=1
th +=1
if th >= len_text or th >= len(synonyms_all):
th = 0
if np.sum(pr)>0:
prepared_flag = 1
break
if prepared_flag == 1:
# print("into local search ")
random_adv_samples.append(prepared_text)
else:
return ' '.join(random_text), 0, 0, \
orig_label, orig_label, qrs, 0, 0
return ' '.join(random_text), 0, 0, \
orig_label, orig_label, qrs, 0, 0
# TODO:my search reduction
random_adv_reducted = []
for i in random_adv_samples:
while True:
choices = []
pert_index = []
for i in range(len(text_ls)):
if random_text[i] != text_ls[i]:
pert_index.append(i)
# For each word substituted in the original text, change it with its original word and compute
# the change in semantic similarity.
for i in range(len(text_ls)):
if random_text[i] != text_ls[i]:
new_text = random_text[:]
new_text[i] = text_ls[i]
semantic_sims = calc_sim(text_ls, [new_text], -1, sim_score_window, sim_predictor)
qrs+=1
pr = get_attack_result([new_text], predictor, orig_label, batch_size)
if np.sum(pr) > 0:
choices.append((i,semantic_sims[0]))
# Sort the relacements by semantic similarity and replace back the words with their original
# counterparts till text remains adversarial.
if len(choices) > 0:
choices.sort(key = lambda x: x[1])
choices.reverse()
for i in range(len(choices)):
new_text = random_text[:]
new_text[choices[i][0]] = text_ls[choices[i][0]]
pr = get_attack_result([new_text], predictor, orig_label, batch_size)
qrs+=1
if pr[0] == 0:
continue
random_text[choices[i][0]] = text_ls[choices[i][0]]
#
if len(choices) == 0:
break
random_adv_reducted.append(random_text)
# # TODO:求pert交集
combined_list = []
# print(None in combined_list)
for items in zip(*random_adv_reducted):
if len(set(items)) == 1:
combined_list.append(items[0])
else:
combined_list.append(items[0]) # Or some marker for different values
# 重新对交集部分进行随机替换
random_text = combined_list[:]
#
if flag == 1:
changed = 0
for i in range(len(text_ls)):
if text_ls[i]!=random_text[i]:
changed+=1
# STEP 2: Search Space Reduction i.e. Move Sample Close to Boundary
while True:
choices = []
# For each word substituted in the original text, change it with its original word and compute
# the change in semantic similarity.
for i in range(len(text_ls)):
if random_text[i] != text_ls[i]:
new_text = random_text[:]
new_text[i] = text_ls[i]
semantic_sims = calc_sim(text_ls, [new_text], -1, sim_score_window, sim_predictor)
qrs+=1
pr = get_attack_result([new_text], predictor, orig_label, batch_size)
if np.sum(pr) > 0:
choices.append((i,semantic_sims[0]))
# Sort the relacements by semantic similarity and replace back the words with their original
# counterparts till text remains adversarial.
if len(choices) > 0:
choices.sort(key = lambda x: x[1])
choices.reverse()
for i in range(len(choices)):
new_text = random_text[:]
new_text[choices[i][0]] = text_ls[choices[i][0]]
pr = get_attack_result([new_text], predictor, orig_label, batch_size)
qrs+=1
if pr[0] == 0:
break
random_text[choices[i][0]] = text_ls[choices[i][0]]
#
if len(choices) == 0:
break
#
changed_indices = []
num_changed = 0
for i in range(len(text_ls)):
if text_ls[i]!=random_text[i]:
changed_indices.append(i)
num_changed+=1
# print("changed:",str(num_changed)+"\tqrs"+str(qrs))
random_sim = calc_sim(text_ls, [random_text], -1, sim_score_window, sim_predictor)[0]
# out of budget
if qrs > budget:
return ' '.join(random_text), len(changed_indices), len(changed_indices), \
orig_label, torch.argmax(predictor([random_text])), qrs, random_sim, random_sim
best_attack = random_text
best_sim = random_sim
old_random_sim = copy.deepcopy(random_sim)
old_random_text = copy.deepcopy(random_text)
# if num changed == 1
if np.sum(get_attack_result([random_text], predictor, orig_label, batch_size)) > 0 and (num_changed == 1):
change_idx = 0
for i in range(len(text_ls)):
if text_ls[i]!=random_text[i]:
change_idx = i
break
idx = word2idx[text_ls[change_idx]]
res = list(zip(*(cos_sim[idx])))
x_ts = []
for widx in res[1]:
w = idx2word[widx]
random_text[change_idx] = w
x_ts.append(random_text[:])
prs = get_attack_result(x_ts, predictor, orig_label, batch_size)
sims = calc_sim(text_ls, x_ts, -1, sim_score_window, sim_predictor)
is_update_random_attack = False
for x_t_, pr, sim in zip(x_ts, prs, sims):
qrs += 1
if np.sum(pr) > 0 and sim >= best_sim:
best_attack = x_t_[:]
best_sim = sim
is_update_random_attack = True
if is_update_random_attack:
return ' '.join(best_attack), 1, 1, \
orig_label, torch.argmax(predictor([best_attack])), qrs, best_sim, best_sim
else:
# print("old_random_text:",get_attack_result([old_random_text], predictor, orig_label, batch_size))
return ' '.join(old_random_text), 1, 1, \
orig_label, torch.argmax(predictor([old_random_text])), qrs, old_random_sim, old_random_sim
# STEP 3: Optimization
# Optimization Procedure
random_adv_embed = []
for idx in words_perturb_doc_idx:
random_adv_embed.append([float(num) for num in embed_content[word_idx_dict[random_text[idx]]].strip().split()[1:]])
random_adv_embed_matrix = np.asarray(random_adv_embed)
random_2_text_ls_dist = np.sum((random_adv_embed_matrix)**2)
is_update_best_attack = True
current_pert_num = None
for t in trange(100):
theta_old_text = best_attack
changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=best_attack[i]:
changes+=1
# 是否更新了best_attack
if is_update_best_attack:
# print(" ".join(best_attack))
# print("changes",changes,"|sentence:"," ".join(best_attack))
old_adv_embed = []
for idx in words_perturb_doc_idx:
old_adv_embed.append([float(num) for num in embed_content[word_idx_dict[theta_old_text[idx]]].strip().split()[1:]])
old_adv_embed_matrix = np.asarray(old_adv_embed)
# P0 = E0 − E
theta_old = old_adv_embed_matrix-words_perturb_embed_matrix
theta_old_2_text_ls_dist = np.sum((theta_old)**2)
dont_use_v = []
opt_stack = [] # 优化历史栈,记录push过的sentence
is_update_best_attack = False
# TODO:构建搜索空间,降重了,不会有重复的query 。
# TODO:标识每组 nonzero_ele 的 text。当当前text不可用时,当前text产生时前面的text也不可用。
# TODO:验证text的按数量计算的可用性。
theta_old_neighbor_text_search_space = []
theta_old_neighbor_text_search_space_dic = {}
for_nums = 300
nonzero_ele = np.nonzero(np.linalg.norm(theta_old,axis = -1))[0].tolist()
# p_sim = []
# for i in nonzero_ele:
# tmp_text = copy.deepcopy(best_attack)
# tmp_text[synonyms_all[i][0]] = text_ls[synonyms_all[i][0]]
# sim = calc_sim(text_ls, [tmp_text], -1, sim_score_window, sim_predictor)[0]
# p_sim.append(sim-best_sim)
for _ in range(for_nums):
# print(changes)
# V = P + βU
# random perturb
u_vec = np.random.normal(loc=0.0, scale=1,size=theta_old.shape)
# 随机生成0.8-1.2之间的单个数字 动态耗时巨大且收益不高
# theta_old_neighbor = theta_old+0.5*u_vec/random_2_text_ls_dist*theta_old_2_text_ls_dist*random.uniform(0.9, 1.1)
theta_old_neighbor = theta_old+0.5*u_vec*random.uniform(0.9, 1.1)
# theta_perturb_dist 距离
theta_perturb_dist = np.sum((theta_old_neighbor)**2, axis=1)
# nonzero_ele = np.nonzero(np.linalg.norm(theta_perturb_dist, axis = -1))[0].tolist()
perturb_strength_order = np.argsort(-theta_perturb_dist[nonzero_ele])
# Optimizing ρi
theta_old_neighbor_text = text_ls[:]
# for changed words to get
# print("nonzero1:",len(nonzero_ele),nonzero_ele)
pert_num = 1
theta_perturb_dist = np.sum((theta_old)**2, axis=1)
perturb_word_idx_list = []
perturb_word_idx_list = nonzero_ele
for perturb_idx in range(len(perturb_word_idx_list)):
perturb_word_idx = perturb_word_idx_list[perturb_idx]
# find the replaceable words
perturb_target = words_perturb_embed_matrix[perturb_word_idx]+theta_old_neighbor[perturb_word_idx]
syn_feat_set = []
for syn in synonyms_all[perturb_word_idx][1]:
syn_feat = [float(num) for num in embed_content[word_idx_dict[syn]].strip().split()[1:]]
syn_feat_set.append(syn_feat)
# find the neighbour synonyms words
# syn_feat_set - perturb_target is the target
perturb_syn_dist = np.sum((syn_feat_set-perturb_target)**2, axis=1)
perturb_syn_order = np.argsort(perturb_syn_dist)
replacement = synonyms_all[perturb_word_idx][1][perturb_syn_order[0]]
# TODO:这里需要深拷贝才能构建,但是是否真的需要深拷贝呢?我们直接采用最大深度的数据是否更好?
# 已证明,深拷贝采用更大深度数据不一定好,但是不深拷贝可以减少 set后的影响。qrs增加了,但是pert和sim都更优化了。
theta_old_neighbor_text[synonyms_all[perturb_word_idx][0]] = replacement
# 必须要先扰动再判断绕过。
# if not (pert_num == changes and pert_num == changes-1):
if not (pert_num == changes):
pert_num+=1
continue
# 采用
tmp = copy.deepcopy(theta_old_neighbor_text)
# 采用最大深度方式
# tmp = theta_old_neighbor_text
theta_old_neighbor_text_search_space.append(tmp)
if pert_num not in theta_old_neighbor_text_search_space_dic.keys():
theta_old_neighbor_text_search_space_dic[pert_num] = []
if tmp not in theta_old_neighbor_text_search_space_dic[pert_num]:
theta_old_neighbor_text_search_space_dic[pert_num].append([tmp,u_vec])
pert_num+=1
# print("changesd")
else:
pass
if current_pert_num is None:
current_pert_num = list(theta_old_neighbor_text_search_space_dic.keys())[-1]
else:
current_pert_num = changes
if current_pert_num == 0:
max_changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=best_attack[i]:
max_changes+=1
return ' '.join(best_attack), max_changes, len(changed_indices), \
orig_label, torch.argmax(predictor([best_attack])), qrs, best_sim, random_sim
# for _ in range(int((int(math.sqrt(num_changed))+2)*10)):
for _ in range(num_changed*3):
# 更新搜索pert num 空间
# TODO:
# 选点
if current_pert_num==1:
current_pert_num_tmp = current_pert_num
else:
# current_pert_num_tmp = current_pert_num -1
current_pert_num_tmp = current_pert_num
# print(search_space_th_tmp)
while True:
try:
item = theta_old_neighbor_text_search_space_dic[current_pert_num_tmp].pop()
u_vec = item[1]
theta_old_neighbor_text = item[0]
break
except Exception as e:
if len(theta_old_neighbor_text_search_space_dic[current_pert_num_tmp])==0 and \
len(theta_old_neighbor_text_search_space_dic[current_pert_num])==0:
theta_old_neighbor_text = best_attack
# print("in while ")
# 解除注释后 性能会更好
# if theta_old_neighbor_text in opt_stack:
# sim = best_sim
# max_changes = 0
# for i in range(len(text_ls)):
# if text_ls[i]!=best_attack[i]:
# max_changes+=1
# return ' '.join(best_attack), max_changes, len(changed_indices), \
# orig_label, torch.argmax(predictor([best_attack])), qrs, sim, random_sim
is_update_best_attack = True
opt_stack.append(theta_old_neighbor_text)
break
current_pert_num_tmp = current_pert_num
if " ".join(theta_old_neighbor_text) in dont_use_v:
continue
tmp_v_list = [row[1] for row in theta_old_neighbor_text_search_space_dic[current_pert_num_tmp]]
tmp_t_list = [row[0] for row in theta_old_neighbor_text_search_space_dic[current_pert_num_tmp]]
pr = get_attack_result([theta_old_neighbor_text], predictor, orig_label, batch_size)
qrs+=1
if qrs > budget:
sim = best_sim
max_changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=best_attack[i]:
max_changes+=1
return ' '.join(best_attack), max_changes, len(changed_indices), \
orig_label, torch.argmax(predictor([best_attack])), qrs, sim, random_sim
if np.sum(pr)>0:
break
if len(tmp_v_list)>=1:
most_similar_index, combined_scores = combined_similarity(u_vec,np.asarray(tmp_v_list))
if type(most_similar_index)==type(np.array(0)):
for i in most_similar_index:
dont_use_v.append(" ".join(tmp_t_list[i]))
else:
dont_use_v.append(" ".join(tmp_t_list[most_similar_index]))
if np.sum(pr)<=0:
if len(tmp_v_list)>=1:
most_similar_index, combined_scores = combined_similarity(u_vec,np.asarray(tmp_v_list))
if type(most_similar_index)==type(np.array(0)):
for i in most_similar_index:
dont_use_v.append(" ".join(tmp_t_list[i]))
else:
dont_use_v.append(" ".join(tmp_t_list[most_similar_index]))
continue
changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=best_attack[i]:
changes+=1
# print("--------remove unnecessary words----")
# -----------remove unnecessary words-------------
while True:
choices = []
pert_index = []
for i in range(len(text_ls)):
if theta_old_neighbor_text[i] != text_ls[i]:
pert_index.append(i)
# For each word substituted in the original text, change it with its original word and compute
# the change in semantic similarity.
for i in range(len(text_ls)):
if theta_old_neighbor_text[i] != text_ls[i]:
new_text = theta_old_neighbor_text[:]
new_text[i] = text_ls[i]
semantic_sims = calc_sim(text_ls, [new_text], -1, sim_score_window, sim_predictor)
qrs+=1
pr = get_attack_result([new_text], predictor, orig_label, batch_size)
if np.sum(pr) > 0:
choices.append((i,semantic_sims[0]))
# Sort the relacements by semantic similarity and replace back the words with their original
# counterparts till text remains adversarial.
if len(choices) > 0:
choices.sort(key = lambda x: x[1])
choices.reverse()
for i in range(len(choices)):
new_text = theta_old_neighbor_text[:]
new_text[choices[i][0]] = text_ls[choices[i][0]]
pr = get_attack_result([new_text], predictor, orig_label, batch_size)
qrs+=1
if pr[0] == 0:
break
theta_old_neighbor_text[choices[i][0]] = text_ls[choices[i][0]]
#
if len(choices) == 0:
break
# -----------remove unnecessary words-------------
# -----------if change num == 1 -------------
x_t = theta_old_neighbor_text
num_changed = 0
for i in range(len(text_ls)):
if text_ls[i] != x_t[i]:
num_changed += 1
x_t_sim = calc_sim(text_ls, [x_t], -1, sim_score_window, sim_predictor)[0]
# if attack success and num changed ==1
if np.sum(get_attack_result([x_t], predictor, orig_label, batch_size)) > 0 and (num_changed == 1):
change_idx = 0
for i in range(len(text_ls)):
if text_ls[i]!=x_t[i]:
change_idx = i
break
idx = word2idx[text_ls[change_idx]]
res = list(zip(*(cos_sim[idx])))
x_ts = []
for widx in res[1]:
w = idx2word[widx]
x_t[change_idx] = w
x_ts.append(x_t[:])
prs = get_attack_result(x_ts, predictor, orig_label, batch_size)
sims = calc_sim(text_ls, x_ts, -1, sim_score_window, sim_predictor)
for x_t_, pr, sim in zip(x_ts, prs, sims):
qrs += 1
if np.sum(pr) > 0 and sim >= best_sim:
best_attack = x_t_[:]
is_update_best_attack = True
best_sim = sim
return ' '.join(best_attack), 1, len(changed_indices), \
orig_label, torch.argmax(predictor([best_attack])), qrs, best_sim, random_sim
# -----------if change num == 1 -------------
x_t = theta_old_neighbor_text
x_t_sim = calc_sim(text_ls, [x_t], -1, sim_score_window, sim_predictor)[0]
# if attack success and is best sim
if np.sum(get_attack_result([x_t], predictor, orig_label, batch_size)) > 0 and x_t_sim >= best_sim:
best_attack = x_t[:]
is_update_best_attack = True
best_sim = x_t_sim
# get adv embed
x_t_adv_embed = []
for idx in words_perturb_doc_idx:
x_t_adv_embed.append(
[float(num) for num in embed_content[word_idx_dict[x_t[idx]]].strip().split()[1:]])
x_t_adv_embed_matrix = np.asarray(x_t_adv_embed)
x_t_pert = x_t_adv_embed_matrix - words_perturb_embed_matrix
x_t_perturb_dist = np.sum((x_t_pert) ** 2, axis=1)
nonzero_ele = np.nonzero(np.linalg.norm(x_t_pert, axis=-1))[0].tolist()
perturb_word_idx_list = nonzero_ele
for i in range(1):
# 生成可替换的sample
replaced_txt = []
for i in perturb_word_idx_list:
for j in synonyms_all[i][1]:
tmp_txt = copy.deepcopy(x_t)
tmp_txt[synonyms_all[i][0]] = j
replaced_txt.append(tmp_txt)
# 计算sim
sims = calc_sim(text_ls, replaced_txt, -1, sim_score_window, sim_predictor)
# 过滤低sim
candi_samples_filter = []
for i in range(len(replaced_txt)):
if sims[i]>=best_sim:
candi_samples_filter.append(replaced_txt[i])
# 倒排sim找最大sim
filtered_sorted_sim = np.argsort(-sims[sims>=best_sim])
for i in filtered_sorted_sim:
pr = get_attack_result([candi_samples_filter[i]], predictor, orig_label, batch_size)
qrs+=1
if np.sum(pr) > 0:
best_attack = candi_samples_filter[i]
is_update_best_attack = True
best_sim = sims[sims>=best_sim][i]
break
if qrs >= budget:
max_changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=best_attack[i]:
max_changes+=1
return ' '.join(best_attack), 1, max_changes, \
orig_label, torch.argmax(predictor([best_attack])), qrs, best_sim, random_sim
sim = calc_sim(text_ls, [best_attack], -1, sim_score_window, sim_predictor)[0]
max_changes = 0
for i in range(len(text_ls)):
if text_ls[i]!=best_attack[i]:
max_changes+=1
return ' '.join(best_attack), max_changes, len(changed_indices), \
orig_label, torch.argmax(predictor([best_attack])), qrs, sim, random_sim
else:
print("Not Found")
return '', 0,0, orig_label, orig_label, 0, 0, 0
def main():
# if True 方便看代码
if True:
parser = argparse.ArgumentParser()
parser.add_argument("--dataset_path",
type=str,
# required=True,
default="[path to HyGloadAttack dir]/data/mr",
help="Which dataset to attack.")
parser.add_argument("--nclasses",
type=int,
default=2,
help="How many classes for classification.")
parser.add_argument("--target_model",
type=str,
# required=True,
default="wordCNN",
choices=['wordLSTM', 'bert', 'wordCNN'],
help="Target models for text classification: fasttext, charcnn, word level lstm "
"For NLI: InferSent, ESIM, bert-base-uncased")
parser.add_argument("--target_model_path",
type=str,
# required=True,
default="[path to HyGloadAttack dir]/dependencies/models/cnn/mr",
help="pre-trained target model path")
parser.add_argument("--word_embeddings_path",
type=str,
default='[path to HyGloadAttack dir]/dependencies/others/glove.6B.200d.txt',
help="path to the word embeddings for the target model")
parser.add_argument("--counter_fitting_embeddings_path",
type=str,
default="[path to HyGloadAttack dir]/dependencies/others/counter-fitted-vectors.txt",
help="path to the counter-fitting embeddings we used to find synonyms")
parser.add_argument("--counter_fitting_cos_sim_path",
type=str,
default='[path to HyGloadAttack dir]/dependencies/others/mat.txt',
help="pre-compute the cosine similarity scores based on the counter-fitting embeddings")
parser.add_argument("--USE_cache_path",
type=str,
# required=True,
default="[path to HyGloadAttack dir]/dependencies/others/USE_cache",
help="Path to the USE encoder cache.")
parser.add_argument("--output_dir",