# try using different optimizers and different optimizer configs model.compile(OPTIMIZER, loss, metrics=['accuracy']) return model import encap_sst_bert_zy as models #embedding_matrix = np.load(('en_aux_files/embeddings_glove_%d.npy' % (VOCAB_SIZE))) #embedding_matrix=embedding_matrix.T #model = bd_lstm(embedding_matrix) #model_path = 'bdlstm_models' #model.load_weights(model_path) #test_y2 = np.array([[0, 1] if t == 1 else [1, 0] for t in test_y]) #all_scores_origin = model.evaluate(test_x, test_y2) #print('all origin test_loss: %f, accuracy: %f' % (all_scores_origin[0], all_scores_origin[1])) model = models.Model(dataset).cuda() ga_atttack = PSOAttack(model,word_candidate, dataset, max_iters=20, pop_size=pop_size) SAMPLE_SIZE = len(dataset.test_y) TEST_SIZE = 1000 test_idx = np.random.choice(len(dataset.test_y), SAMPLE_SIZE, replace=False) test_len = [] for i in range(SAMPLE_SIZE): test_len.append(np.sum(np.sign(test_x[test_idx[i]]))) print('Shortest sentence in our test set is %d words' %np.min(test_len)) test_list = [] orig_list = [] orig_label_list = [] adv_list = [] dist_list = [] adv_orig=[]
with open('./all_seqs.pkl', 'rb') as fh: train, valid, test = pickle.load(fh) with open('pos_tags_test.pkl', 'rb') as fp: test_pos_tags = pickle.load(fp) test_s1 = [t[1:-1] for t in test['s1']] test_s2 = [t[1:-1] for t in test['s2']] model = Model() model.evaluate(test['s1'], test['s2'], test['label']) np.random.seed(3333) vocab = {w: i for (w, i) in tokenizer.word_index.items()} inv_vocab = {i: w for (w, i) in vocab.items()} #test_accuracy = model.evaluate([test[0], test[1]], test[2])[1] #print('\nTest accuracy = ', test_accuracy) adversary = PSOAttack(model, word_candidate, pop_size=60, max_iters=20) print('the length of test cases is:', len(test_s1)) TEST_SIZE = 5000 test_idxs = np.random.choice(len(test_s1), size=TEST_SIZE, replace=False) test_list = [] input_list = [] output_list = [] dist_list = [] test_times = [] success = [] change_list = [] target_list = [] true_label_list = [] success_count = 0 i = 0 while len(test_list) < 1000: