-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAttacker.py
569 lines (455 loc) · 26.1 KB
/
RAttacker.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
import numpy as np
import torch
import torch.nn as nn
import copy
from torchvision import transforms
from PIL import Image
import torch.nn.functional as F
import random
import time
class Attacker():
def __init__(self, model, img_attacker, txt_attacker):
self.model=model
self.img_attacker = img_attacker
self.txt_attacker = txt_attacker
def attack(self, imgs, txts, txt2img, device='cpu', max_length=30, scales=None, masks=None, **kwargs):
with torch.no_grad():
origin_img_output = self.model.inference_image(self.img_attacker.normalization(imgs))
img_supervisions = origin_img_output['image_feat'][txt2img]
adv_txts = self.txt_attacker.img_guided_attack(self.model, txts, img_embeds=img_supervisions)
with torch.no_grad():
txts_input = self.txt_attacker.tokenizer(adv_txts, padding='max_length', truncation=True, max_length=max_length, return_tensors="pt").to(device)
txts_output = self.model.inference_text(txts_input)
txt_supervisions = txts_output['text_feat']
start_time = time.time()
adv_imgs,last_adv_imgs = self.img_attacker.txt_guided_attack(self.model, imgs, txt2img, device,
scales=scales, txt_embeds = txt_supervisions)
end_time = time.time()
execuate_time = end_time - start_time
with torch.no_grad():
adv_imgs_outputs = self.model.inference_image(self.img_attacker.normalization(adv_imgs))
adv_img_supervisions = adv_imgs_outputs['image_feat'][txt2img]
last_adv_imgs_outputs = self.model.inference_image(self.img_attacker.normalization(last_adv_imgs))
last_adv_img_supervisions = last_adv_imgs_outputs['image_feat'][txt2img]
adv_txts = self.txt_attacker.img_guided_attack(self.model, txts, img_embeds=img_supervisions,
adv_img_embeds = adv_img_supervisions,last_adv_img_embeds=last_adv_img_supervisions)
return adv_imgs, adv_txts, execuate_time
class ImageAttacker():
def __init__(self, normalization, eps=2/255, steps=10, step_size=0.5/255,sample_numbers=5):
self.normalization = normalization
self.eps = eps
self.steps = steps
self.step_size = step_size
self.sample_numbers = sample_numbers
def loss_func(self, adv_imgs_embeds, txts_embeds, txt2img):
device = adv_imgs_embeds.device
it_sim_matrix = adv_imgs_embeds @ txts_embeds.T
it_labels = torch.zeros(it_sim_matrix.shape).to(device)
for i in range(len(txt2img)):
it_labels[txt2img[i], i]=1
loss_IaTcpos = -(it_sim_matrix * it_labels).sum(-1).mean()
loss = loss_IaTcpos
return loss
def rand3Num(self):
while True:
num1 = random.randint(1, 100)
if 100-num1>1:
num2 = random.randint(1, 100 - num1)
else:
num1 = 98
num2 = 1
num3 = 100 - num1 - num2
if 1 <= num3 <= 100:
break
return (num1,num2,num3)
def txt_guided_attack(self, model, imgs, txt2img, device, scales=None, txt_embeds=None):
model.eval()
b, _, _, _ = imgs.shape
if scales is None:
scales_num = 1
else:
scales_num = len(scales) +1
adv_imgs = imgs.detach() + torch.from_numpy(np.random.uniform(-self.eps, self.eps, imgs.shape)).float().to(device)
adv_imgs = torch.clamp(adv_imgs, 0.0, 1.0)
last_adv_imgs=None
start_time = time.time()
ratio_list = []
for step in range(self.steps): # self.steps=10
if last_adv_imgs!=None:
samples=[]
clone_adv_imgs = adv_imgs.clone()
loss_list=[]
for k in range(self.sample_numbers):
samples.append(self.rand3Num())
for sample in samples:
adv_imgs=(sample[0]/100)*clone_adv_imgs+(sample[1]/100)*imgs+(sample[2]/100)*last_adv_imgs
adv_imgs.requires_grad_()
if self.normalization is not None:
adv_imgs_output = model.inference_image(self.normalization(adv_imgs))
else:
adv_imgs_output = model.inference_image(adv_imgs)
adv_imgs_embeds = adv_imgs_output['image_feat']
model.zero_grad()
with torch.enable_grad():
loss = torch.tensor(0.0, dtype=torch.float32).to(device)
loss = self.loss_func(adv_imgs_embeds, txt_embeds, txt2img)
adv_imgs.retain_grad()
loss.backward()
grad = adv_imgs.grad
grad = grad / torch.mean(torch.abs(grad), dim=(1,2,3), keepdim=True)
perturbation = self.step_size * grad.sign()
adv_imgs = clone_adv_imgs.detach()+perturbation
if self.normalization is not None:
adv_imgs_output = model.inference_image(self.normalization(adv_imgs))
else:
adv_imgs_output = model.inference_image(adv_imgs)
adv_imgs_embeds = adv_imgs_output['image_feat']
model.zero_grad()
with torch.enable_grad():
loss = torch.tensor(0.0, dtype=torch.float32).to(device)
loss = self.loss_func(adv_imgs_embeds, txt_embeds, txt2img)
loss.backward()
loss_list.append(loss.item())
candidate_index=loss_list.index(max(loss_list))
ratio_list.append(samples[candidate_index])
adv_imgs = (samples[candidate_index][0]/100)*clone_adv_imgs+(samples[candidate_index][1]/100)*imgs+(samples[candidate_index][2]/100)*last_adv_imgs
adv_imgs.requires_grad_()
scaled_imgs = self.get_scaled_imgs(adv_imgs, [0.5,0.75,1.25,1.5], device)
if self.normalization is not None:
adv_imgs_output = model.inference_image(self.normalization(scaled_imgs))
else:
adv_imgs_output = model.inference_image(scaled_imgs)
adv_imgs_embeds = adv_imgs_output['image_feat']
model.zero_grad()
with torch.enable_grad():
loss = torch.tensor(0.0, dtype=torch.float32).to(device)
for i in range(5):
loss_item = self.loss_func(adv_imgs_embeds[i*b:i*b+b], txt_embeds, txt2img)
loss += loss_item
adv_imgs.retain_grad()
loss.backward()
grad = adv_imgs.grad
grad = grad / torch.mean(torch.abs(grad), dim=(1,2,3), keepdim=True)
perturbation = self.step_size * grad.sign()
adv_imgs = clone_adv_imgs.detach() + perturbation
adv_imgs = torch.min(torch.max(adv_imgs, imgs - self.eps), imgs + self.eps)
adv_imgs = torch.clamp(adv_imgs, 0.0, 1.0)
last_adv_imgs = clone_adv_imgs.clone()
else:
last_adv_imgs = adv_imgs.clone()
adv_imgs.requires_grad_()
scaled_imgs = self.get_scaled_imgs(adv_imgs, [0.5,0.75,1.25,1.5], device)
if self.normalization is not None:
adv_imgs_output = model.inference_image(self.normalization(scaled_imgs))
else:
adv_imgs_output = model.inference_image(scaled_imgs)
adv_imgs_embeds = adv_imgs_output['image_feat']
model.zero_grad()
with torch.enable_grad():
loss = torch.tensor(0.0, dtype=torch.float32).to(device)
for i in range(5):
loss_item = self.loss_func(adv_imgs_embeds[i*b:i*b+b], txt_embeds, txt2img)
loss += loss_item
loss.backward()
grad = adv_imgs.grad
grad = grad / torch.mean(torch.abs(grad), dim=(1,2,3), keepdim=True)
perturbation = self.step_size * grad.sign()
adv_imgs = adv_imgs.detach()+perturbation
adv_imgs = torch.min(torch.max(adv_imgs, imgs - self.eps), imgs + self.eps)
adv_imgs = torch.clamp(adv_imgs, 0.0, 1.0)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"The function execution time: {elapsed_time} seconds")
return adv_imgs,last_adv_imgs
def save_img(self,img_name,norm_img):
pil_array = (norm_img * 255).to(torch.uint8).cpu().numpy()
pil_img=Image.fromarray(np.transpose(pil_array, (1, 2, 0)))
img_path="./mscoco_imgs/"
pil_img.save(img_path+img_name)
def get_scaled_imgs(self, imgs, scales=None, device='cuda'):
if scales is None:
return imgs
ori_shape = (imgs.shape[-2], imgs.shape[-1])
reverse_transform = transforms.Resize(ori_shape,
interpolation=transforms.InterpolationMode.BICUBIC)
result = []
for ratio in scales:
scale_shape = (int(ratio*ori_shape[0]),
int(ratio*ori_shape[1]))
scale_transform = transforms.Resize(scale_shape,
interpolation=transforms.InterpolationMode.BICUBIC)
scaled_imgs = imgs + torch.from_numpy(np.random.normal(0.0, 0.05, imgs.shape)).float().to(device)
scaled_imgs = scale_transform(scaled_imgs)
scaled_imgs = torch.clamp(scaled_imgs, 0.0, 1.0)
reversed_imgs = reverse_transform(scaled_imgs)
result.append(reversed_imgs)
return torch.cat([imgs,]+result, 0)
filter_words = ['a', 'about', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'ain', 'all', 'almost',
'alone', 'along', 'already', 'also', 'although', 'am', 'among', 'amongst', 'an', 'and', 'another',
'any', 'anyhow', 'anyone', 'anything', 'anyway', 'anywhere', 'are', 'aren', "aren't", 'around', 'as',
'at', 'back', 'been', 'before', 'beforehand', 'behind', 'being', 'below', 'beside', 'besides',
'between', 'beyond', 'both', 'but', 'by', 'can', 'cannot', 'could', 'couldn', "couldn't", 'd', 'didn',
"didn't", 'doesn', "doesn't", 'don', "don't", 'down', 'due', 'during', 'either', 'else', 'elsewhere',
'empty', 'enough', 'even', 'ever', 'everyone', 'everything', 'everywhere', 'except', 'first', 'for',
'former', 'formerly', 'from', 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'he', 'hence',
'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers', 'herself', 'him', 'himself', 'his',
'how', 'however', 'hundred', 'i', 'if', 'in', 'indeed', 'into', 'is', 'isn', "isn't", 'it', "it's",
'its', 'itself', 'just', 'latter', 'latterly', 'least', 'll', 'may', 'me', 'meanwhile', 'mightn',
"mightn't", 'mine', 'more', 'moreover', 'most', 'mostly', 'must', 'mustn', "mustn't", 'my', 'myself',
'namely', 'needn', "needn't", 'neither', 'never', 'nevertheless', 'next', 'no', 'nobody', 'none',
'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'o', 'of', 'off', 'on', 'once', 'one', 'only',
'onto', 'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'per',
'please', 's', 'same', 'shan', "shan't", 'she', "she's", "should've", 'shouldn', "shouldn't", 'somehow',
'something', 'sometime', 'somewhere', 'such', 't', 'than', 'that', "that'll", 'the', 'their', 'theirs',
'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore', 'therein',
'thereupon', 'these', 'they', 'this', 'those', 'through', 'throughout', 'thru', 'thus', 'to', 'too',
'toward', 'towards', 'under', 'unless', 'until', 'up', 'upon', 'used', 've', 'was', 'wasn', "wasn't",
'we', 'were', 'weren', "weren't", 'what', 'whatever', 'when', 'whence', 'whenever', 'where',
'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', 'wherever', 'whether', 'which', 'while',
'whither', 'who', 'whoever', 'whole', 'whom', 'whose', 'why', 'with', 'within', 'without', 'won',
"won't", 'would', 'wouldn', "wouldn't", 'y', 'yet', 'you', "you'd", "you'll", "you're", "you've",
'your', 'yours', 'yourself', 'yourselves', '.', '-', 'a the', '/', '?', 'some', '"', ',', 'b', '&', '!',
'@', '%', '^', '*', '(', ')', "-", '-', '+', '=', '<', '>', '|', ':', ";", '~', '·']
filter_words = set(filter_words)
class TextAttacker():
def __init__(self, ref_net, tokenizer, cls=True, max_length=30, number_perturbation=1, topk=10, threshold_pred_score=0.3, batch_size=32,text_ratios=[0.6,0.2,0.2]):
self.ref_net = ref_net
self.tokenizer = tokenizer
self.max_length = max_length
# epsilon_txt
self.num_perturbation = number_perturbation
self.threshold_pred_score = threshold_pred_score
self.topk = topk
self.batch_size = batch_size
self.cls = cls
self.text_ratios = text_ratios
def img_guided_attack(self, net, texts, img_embeds = None, adv_img_embeds = None,last_adv_img_embeds=None):
device = self.ref_net.device
text_inputs = self.tokenizer(texts, padding='max_length', truncation=True, max_length=self.max_length, return_tensors='pt').to(device)
# substitutes
mlm_logits = self.ref_net(text_inputs.input_ids, attention_mask=text_inputs.attention_mask).logits
word_pred_scores_all, word_predictions = torch.topk(mlm_logits, self.topk, -1) # seq-len k
# original state
origin_output = net.inference_text(text_inputs)
if self.cls:
origin_embeds = origin_output['text_feat'][:, 0, :].detach()
else:
origin_embeds = origin_output['text_feat'].flatten(1).detach()
final_adverse = []
for i, text in enumerate(texts):
# word importance eval
important_scores = self.get_important_scores(text, net, origin_embeds[i], self.batch_size, self.max_length)
list_of_index = sorted(enumerate(important_scores), key=lambda x: x[1], reverse=True)
words, sub_words, keys = self._tokenize(text)
final_words = copy.deepcopy(words)
change = 0
for top_index in list_of_index:
if change >= self.num_perturbation:
break
tgt_word = words[top_index[0]]
if tgt_word in filter_words:
continue
if keys[top_index[0]][0] > self.max_length - 2:
continue
substitutes = word_predictions[i, keys[top_index[0]][0]:keys[top_index[0]][1]] # L, k
word_pred_scores = word_pred_scores_all[i, keys[top_index[0]][0]:keys[top_index[0]][1]]
substitutes = get_substitues(substitutes, self.tokenizer, self.ref_net, 1, word_pred_scores,
self.threshold_pred_score)
replace_texts = [' '.join(final_words)]
available_substitutes = [tgt_word]
for substitute_ in substitutes:
substitute = substitute_
if substitute == tgt_word:
continue # filter out original word
if '##' in substitute:
continue # filter out sub-word
if substitute in filter_words:
continue
'''
# filter out atonyms
if substitute in w2i and tgt_word in w2i:
if cos_mat[w2i[substitute]][w2i[tgt_word]] < 0.4:
continue
'''
temp_replace = copy.deepcopy(final_words)
temp_replace[top_index[0]] = substitute
available_substitutes.append(substitute)
replace_texts.append(' '.join(temp_replace))
replace_text_input = self.tokenizer(replace_texts, padding='max_length', truncation=True, max_length=self.max_length, return_tensors='pt').to(device)
replace_output = net.inference_text(replace_text_input)
if self.cls:
replace_embeds = replace_output['text_feat'][:, 0, :]
else:
replace_embeds = replace_output['text_feat'].flatten(1)
if adv_img_embeds==None:
loss = self.loss_func(replace_embeds, img_embeds, i)
else:
loss = self.text_ratios[0]*self.loss_func(replace_embeds,img_embeds,i) + self.text_ratios[1]*self.loss_func(replace_embeds,adv_img_embeds,i) + self.text_ratios[2]*self.loss_func(replace_embeds,last_adv_img_embeds,i)
candidate_idx = loss.argmax()
final_words[top_index[0]] = available_substitutes[candidate_idx]
if available_substitutes[candidate_idx] != tgt_word:
change += 1
final_adverse.append(' '.join(final_words))
return final_adverse
def loss_func(self, txt_embeds, img_embeds, label):
loss_TaIcpos = -txt_embeds.mul(img_embeds[label].repeat(len(txt_embeds), 1)).sum(-1)
loss = loss_TaIcpos
return loss
def attack(self, net, texts):
device = self.ref_net.device
text_inputs = self.tokenizer(texts, padding='max_length', truncation=True, max_length=self.max_length, return_tensors='pt').to(device)
# substitutes
mlm_logits = self.ref_net(text_inputs.input_ids, attention_mask=text_inputs.attention_mask).logits
word_pred_scores_all, word_predictions = torch.topk(mlm_logits, self.topk, -1) # seq-len k
# original state
origin_output = net.inference_text(text_inputs)
if self.cls:
origin_embeds = origin_output['text_embed'][:, 0, :].detach()
else:
origin_embeds = origin_output['text_embed'].flatten(1).detach()
criterion = torch.nn.KLDivLoss(reduction='none')
final_adverse = []
for i, text in enumerate(texts):
# word importance eval
important_scores = self.get_important_scores(text, net, origin_embeds[i], self.batch_size, self.max_length)
list_of_index = sorted(enumerate(important_scores), key=lambda x: x[1], reverse=True)
words, sub_words, keys = self._tokenize(text)
final_words = copy.deepcopy(words)
change = 0
for top_index in list_of_index:
if change >= self.num_perturbation:
break
tgt_word = words[top_index[0]]
if tgt_word in filter_words:
continue
if keys[top_index[0]][0] > self.max_length - 2:
continue
substitutes = word_predictions[i, keys[top_index[0]][0]:keys[top_index[0]][1]] # L, k
word_pred_scores = word_pred_scores_all[i, keys[top_index[0]][0]:keys[top_index[0]][1]]
substitutes = get_substitues(substitutes, self.tokenizer, self.ref_net, 1, word_pred_scores,
self.threshold_pred_score)
replace_texts = [' '.join(final_words)]
available_substitutes = [tgt_word]
for substitute_ in substitutes:
substitute = substitute_
if substitute == tgt_word:
continue # filter out original word
if '##' in substitute:
continue # filter out sub-word
if substitute in filter_words:
continue
'''
# filter out atonyms
if substitute in w2i and tgt_word in w2i:
if cos_mat[w2i[substitute]][w2i[tgt_word]] < 0.4:
continue
'''
temp_replace = copy.deepcopy(final_words)
temp_replace[top_index[0]] = substitute
available_substitutes.append(substitute)
replace_texts.append(' '.join(temp_replace))
replace_text_input = self.tokenizer(replace_texts, padding='max_length', truncation=True, max_length=self.max_length, return_tensors='pt').to(device)
replace_output = net.inference_text(replace_text_input)
if self.cls:
replace_embeds = replace_output['text_embed'][:, 0, :]
else:
replace_embeds = replace_output['text_embed'].flatten(1)
loss = criterion(replace_embeds.log_softmax(dim=-1), origin_embeds[i].softmax(dim=-1).repeat(len(replace_embeds), 1))
loss = loss.sum(dim=-1)
candidate_idx = loss.argmax()
final_words[top_index[0]] = available_substitutes[candidate_idx]
if available_substitutes[candidate_idx] != tgt_word:
change += 1
final_adverse.append(' '.join(final_words))
return final_adverse
def _tokenize(self, text):
words = text.split(' ')
sub_words = []
keys = []
index = 0
for word in words:
sub = self.tokenizer.tokenize(word)
sub_words += sub
keys.append([index, index + len(sub)])
index += len(sub)
return words, sub_words, keys
def _get_masked(self, text):
words = text.split(' ')
len_text = len(words)
masked_words = []
for i in range(len_text):
masked_words.append(words[0:i] + ['[UNK]'] + words[i + 1:])
# list of words
return masked_words
def get_important_scores(self, text, net, origin_embeds, batch_size, max_length):
device = origin_embeds.device
masked_words = self._get_masked(text)
masked_texts = [' '.join(words) for words in masked_words] # list of text of masked words
masked_embeds = []
for i in range(0, len(masked_texts), batch_size):
masked_text_input = self.tokenizer(masked_texts[i:i+batch_size], padding='max_length', truncation=True, max_length=max_length, return_tensors='pt').to(device)
masked_output = net.inference_text(masked_text_input)
if self.cls:
masked_embed = masked_output['text_feat'][:, 0, :].detach()
else:
masked_embed = masked_output['text_feat'].flatten(1).detach()
masked_embeds.append(masked_embed)
masked_embeds = torch.cat(masked_embeds, dim=0)
criterion = torch.nn.KLDivLoss(reduction='none')
import_scores = criterion(masked_embeds.log_softmax(dim=-1), origin_embeds.softmax(dim=-1).repeat(len(masked_texts), 1))
return import_scores.sum(dim=-1)
def get_substitues(substitutes, tokenizer, mlm_model, use_bpe, substitutes_score=None, threshold=3.0):
# substitues L,k
# from this matrix to recover a word
words = []
sub_len, k = substitutes.size() # sub-len, k
if sub_len == 0:
return words
elif sub_len == 1:
for (i, j) in zip(substitutes[0], substitutes_score[0]):
if threshold != 0 and j < threshold:
break
words.append(tokenizer._convert_id_to_token(int(i)))
else:
if use_bpe == 1:
words = get_bpe_substitues(substitutes, tokenizer, mlm_model)
else:
return words
#
# print(words)
return words
def get_bpe_substitues(substitutes, tokenizer, mlm_model):
# substitutes L, k
device = mlm_model.device
substitutes = substitutes[0:12, 0:4] # maximum BPE candidates
# find all possible candidates
all_substitutes = []
for i in range(substitutes.size(0)):
if len(all_substitutes) == 0:
lev_i = substitutes[i]
all_substitutes = [[int(c)] for c in lev_i]
else:
lev_i = []
for all_sub in all_substitutes:
for j in substitutes[i]:
lev_i.append(all_sub + [int(j)])
all_substitutes = lev_i
# all substitutes list of list of token-id (all candidates)
c_loss = nn.CrossEntropyLoss(reduction='none')
word_list = []
# all_substitutes = all_substitutes[:24]
all_substitutes = torch.tensor(all_substitutes) # [ N, L ]
all_substitutes = all_substitutes[:24].to(device)
# print(substitutes.size(), all_substitutes.size())
N, L = all_substitutes.size()
word_predictions = mlm_model(all_substitutes)[0] # N L vocab-size
ppl = c_loss(word_predictions.view(N * L, -1), all_substitutes.view(-1)) # [ N*L ]
ppl = torch.exp(torch.mean(ppl.view(N, L), dim=-1)) # N
_, word_list = torch.sort(ppl)
word_list = [all_substitutes[i] for i in word_list]
final_words = []
for word in word_list:
tokens = [tokenizer._convert_id_to_token(int(i)) for i in word]
text = tokenizer.convert_tokens_to_string(tokens)
final_words.append(text)
return final_words