def main(): discriminator = DIS(FEATURE_SIZE, HIDDEN_SIZE, WEIGHT_DECAY, D_LEARNING_RATE, param=None) generator = GEN(FEATURE_SIZE, HIDDEN_SIZE, WEIGHT_DECAY, G_LEARNING_RATE, temperature=TEMPERATURE, param=None) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.initialize_all_variables()) print('start adversarial training') p_best_val = 0.0 ndcg_best_val = 0.0 for epoch in range(30): if epoch >= 0: # G generate negative for D, then train D print('Training D ...') for d_epoch in range(100): if d_epoch % 30 == 0: generate_for_d(sess, generator, DIS_TRAIN_FILE) train_size = ut.file_len(DIS_TRAIN_FILE) index = 1 while True: if index > train_size: break if index + BATCH_SIZE <= train_size + 1: input_pos, input_neg = ut.get_batch_data( DIS_TRAIN_FILE, index, BATCH_SIZE) else: input_pos, input_neg = ut.get_batch_data( DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE pred_data = [] pred_data.extend(input_pos) pred_data.extend(input_neg) pred_data = np.asarray(pred_data) pred_data_label = [1.0] * len(input_pos) pred_data_label.extend([0.0] * len(input_neg)) pred_data_label = np.asarray(pred_data_label) _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.pred_data: pred_data, discriminator.pred_data_label: pred_data_label }) # Train G print('Training G ...') for g_epoch in range(30): for query in query_pos_train.keys(): pos_list = query_pos_train[query] pos_set = set(pos_list) all_list = query_index_url[query] all_list_feature = [ query_url_feature[query][url] for url in all_list ] all_list_feature = np.asarray(all_list_feature) all_list_score = sess.run( generator.pred_score, {generator.pred_data: all_list_feature}) # softmax for all exp_rating = np.exp(all_list_score - np.max(all_list_score)) prob = exp_rating / np.sum(exp_rating) prob_IS = prob * (1.0 - LAMBDA) for i in range(len(all_list)): if all_list[i] in pos_set: prob_IS[i] += (LAMBDA / (1.0 * len(pos_list))) choose_index = np.random.choice(np.arange(len(all_list)), [5 * len(pos_list)], p=prob_IS) choose_list = np.array(all_list)[choose_index] choose_feature = [ query_url_feature[query][url] for url in choose_list ] choose_IS = np.array(prob)[choose_index] / np.array( prob_IS)[choose_index] choose_index = np.asarray(choose_index) choose_feature = np.asarray(choose_feature) choose_IS = np.asarray(choose_IS) choose_reward = sess.run( discriminator.reward, feed_dict={discriminator.pred_data: choose_feature}) _ = sess.run(generator.g_updates, feed_dict={ generator.pred_data: all_list_feature, generator.sample_index: choose_index, generator.reward: choose_reward, generator.important_sampling: choose_IS }) p_5 = precision_at_k(sess, generator, query_pos_test, query_pos_train, query_url_feature, k=5) ndcg_5 = ndcg_at_k(sess, generator, query_pos_test, query_pos_train, query_url_feature, k=5) if p_5 > p_best_val: p_best_val = p_5 ndcg_best_val = ndcg_5 generator.save_model(sess, GAN_MODEL_BEST_FILE) print("Best:", "gen p@5 ", p_5, "gen ndcg@5 ", ndcg_5) elif p_5 == p_best_val: if ndcg_5 > ndcg_best_val: ndcg_best_val = ndcg_5 generator.save_model(sess, GAN_MODEL_BEST_FILE) print("Best:", "gen p@5 ", p_5, "gen ndcg@5 ", ndcg_5) sess.close() param_best = cPickle.load(open(GAN_MODEL_BEST_FILE)) assert param_best is not None generator_best = GEN(FEATURE_SIZE, HIDDEN_SIZE, WEIGHT_DECAY, G_LEARNING_RATE, temperature=TEMPERATURE, param=param_best) sess = tf.Session(config=config) sess.run(tf.initialize_all_variables()) p_1_best = precision_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=1) p_3_best = precision_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=3) p_5_best = precision_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=5) p_10_best = precision_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=10) ndcg_1_best = ndcg_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=1) ndcg_3_best = ndcg_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=3) ndcg_5_best = ndcg_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=5) ndcg_10_best = ndcg_at_k(sess, generator_best, query_pos_test, query_pos_train, query_url_feature, k=10) map_best = MAP(sess, generator_best, query_pos_test, query_pos_train, query_url_feature) mrr_best = MRR(sess, generator_best, query_pos_test, query_pos_train, query_url_feature) print("Best ", "p@1 ", p_1_best, "p@3 ", p_3_best, "p@5 ", p_5_best, "p@10 ", p_10_best) print("Best ", "ndcg@1 ", ndcg_1_best, "ndcg@3 ", ndcg_3_best, "ndcg@5 ", ndcg_5_best, "p@10 ", ndcg_10_best) print("Best MAP ", map_best) print("Best MRR ", mrr_best)
def main(): with tf.device('/gpu:' + str(GPU_ID)): dis_param = cPickle.load(open(DIS_MODEL_PRETRAIN_FILE)) # gen_param = cPickle.load(open(GEN_MODEL_PRETRAIN_FILE)) discriminator = DIS(IMAGE_DIM, TEXT_DIM, HIDDEN_DIM, OUTPUT_DIM, WEIGHT_DECAY, D_LEARNING_RATE, BETA, GAMMA, param=dis_param) # generator = GEN(IMAGE_DIM, TEXT_DIM, HIDDEN_DIM, OUTPUT_DIM, CLASS_DIM, WEIGHT_DECAY, G_LEARNING_RATE, param = gen_param) # discriminator = DIS(IMAGE_DIM, TEXT_DIM, HIDDEN_DIM, OUTPUT_DIM, WEIGHT_DECAY, D_LEARNING_RATE, BETA, GAMMA, param = None) generator = GEN(IMAGE_DIM, TEXT_DIM, HIDDEN_DIM, OUTPUT_DIM, CLASS_DIM, WEIGHT_DECAY, G_LEARNING_RATE, param=None) config = tf.ConfigProto(allow_soft_placement=True) config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.initialize_all_variables()) print('start adversarial training') map_best_val_gen = 0.0 map_best_val_dis = 0.0 for epoch in range(WHOLE_EPOCH): print('Training D ...') for d_epoch in range(D_EPOCH): print('d_epoch: ' + str(d_epoch)) if d_epoch % GS_EPOCH == 0: print('negative text sampling for d using g ...') dis_train_i2t_list = generate_samples( sess, generator, train_i2t, train_i2t_pos, train_i2t_neg, 'i2t') print('negative image sampling for d using g ...') dis_train_t2i_list = generate_samples( sess, generator, train_t2i, train_t2i_pos, train_t2i_neg, 't2i') discriminator = train_discriminator(sess, discriminator, dis_train_i2t_list, 'i2t') discriminator = train_discriminator(sess, discriminator, dis_train_t2i_list, 't2i') if (d_epoch + 1) % (D_DISPLAY) == 0: i2t_test_map = MAP(sess, discriminator, test_i2t_pos, test_i2t, feature_dict, 'i2t') print('E%d D%d I2T_Test_MAP: %.4f' % (epoch, d_epoch, i2t_test_map)) t2i_test_map = MAP(sess, discriminator, test_t2i_pos, test_t2i, feature_dict, 't2i') print('E%d D%d T2I_Test_MAP: %.4f' % (epoch, d_epoch, t2i_test_map)) with open('record.txt', 'a') as record_file: record_file.write('E%d D%d I2T_Test_MAP: %.4f\n' % (epoch, d_epoch, i2t_test_map)) record_file.write('E%d D%d T2I_Test_MAP: %.4f\n' % (epoch, d_epoch, t2i_test_map)) average_map = 0.5 * (i2t_test_map + t2i_test_map) if average_map > map_best_val_dis: map_best_val_dis = average_map discriminator.save_model(sess, DIS_MODEL_BEST_FILE) discriminator.save_model(sess, DIS_MODEL_NEWEST_FILE) print('Training G ...') for g_epoch in range(G_EPOCH): print('g_epoch: ' + str(g_epoch)) generator = train_generator(sess, generator, discriminator, train_i2t, train_i2t_pos, 'i2t') generator = train_generator(sess, generator, discriminator, train_t2i, train_t2i_pos, 't2i') if (g_epoch + 1) % (G_DISPLAY) == 0: i2t_test_map = MAP(sess, generator, test_i2t_pos, test_i2t, feature_dict, 'i2t') print('E%d G%d I2T_Test_MAP: %.4f' % (epoch, g_epoch, i2t_test_map)) t2i_test_map = MAP(sess, generator, test_t2i_pos, test_t2i, feature_dict, 't2i') print('E%d G%d T2I_Test_MAP: %.4f' % (epoch, g_epoch, t2i_test_map)) with open('record.txt', 'a') as record_file: record_file.write('E%d G%d I2T_Test_MAP: %.4f\n' % (epoch, g_epoch, i2t_test_map)) record_file.write('E%d G%d T2I_Test_MAP: %.4f\n' % (epoch, g_epoch, t2i_test_map)) average_map = 0.5 * (i2t_test_map + t2i_test_map) if average_map > map_best_val_gen: map_best_val_gen = average_map generator.save_model(sess, GEN_MODEL_BEST_FILE) generator.save_model(sess, GEN_MODEL_NEWEST_FILE) sess.close()
def main(): #call discriminator, generator discriminator = DIS(FEATURE_SIZE, HIDDEN_SIZE, WEIGHT_DECAY, D_LEARNING_RATE) generator = GEN(FEATURE_SIZE, HIDDEN_SIZE, WEIGHT_DECAY, G_LEARNING_RATE, temperature=TEMPERATURE) print('start adversarial training') p_best_val = 0.0 ndcg_best_val = 0.0 for epoch in range(30): if epoch >= 0: # G generate negative for D, then train D print('Training D ...') for d_epoch in range(100): if d_epoch % 30 == 0: generate_for_d(generator, DIS_TRAIN_FILE) train_size = ut.file_len(DIS_TRAIN_FILE) index = 1 while True: if index > train_size: break if index + BATCH_SIZE <= train_size + 1: input_pos, input_neg = ut.get_batch_data(DIS_TRAIN_FILE, index, BATCH_SIZE) else: input_pos, input_neg = ut.get_batch_data(DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE pred_data = [] #prepare pos and neg data pred_data.extend(input_pos) pred_data.extend(input_neg) pred_data = np.asarray(pred_data) #prepara pos and neg label pred_data_label = [1.0] * len(input_pos) pred_data_label.extend([0.0] * len(input_neg)) pred_data_label = np.asarray(pred_data_label) #train discriminator.train(pred_data, pred_data_label) # Train G print('Training G ...') for g_epoch in range(10): start_time = time.time() print ('now_ G_epoch : ', str(g_epoch)) for query in query_pos_train.keys(): pos_list = query_pos_train[query] pos_set = set(pos_list) #all url all_list = query_index_url[query] #all feature all_list_feature = [query_url_feature[query][url] for url in all_list] all_list_feature = np.asarray(all_list_feature) # G generate all url prob prob = generator.get_prob(all_list_feature[np.newaxis, :]) prob = prob[0] prob = prob.reshape([-1]) #important sampling, change doc prob prob_IS = prob * (1.0 - LAMBDA) for i in range(len(all_list)): if all_list[i] in pos_set: prob_IS[i] += (LAMBDA / (1.0 * len(pos_list))) # G generate some url (5 * postive doc num) choose_index = np.random.choice(np.arange(len(all_list)), [5 * len(pos_list)], p=prob_IS) #choose url choose_list = np.array(all_list)[choose_index] #choose feature choose_feature = [query_url_feature[query][url] for url in choose_list] #prob / importan sampling prob (loss => prob * reward * prob / importan sampling prob) choose_IS = np.array(prob)[choose_index] / np.array(prob_IS)[choose_index] choose_index = np.asarray(choose_index) choose_feature = np.asarray(choose_feature) choose_IS = np.asarray(choose_IS) #get reward((prob - 0.5) * 2 ) choose_reward = discriminator.get_preresult(choose_feature) #train generator.train(choose_feature[np.newaxis, :], choose_reward.reshape([-1])[np.newaxis, :], choose_IS[np.newaxis, :]) print("train end--- %s seconds ---" % (time.time() - start_time)) p_5 = precision_at_k(generator, query_pos_test, query_pos_train, query_url_feature, k=5) ndcg_5 = ndcg_at_k(generator, query_pos_test, query_pos_train, query_url_feature, k=5) if p_5 > p_best_val: p_best_val = p_5 ndcg_best_val = ndcg_5 generator.save_model(GAN_MODEL_BEST_FILE) print("Best:", "gen p@5 ", p_5, "gen ndcg@5 ", ndcg_5) elif p_5 == p_best_val: if ndcg_5 > ndcg_best_val: ndcg_best_val = ndcg_5 generator.save_model(GAN_MODEL_BEST_FILE) print("Best:", "gen p@5 ", p_5, "gen ndcg@5 ", ndcg_5)