-
Notifications
You must be signed in to change notification settings - Fork 1
/
attack_bb.py
603 lines (549 loc) · 29.3 KB
/
attack_bb.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy
import random
import string
import nltk
import sys, os
nltk.download('sentiwordnet')
nltk.download('wordnet')
nltk.download('averaged_perceptron_tagger')
nltk.download('punkt')
def flush():
sys.stdout.flush()
sys.stderr.flush()
flush()
from nltk.corpus import sentiwordnet as swn
from nltk.corpus import wordnet as wn
from nltk.stem import WordNetLemmatizer
import pickle as pkl
import gzip as gz
import pandas as pd
import copy
import threading
import time
next(swn.all_senti_synsets())
next(wn.words())
import config
flags = tf.app.flags.FLAGS
if flags.is_training:
import mh
import dataset
else:
import mh
import dataset
import classifier
from util import random_pick_idx_with_unnormalized_prob
from util import just_acc
from util import get_part_of_speech
class multi_thread_cgmh(threading.Thread):
def __init__(self, bb_atk_data, bb_w2i, bb_i2w, vocab, bb, m, sess, index=0, bb_max_seqlen=400, negs=[]):
threading.Thread.__init__(self)
self.__bb_atk_data = bb_atk_data
self.__vocab = vocab
self.__bb_word2idx = bb_w2i
self.__bb_idx2word = bb_i2w
self.__bb_max_seqlen = bb_max_seqlen
self.__bb = bb
self.__model = m
self.__sess = sess
self.__idx = index
self.__negs = []
def run(self):
out_dir = flags.cgmh_output_dir
fout = open(os.path.join(out_dir,
"log"+str(self.__idx)+".log"), "w")
res_path = os.path.join(out_dir,
"res"+str(self.__idx)+".res")
bb_atk_data = self.__bb_atk_data
bb_atk_data_size = len(self.__bb_atk_data['raw'])
bb_word2idx = self.__bb_word2idx
bb_idx2word = self.__bb_idx2word
vocab = self.__vocab
bb = self.__bb
m = self.__model
bb_max_seqlen = self.__bb_max_seqlen
sess = self.__sess
negations = self.__negs
op_prob = [flags.swp_prob,
flags.ins_prob,
flags.del_prob,
flags.pass_prob]
op_prob = op_prob / numpy.sum(op_prob)
n_sample = flags.sample_max_n
n_candidate = flags.n_candidate
just_acc_rate = flags.just_acc_rate
swp_lm_threshold = flags.lm_swp_threshold
ins_lm_threshold = flags.lm_ins_threshold
del_lm_threshold = flags.lm_del_threshold
swp_prob_threshold = flags.swp_threshold
ins_prob_threshold = flags.ins_threshold
del_prob_threshold = flags.del_threshold
swn_obj_threshold = flags.senti_obj_threshold
swn_pos_threshold = flags.senti_pos_threshold
seq_min_len = flags.seq_min_len
mode = flags.index_mode
res_log = []
sents = []
idx = 0
op = 3
total_time = 0
n_succ = 0
lemmatzr = WordNetLemmatizer()
for i in range(bb_atk_data_size):
start_time = time.time()
print ("===== DATA %d/%d =====" % (i+1, bb_atk_data_size),
file=fout, flush=True)
print ("DATA %d/%d, id=%d" % (i+1, bb_atk_data_size, self.__idx))
flush()
res_log.append([])
raw = copy.deepcopy(bb_atk_data["raw"][i])
raw = nltk.word_tokenize(raw.lower())
l = len(raw) + 1
if (l > flags.seq_max_len):
l = flags.seq_max_len
seq = [vocab.get_init_idx()]
for ii in range(1, l):
seq.append(vocab.get_vocab_idx(raw[ii-1]))
while len(seq) < flags.seq_max_len:
seq.append(vocab.get_pad_idx())
mask = [True]
for ii in range(1, l):
mask.append(False)
bb_y = bb_atk_data["y"][i]
bb_l = len(raw)
if (bb_l > bb_max_seqlen):
bb_l = bb_max_seqlen
bb_seq = []
for ii in range(bb_l):
if raw[ii] in bb_word2idx.keys():
bb_seq.append(bb_word2idx[raw[ii]])
else:
bb_seq.append(bb_word2idx["<unk>"])
while len(bb_seq) < bb_max_seqlen:
bb_seq.append(bb_word2idx["<pad>"])
sents.append([])
sample_cnt = 0
sample_all = 0
idx = 0
sents[-1].append(copy.deepcopy(raw))
print("%d/%d\tOriginal\tFAIL with %.5f" % (i+1, bb_atk_data_size, 1-bb_atk_data["prob"][i]),
end="\n\t", file=fout, flush=True)
for ii in range(len(raw)):
print (raw[ii], end=" ", file=fout, flush=True)
if bb_y == 1:
print("\t<POS>", file=fout, flush=True)
else:
print("\t<NEG>", file=fout, flush=True)
while sample_all < n_sample:
try:
wn.ensure_loaded()
sample_all += 1
op = random_pick_idx_with_unnormalized_prob(op_prob)
succ = False
if op == 3:
tmp_prob = sess.run(bb.prob, feed_dict={bb.X: [bb_seq],
bb.L: [bb_l]})[0][1-bb_y]
if tmp_prob >= 0.5:
res_log[i].append((sample_all, 1))
print ("%d/%d\t%d acc / %d all\tPASS\t SUCC with %.5f" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all+1, tmp_prob),
file=fout, flush=True)
succ = True
else:
res_log[i].append((sample_all, 0))
print ("%d/%d\t%d acc / %d all\tPASS\t FAIL with %.5f" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all+1, tmp_prob),
file=fout, flush=True)
sample_cnt += 1
sents[-1].append(copy.deepcopy(raw))
print("", end="\t", file=fout, flush=True)
for ii in range(len(raw)):
print (raw[ii], end=" ", file=fout, flush=True)
if bb_y == 1:
print("\t<POS>", file=fout, flush=True)
else:
print("\t<NEG>", file=fout, flush=True)
if succ:
print ("\tSUCC!")
flush()
break
continue
if mode == "random":
idx = random.randint(0, l-1)
elif mode == "traverse":
idx = (idx + 1) % l
elif mode == "grad":
if op == 1:
idx = random.randint(0, l-1)
else:
grad_vecs = sess.run(bb.embed_grad, feed_dict={bb.X: [bb_seq],
bb.L: [bb_l],
bb.Y: [1-bb_y]})[0][0]
grads = numpy.linalg.norm(grad_vecs, axis=-1)
candidate_grads = []
candidate_idxs = []
position_tag = nltk.pos_tag(raw)
for pos in range(len(position_tag)):
tmp_tag = get_part_of_speech(position_tag[pos][1])
if tmp_tag is None:
candidate_grads.append(grads[pos])
candidate_idxs.append(pos+1)
continue
tmp_wn = wn.synsets(lemmatzr.lemmatize(raw[pos]), pos=tmp_tag)
if len(tmp_wn) <= 0:
candidate_grads.append(grads[pos])
candidate_idxs.append(pos+1)
continue
tmp_swn = swn.senti_synset(tmp_wn[0].name())
if (tmp_swn.obj_score() > swn_obj_threshold \
or (tmp_swn.obj_score() <= swn_obj_threshold \
and abs(tmp_swn.pos_score()-tmp_swn.neg_score()) <= swn_pos_threshold)):
candidate_grads.append(grads[pos])
candidate_idxs.append(pos+1)
continue
idx_idx = random_pick_idx_with_unnormalized_prob(candidate_grads)
idx = candidate_idxs[idx_idx]
else:
assert False, "Invalid mode \""+mode+"\""
old_wrong_prob = sess.run(bb.prob, feed_dict={bb.X: [bb_seq],
bb.L: [bb_l]})[0][1-bb_y]
if op == 0:
if mask[idx]:
continue
proposal = m.op_replace(sess, copy.deepcopy(seq), l,
copy.deepcopy(bb_seq), bb_l, 1-bb_y,
idx, n_candidate, op_prob)
tmp_bb_seq = copy.deepcopy(bb_seq)
tmp_str = vocab.get_vocab(proposal['proposal'][idx])
if tmp_str in bb_word2idx.keys():
tmp_bb_seq[idx-1] = bb_word2idx[tmp_str]
else:
tmp_bb_seq[idx-1] = bb_word2idx["<unk>"]
new_wrong_prob = sess.run(bb.prob, feed_dict={bb.X: [tmp_bb_seq],
bb.L: [bb_l]})[0][1-bb_y]
tmp_raw = copy.deepcopy(raw)
tmp_raw[idx-1] = vocab.get_vocab(proposal["proposal"][idx])
new_tag = get_part_of_speech(nltk.pos_tag(tmp_raw)[idx-1][1])
if new_tag is None:
new_obj = 1
new_pos = 0
else:
new_wn = wn.synsets(lemmatzr.lemmatize(tmp_raw[idx-1]), pos=new_tag)
if len(new_wn) <= 0:
new_obj = 1
new_pos = 0
else:
new_swn = swn.senti_synset(new_wn[0].name())
new_obj = new_swn.obj_score()
new_pos = new_swn.pos_score() - new_swn.neg_score()
if (just_acc(just_acc_rate)
or (numpy.random.uniform(0,1) <= \
proposal["alpha"] * new_wrong_prob / old_wrong_prob
and proposal["old_prob"] * swp_lm_threshold <= proposal["new_prob"]
and old_wrong_prob * swp_prob_threshold <= new_wrong_prob
and (new_obj > swn_obj_threshold # objective
or (new_obj <= swn_obj_threshold # neutral
and abs(new_pos) <= swn_pos_threshold))
and (tmp_str not in negations))):
if new_wrong_prob >= 0.5:
res_log[i].append((sample_all, 1))
print ("%d/%d\t%d acc / %d all\tSWP\t SUCC with %.5f\t[%s](%d) => [%s](%d) (%d)" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all, new_wrong_prob,
vocab.get_vocab(seq[idx]), seq[idx],
vocab.get_vocab(proposal["proposal"][idx]), proposal["proposal"][idx], idx),
file=fout, flush=True)
succ = True
else:
res_log[i].append((sample_all, 0))
print ("%d/%d\t%d acc / %d all\tSWP\t FAIL with %.5f\t[%s](%d) => [%s](%d) (%d)" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all, new_wrong_prob,
vocab.get_vocab(seq[idx]), seq[idx],
vocab.get_vocab(proposal["proposal"][idx]), proposal["proposal"][idx], idx),
file=fout, flush=True)
sample_cnt += 1
seq = proposal["proposal"]
bb_seq = tmp_bb_seq
raw = tmp_raw
sents[-1].append(copy.deepcopy(raw))
print("", end="\t", file=fout, flush=True)
for ii in range(len(raw)):
print (raw[ii], end=" ", file=fout, flush=True)
if bb_y == 1:
print("\t<POS>", file=fout, flush=True)
else:
print("\t<NEG>", file=fout, flush=True)
else:
print ("%d/%d\t%d acc / %d all\tSWP\talpha %.2e" %
(i+1, bb_atk_data_size, sample_cnt, sample_all, proposal["alpha"]),
file=fout, flush=True)
elif op == 1:
if idx == l-1:
continue
proposal = m.op_insert(sess, copy.deepcopy(seq), l,
copy.deepcopy(bb_seq), bb_l, 1-bb_y,
idx, n_candidate, op_prob)
tmp_bb_seq = numpy.asarray(copy.deepcopy(bb_seq)).tolist()
tmp_str = vocab.get_vocab(proposal['proposal'][idx+1])
if tmp_str in bb_word2idx.keys():
tmp_bb_seq = tmp_bb_seq[:idx]+[bb_word2idx[tmp_str]]+tmp_bb_seq[idx:]
else:
tmp_bb_seq = tmp_bb_seq[:idx]+[bb_word2idx["<unk>"]]+tmp_bb_seq[idx:]
tmp_bb_seq = tmp_bb_seq[:-1]
tmp_bb_l = bb_l + 1
if tmp_bb_l >bb_max_seqlen:
tmp_bb_l = bb_max_seqlen
new_wrong_prob = sess.run(bb.prob, feed_dict={bb.X: [tmp_bb_seq],
bb.L: [tmp_bb_l]})[0][1-bb_y]
tmp_raw = copy.deepcopy(raw)
tmp_raw = tmp_raw[:idx]+[tmp_str]+tmp_raw[idx:]
new_tag = get_part_of_speech(nltk.pos_tag(tmp_raw)[idx][1])
if new_tag is None:
new_obj = 1
new_pos = 0
else:
new_wn = wn.synsets(lemmatzr.lemmatize(tmp_raw[idx]), pos=new_tag)
if len(new_wn) <= 0:
new_obj = 1
new_pos = 0
else:
new_swn = swn.senti_synset(new_wn[0].name())
new_obj = new_swn.obj_score()
new_pos = new_swn.pos_score() - new_swn.neg_score()
if (just_acc(just_acc_rate)
or (numpy.random.uniform(0,1) <= \
proposal["alpha"] * new_wrong_prob / old_wrong_prob
and proposal["old_prob"] * ins_lm_threshold <= proposal["new_prob"]
and old_wrong_prob * ins_prob_threshold <= new_wrong_prob
and (new_obj > swn_obj_threshold # objective
or (new_obj <= swn_obj_threshold # neutral
and new_pos <= swn_pos_threshold))
and (tmp_str not in negations))):
if new_wrong_prob >= 0.5:
res_log[i].append((sample_all, 1))
print ("%d/%d\t%d acc / %d all\tINS\t SUCC with %.5f\t[] => [%s](%d,%.1f,%.1f) (%d)" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all, new_wrong_prob,
vocab.get_vocab(proposal["proposal"][idx+1]), proposal["proposal"][idx+1],
new_obj, new_pos, idx), file=fout, flush=True)
succ = True
else:
res_log[i].append((sample_all, 0))
print ("%d/%d\t%d acc / %d all\tINS\t FAIL with %.5f\t[] => [%s](%d,%.1f,%.1f) (%d)" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all, new_wrong_prob,
vocab.get_vocab(proposal["proposal"][idx+1]), proposal["proposal"][idx+1],
new_obj, new_pos, idx), file=fout, flush=True)
sample_cnt += 1
seq = proposal["proposal"]
bb_seq = tmp_bb_seq
l += 1
mask = mask[:idx+1] + [False] + mask[idx+1:]
if l > flags.seq_max_len:
l = flags.seq_max_len
mask = mask[:l]
bb_l = tmp_bb_l
raw = raw[:idx] + [vocab.get_vocab(seq[idx+1])] + raw[idx:]
sents[-1].append(copy.deepcopy(raw))
print("", end="\t", file=fout, flush=True)
for ii in range(len(raw)):
print (raw[ii], end=" ", file=fout, flush=True)
if bb_y == 1:
print("\t<POS>", file=fout, flush=True)
else:
print("\t<NEG>", file=fout, flush=True)
else:
print ("%d/%d\t%d acc / %d all\tINS\talpha %.2e" %
(i+1, bb_atk_data_size, sample_cnt, sample_all, proposal["alpha"]),
file=fout, flush=True)
elif op == 2:
if mask[idx] or l-1 < seq_min_len:
continue
proposal = m.op_delete(sess, copy.deepcopy(seq), l,
copy.deepcopy(bb_seq), bb_l, 1-bb_y,
idx, n_candidate, op_prob)
tmp_bb_seq = numpy.asarray(copy.deepcopy(bb_seq)).tolist()
tmp_str = vocab.get_vocab(seq[idx])
tmp_bb_seq = tmp_bb_seq[:idx-1]+tmp_bb_seq[idx:]+[bb_word2idx['<pad>']]
tmp_bb_l = bb_l - 1
new_wrong_prob = sess.run(bb.prob, feed_dict={bb.X: [tmp_bb_seq],
bb.L: [tmp_bb_l]})[0][1-bb_y]
if (just_acc(just_acc_rate)
or (numpy.random.uniform(0,1) <= \
proposal["alpha"] * new_wrong_prob / old_wrong_prob
and proposal["old_prob"] * del_lm_threshold <= proposal["new_prob"]
and old_wrong_prob * del_prob_threshold <= new_wrong_prob)
and (tmp_str not in negations)):
if new_wrong_prob >= 0.5:
res_log[i].append((sample_all, 1))
print ("%d/%d\t%d acc / %d all\tDEL\t SUCC with %.5f\t[%s](%d) => [] (%d)" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all, new_wrong_prob,
vocab.get_vocab(seq[idx]), seq[idx], idx), file=fout, flush=True)
succ = True
else:
res_log[i].append((sample_all, 0))
print ("%d/%d\t%d acc / %d all\tDEL\t FAIL with %.5f\t[%s](%d) => [] (%d)" %
(i+1, bb_atk_data_size, sample_cnt+1, sample_all, new_wrong_prob,
vocab.get_vocab(seq[idx]), seq[idx], idx), file=fout, flush=True)
sample_cnt+= 1
seq = proposal["proposal"]
bb_seq = tmp_bb_seq
l -= 1
mask = mask[:idx] + mask[idx+1:]
bb_l = tmp_bb_l
raw = raw[:idx-1] + raw[idx:]
sents[-1].append(copy.deepcopy(raw))
print("", end="\t", file=fout, flush=True)
for ii in range(len(raw)):
print (raw[ii], end=" ", file=fout, flush=True)
if bb_y == 1:
print("\t<POS>", file=fout, flush=True)
else:
print("\t<NEG>", file=fout, flush=True)
else:
print ("%d/%d\t%d acc / %d all\tDEL\talpha %.2e" %
(i+1, bb_atk_data_size, sample_cnt, sample_all, proposal["alpha"]),
file=fout, flush=True)
if succ:
end_time = time.time()
total_time += end_time - start_time
n_succ += 1
print ("\tSUCC!")
print ("\t\ttime =", total_time, n_succ)
flush()
assert len(mask) == l
except Exception as e:
print ("Something went wrong... Abort!", file=fout, flush=True)
print ("Something went wrong... Abort! -- Thread %d" % self.__idx)
print ("\t", e)
sys.stdout.flush()
sys.stderr.flush()
continue
with open(res_path, "wb") as f:
pkl.dump((res_log, sents), f)
def main(_):
wn.ensure_loaded()
if not os.path.isdir(flags.cgmh_output_dir):
os.mkdir(flags.cgmh_output_dir)
os.environ['CUDA_VISIBLE_DEVICES'] = flags.gpu
n_gpu = len(flags.gpu.split(","))
numpy.seterr(divide='ignore', invalid='ignore')
with gz.open(flags.atk_data_path, "rb") as f:
bb_atk_data = pkl.load(f)
bb_atk_data_list = []
bb_data_chunk_size = int(numpy.ceil(len(bb_atk_data['raw'])/n_gpu/flags.models_per_gpu))
for i in range(n_gpu*flags.models_per_gpu-1):
tmp_bb_atk_data = {}
for k in bb_atk_data.keys():
tmp_bb_atk_data[k] = bb_atk_data[k][int(i*bb_data_chunk_size) \
:int((i+1)*bb_data_chunk_size)]
bb_atk_data_list.append(tmp_bb_atk_data)
tmp_bb_atk_data = {}
for k in bb_atk_data.keys():
tmp_bb_atk_data[k] = bb_atk_data[k][int((n_gpu*flags.models_per_gpu-1)*bb_data_chunk_size):]
bb_atk_data_list.append(tmp_bb_atk_data)
print ("ATTACK DATA LOADED!")
for i in range(n_gpu*flags.models_per_gpu):
print ("\tchunk %d, size = %d" % (i, len(bb_atk_data_list[i]["raw"])),
list(bb_atk_data_list[i].keys()))
flush()
bb_path = flags.imdb_root_path # Root dir of the black box model
bb_data = pd.read_pickle(os.path.join(bb_path, "data/test_df_file"))
with open(os.path.join(bb_path, "data/emb_matrix"), "rb") as f:
bb_embed_matrix, bb_word2idx, bb_idx2word = pkl.load(f)
(bb_max_seqlen, ) = bb_data['text'][0].shape
vocab = dataset.Dictionary(dict_path=flags.lm_vocab_data,
dict_size=flags.vocab_size)
punkts = []
for i in string.punctuation:
if not vocab.get_vocab_idx(i) == vocab.get_unk_idx():
punkts.append(vocab.get_vocab_idx(i))
negations = ["no", "not", "n't", "nt"]
'''
print (bb_atk_data_list[0]['raw'][0])
for i in bb_atk_data_list[0]["x"][0]:
print (bb_idx2word[i], end=" ")
print ()
for i in bb_atk_data_list[0]["x"][0]:
print (vocab.get_vocab(i), end=" ")
print ()
'''
bb_saver = []
bb_list = []
bb_latest_ckpt_file = tf.train.latest_checkpoint(os.path.join(bb_path, flags.model_dir))
for i in range(n_gpu*flags.models_per_gpu):
with tf.device("/device:GPU:"+str(i%n_gpu)):
with tf.variable_scope("IMDB"+str(i)):
bb_list.append(classifier.BiRNNSequenceClassifier(max_seqlen=bb_max_seqlen,
n_class=2,
average_hidden=True,
concat_fw_bw=True,
embed_matrix=bb_embed_matrix,
embed_trainable=False,
opt_type="adam",
lr=1e-3,
cell_type="lstm",
hidden_size=128))
tmp_var_list = {}
for v in tf.global_variables():
if v.name.startswith('IMDB'+str(i)):
tmp_name = v.name
while "IMDB"+str(i) in tmp_name:
tmp_name = tmp_name.strip("IMDB"+str(i)+"/")
tmp_var_list[tmp_name.split(":")[0]] = v
bb_saver.append(tf.train.Saver(var_list=tmp_var_list))
m_list = []
for i in range(n_gpu*flags.models_per_gpu):
with tf.device("/device:GPU:"+str(i)):
with tf.variable_scope("MH"+str(i)):
m_list.append(mh.MetropolisHastings(seq_max_len=flags.seq_max_len,
embed_w=flags.embed_width,
vocab_size=vocab.get_dict_size(),
n_layer=flags.n_rnn_layer,
n_hidden=flags.n_rnn_cell,
keep_prob=flags.keep_prob,
lr=flags.learning_rate,
n_gpu=[i%n_gpu],
grad_clip=flags.grad_clip,
init_idx=vocab.get_init_idx(),
is_training=False,
punkt_idx=punkts,
scope_name="MH"+str(i)))
init_op = tf.global_variables_initializer()
configs = tf.ConfigProto(allow_soft_placement=True)
configs.gpu_options.allow_growth = True
sess = tf.Session(config=configs)
sess.run(init_op)
for tmp_m in m_list:
tmp_m.load(sess,
forward_model_save_path=flags.forward_model,
backward_model_save_path=flags.backward_model)
for i in bb_saver:
i.restore(sess, bb_latest_ckpt_file)
print ("MODELS LOADED!")
for i in range(n_gpu*flags.models_per_gpu):
print ("\t", m_list[i])
print ("\t", bb_list[i])
flush()
threads = []
for i in range(n_gpu*flags.models_per_gpu):
threads.append(multi_thread_cgmh(bb_atk_data=bb_atk_data_list[i],
bb_w2i=bb_word2idx,
bb_i2w=bb_idx2word,
vocab=vocab,
bb=bb_list[i],
m=m_list[i],
sess=sess,
index=i,
bb_max_seqlen=400,
negs=negations))
print ("THREADS BUILT!")
flush()
for i in range(len(threads)):
print ("\t%d"%i, threads[i])
for i in threads:
i.start()
flush()
for i in threads:
i.join()
if __name__ == "__main__":
tf.app.run(main=main)