def main(): i_file_output = 0 print "load model..." generator = GEN(AUTHER_NUM, EMB_DIM, lamda=0.0 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=FLAGS.init_lr_gen, lr_decay_step=FLAGS.lr_decay_iter_gen) discriminator = DIS(AUTHER_NUM, EMB_DIM, lamda=0.01 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=FLAGS.init_lr_dis, lr_decay_step=FLAGS.lr_decay_iter_dis) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) dis_log = open(outputdir + 'dis_log.txt', 'w') gen_log = open(outputdir + 'gen_log.txt', 'w') # minimax training best_gen = 0. best_dis = 0. draw_count_D = 0 draw_count_G = 0 for epoch in range(FLAGS.epochs): #5000 if epoch >= 0: # Train D generate_for_d(sess, generator, DIS_TRAIN_FILE) train_size = ut.file_len(DIS_TRAIN_FILE) # generate file length for d_epoch in range(5): index = 1 while True: if index > train_size: break if index + BATCH_SIZE <= train_size + 1: input_auther, input_coauther_real, input_coauther_fake = ut.get_batch_data( DIS_TRAIN_FILE, index, BATCH_SIZE) else: input_auther, input_coauther_real, input_coauther_fake = ut.get_batch_data( DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE _ = sess.run( [discriminator.d_updates, discriminator.clip_D], feed_dict={ discriminator.auther: input_auther, discriminator.co_real: input_coauther_real, discriminator.co_fake: input_coauther_fake }) result = simple_test(sess, discriminator) buf = '\t'.join([str(x) for x in result]) dis_log.write(str(epoch) + '\t' + buf + '\n') dis_log.flush() p_5 = result[2] if p_5 > best_dis: print 'best_dis: ', epoch, result best_dis = p_5 discriminator.save_model(sess, outputdir + "gan_discriminator.pkl") # Train G for g_epoch in range(1): for u in auther_pos_train: sample_lambda = 0.2 pos = list(set(auther_pos_train[u])) sample_times = 128 rating = sess.run(generator.softmax_logits, {generator.auther: [u]}) prob = np.reshape(rating, [-1]) sample = np.random.choice(np.arange(AUTHER_NUM), size=sample_times, p=prob) ########################################################################### # Get reward and adapt it with importance sampling ########################################################################### reward = sess.run( discriminator.reward, { discriminator.auther: np.tile(u, (sample_times)), discriminator.co_fake: sample }) ########################################################################### # Update G ########################################################################### _ = sess.run( generator.gan_updates, { generator.auther: np.tile(u, (sample_times)), generator.co: sample, generator.reward: reward }) result = simple_test(sess, generator) buf = '\t'.join([str(x) for x in result]) gen_log.write(str(epoch) + '\t' + buf + '\n') gen_log.flush() p_5 = result[2] if p_5 > best_gen: print 'best_gen: ', epoch, result best_gen = p_5 generator.save_model(sess, outputdir + "gan_generator.pkl") draw_count_G += 1 gen_log.close() dis_log.close()
def main(): print "load model..." param = cPickle.load(open(workdir + "model_dns_ori.pkl")) #载入模型参数 #参数恢复到模型中 generator = GEN(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.0 / BATCH_SIZE, param=param, initdelta=INIT_DELTA, learning_rate=0.001) discriminator = DIS(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.1 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=0.001) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) #启动图 sess.run(tf.global_variables_initializer()) #初始化 print "gen ", simple_test(sess, generator) print "dis ", simple_test(sess, discriminator) dis_log = open(workdir + 'dis_log.txt', 'w') gen_log = open(workdir + 'gen_log.txt', 'w') # minimax training,G和D交叉训练 best = 0. for epoch in range(15): if epoch >= 0: for d_epoch in range(100): if d_epoch % 5 == 0: #每隔5轮就产生新样本 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_user, input_item, input_label = ut.get_batch_data( DIS_TRAIN_FILE, index, BATCH_SIZE) else: #最后一个批次数目不够的数据 input_user, input_item, input_label = ut.get_batch_data( DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.u: input_user, discriminator.i: input_item, discriminator.label: input_label }) # Train G for g_epoch in range(50): # 50 for u in user_pos_train: sample_lambda = 0.2 pos = user_pos_train[u] rating = sess.run(generator.all_logits, {generator.u: u}) exp_rating = np.exp(rating) prob = exp_rating / np.sum(exp_rating) # 生成器的概率分布 p_\theta pn = (1 - sample_lambda) * prob pn[pos] += sample_lambda * 1.0 / len(pos) # Now, pn is the Pn in importance sampling, prob is generator distribution p_\theta sample = np.random.choice(np.arange(ITEM_NUM), 2 * len(pos), p=pn) #根据分布采样 ########################################################################### #通过判别器D得到对应的reward ########################################################################### reward = sess.run(discriminator.reward, { discriminator.u: u, discriminator.i: sample }) reward = reward * prob[sample] / pn[sample] ########################################################################### #更新G的参数 ########################################################################### _ = sess.run( generator.gan_updates, { generator.u: u, generator.i: sample, generator.reward: reward }) result = simple_test(sess, generator) #测试结果 print "epoch ", epoch, "gen: ", result buf = '\t'.join([str(x) for x in result]) gen_log.write(str(epoch) + '\t' + buf + '\n') #记录 gen_log.flush() #刷新 p_5 = result[1] if p_5 > best: #记录最佳 print 'best: ', result best = p_5 generator.save_model(sess, "ml-100k/gan_generator.pkl") gen_log.close() dis_log.close()
def main(): print("load initial model ...") #param_old = pickle.load(open(GAN_MODEL_BEST_FILE_OLD, 'rb+'), encoding="iso-8859-1") generator = GEN(FEATURE_SIZE, G_WEIGHT_DECAY, G_LEARNING_RATE, LEN_UNSIGNED, param=None) print('Gen Done!!!') discriminator = DIS(FEATURE_SIZE, D_WEIGHT_DECAY, D_LEARNING_RATE, LEN_GAN, param=None) print("DIS Done!!!") config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) print(GAN_MODEL_BEST_FILE) G_map_best = 0 Test_map_best = 0 for epoch in range(10): print("epoch" + str(epoch)) # 从PRED_SIZE中随机抽取QUERY_TRAIN_SIZE个样本作为query random_query_D_feature = [] random_query_D_label = [] generated_data = [] neg_data = [] for index_query in range(0, QUERY_TRAIN_SIZE): if index_query % 10 == 0 or index_query == QUERY_TRAIN_SIZE: print("random_query_from_G_for_D " + str(index_query)) # 随机生成query序号 query = random.randint(0, TRAIN_SIZE - 1) random_query_D_feature.append(query_train_feature[query]) random_query_D_label.append(query_train_label[query]) current_query_feature = [] current_query_feature.append(query_train_feature[query]) current_query_feature = np.asarray(current_query_feature) # 针对每一个query,计算dataset的得分以及根据softmax对dataset排序 pred_list_score = sess.run(generator.pred_score, feed_dict={ generator.query_data: current_query_feature, generator.pred_data: pred_feature }) exp_rating = np.exp(pred_list_score) prob = exp_rating / np.sum(exp_rating) sortlist = combine(unsigned_list_pred_index, prob) sortlist.sort(key=lambda x: x[1], reverse=True) # 取排名的前LEN_GAN个加入generated_data query序号 + dataset图片序号 + dataset特征 for i in range(0, LEN_GAN): generated_data.append((index_query, sortlist[i][0], pred_feature[int(sortlist[i][0])])) for j in range(PRED_SIZE - LEN_GAN, PRED_SIZE): neg_data.append((index_query, sortlist[j][0], pred_feature[int(sortlist[j][0])])) # Train D print('Training D ...') for d_epoch in range(10): print('d_epoch' + str(d_epoch)) for index_query in range(0, QUERY_TRAIN_SIZE): #每次获取QUERY_TRAIN_SIZE个query的特征 input_query = [] input_query.append(random_query_D_feature[index_query]) #从generated_data中读取排好序的特征 input_gan = [] input_neg = [] for index_gan in range(0, LEN_GAN): input_gan.append(generated_data[index_query * LEN_GAN + index_gan][2]) for index_gan in range(0, LEN_GAN): input_neg.append(neg_data[index_query * LEN_GAN + index_gan][2]) _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.query_data: input_query, discriminator.gan_data: input_gan, discriminator.neg_data: input_neg }) #测试判别器参数好坏 D_map = MAP(sess, discriminator, random_query_D_feature, random_query_D_label, pred_label, generated_data, QUERY_TRAIN_SIZE, LEN_GAN) print("map:", "map_D", D_map) # Train G print('Training G ...') number_index = np.random.permutation(TRAIN_SIZE) number = 0 for g_epoch in range(10): print('g_epoch' + str(g_epoch)) #从PRED_SIZE中随机抽取QUERY_TRAIN_SIZE个样本作为query random_query_G_feature = [] random_query_G_label = [] generated_data = [] neg_data = [] for index_query in range(0, QUERY_TRAIN_SIZE): if index_query % 10 == 0 or index_query == QUERY_TRAIN_SIZE: print("random_query_from_G_for_G " + str(index_query)) # 随机生成query序号 if number == TRAIN_SIZE - 1: number = 0 query = number_index[number] number = number + 1 random_query_G_feature.append(query_train_feature[query]) random_query_G_label.append(query_train_label[query]) current_query_feature_un = [] current_query_feature_un.append(query_train_feature[query]) current_query_feature_un = np.asarray(current_query_feature_un) #针对每一个query,计算dataset的得分以及根据softmax对dataset排序 pred_list_score = sess.run(generator.pred_score, feed_dict={ generator.query_data: current_query_feature_un, generator.pred_data: pred_feature }) exp_rating = np.exp(pred_list_score) prob = exp_rating / np.sum(exp_rating) sortlist = combine(unsigned_list_pred_index, prob) sortlist.sort(key=lambda x: x[1], reverse=True) # 取排名的前LEN_GAN个加入generated_data query序号 + dataset图片序号 + dataset特征 for i in range(0, LEN_GAN): generated_data.append((index_query, sortlist[i][0], pred_feature[int(sortlist[i][0])])) for j in range(PRED_SIZE - LEN_GAN, PRED_SIZE): neg_data.append((index_query, sortlist[j][0], pred_feature[int(sortlist[j][0])])) #获取根据query检索出来的图库中的图片特征 gan_list_feature = [] neg_list_feature = [] for index_gan in range(0, LEN_GAN): gan_list_feature.append( generated_data[index_query * LEN_GAN + index_gan][2]) gan_list_feature = np.asarray(gan_list_feature) for index_gan in range(0, LEN_GAN): neg_list_feature.append(neg_data[index_query * LEN_GAN + index_gan][2]) neg_list_feature = np.asarray(neg_list_feature) #根据生成的GAN序列和query的特征进行生成reward gan_reward = sess.run(discriminator.reward, feed_dict={ discriminator.query_data: current_query_feature_un, discriminator.gan_data: gan_list_feature, discriminator.neg_data: neg_list_feature }) gan_index = np.random.choice(np.arange( len(unsigned_list_pred_index)), size=LEN_GAN, p=prob) _ = sess.run(generator.gan_updates, feed_dict={ generator.query_data: current_query_feature_un, generator.pred_data: pred_feature, generator.sample_index: gan_index, generator.reward: gan_reward, }) G_map = MAP_G(sess, generator, random_query_D_feature, random_query_G_label, pred_label, generated_data, QUERY_TRAIN_SIZE, LEN_GAN) if G_map > G_map_best: G_map_best = G_map print("Best_G_map:", "map_G", G_map) print("map:", "map_G", G_map) Test_map = MAP_test(sess, generator, query_test_feature, query_test_label, pred_label, pred_feature, QUERY_TEST_SIZE, LEN_UNSIGNED, LEN_GAN) if Test_map > Test_map_best: Test_map_best = Test_map generator.save_model(sess, GAN_MODEL_BEST_FILE) print("Best_Test_map:", "map_Test", Test_map) print("map:", "map_Test", Test_map) # test param_best = pickle.load(open(GAN_MODEL_BEST_FILE, 'rb+'), encoding="iso-8859-1") assert param_best is not None generator_best = GEN(FEATURE_SIZE, G_WEIGHT_DECAY, G_LEARNING_RATE, LEN_UNSIGNED, param=param_best) sess = tf.Session(config=config) sess.run(tf.initialize_all_variables()) map_best = MAP_test(sess, generator_best, query_test_feature, query_test_label, pred_label, pred_feature, QUERY_TEST_SIZE, LEN_UNSIGNED, LEN_GAN) print("GAN_Best MAP ", map_best) sess.close()
def main(): print("load model...") #param = pickle.load(open(workdir + "model_dns_ori.pkl")) #.pkl是python 用来保存文件的 with open(workdir + "model_dns_ori.pkl", 'rb') as data_file: param = pickle.load(data_file, encoding='bytes') #param = cPickle.load(open(workdir + "model_dns_ori.pkl")) #with open(workdir + "model_dns_ori.pkl",'rb') as data_file: #param = pickle.load(data_file,encoding='bytes') print(param) generator = GEN(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.0 / BATCH_SIZE, param=param, initdelta=INIT_DELTA, learning_rate=0.001) discriminator = DIS(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.1 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=0.001) config = tf.ConfigProto( ) # 一般用在创建session的时候。用来对session进行参数配置,配置session运行参数&&GPU设备指定 config.gpu_options.allow_growth = True ## 使用allow_growth option,刚一开始分配少量的GPU容量,然后按需慢慢的增加,由于不会释放 #内存,所以会导致碎片 sess = tf.Session( config=config ) # 要运行刚才定义的三个操作中的任何一个,我们需要为Graph创建一个Session。 Session还将分配内存来存储变量的当前值 sess.run(tf.global_variables_initializer()) print("gen ", simple_test(sess, generator)) print("dis ", simple_test(sess, discriminator)) dis_log = open(workdir + 'dis_log.txt', 'w') gen_log = open(workdir + 'gen_log.txt', 'w') # minimax training best = 0. for epoch in range(15): if epoch >= 0: for d_epoch in range(100): if d_epoch % 5 == 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_user, input_item, input_label = ut.get_batch_data( DIS_TRAIN_FILE, index, BATCH_SIZE) else: input_user, input_item, input_label = ut.get_batch_data( DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.u: input_user, discriminator.i: input_item, discriminator.label: input_label }) # Train G for g_epoch in range(50): # 50 for u in user_pos_train: sample_lambda = 0.2 pos = user_pos_train[u] rating = sess.run(generator.all_logits, {generator.u: u}) exp_rating = np.exp(rating) prob = exp_rating / np.sum( exp_rating) # prob is generator distribution p_\theta pn = (1 - sample_lambda) * prob pn[pos] += sample_lambda * 1.0 / len(pos) # Now, pn is the Pn in importance sampling, prob is generator distribution p_\theta sample = np.random.choice(np.arange(ITEM_NUM), 2 * len(pos), p=pn) ########################################################################### # Get reward and adapt it with importance sampling ########################################################################### reward = sess.run(discriminator.reward, { discriminator.u: u, discriminator.i: sample }) reward = reward * prob[sample] / pn[sample] ########################################################################### # Update G ########################################################################### _ = sess.run( generator.gan_updates, { generator.u: u, generator.i: sample, generator.reward: reward }) result = simple_test(sess, generator) print("epoch ", epoch, "gen: ", result) buf = '\t'.join([str(x) for x in result]) gen_log.write(str(epoch) + '\t' + buf + '\n') gen_log.flush() p_5 = result[1] if p_5 > best: print('best: ', result) best = p_5 generator.save_model(sess, "ml-100k/gan_generator.pkl") gen_log.close() dis_log.close()
def main(): print("load model...") param = None #param = pickle.load(open(workdir + "model_dns_ori.pkl")) #param = cPickle.load(open(workdir + "model_dns_ori.pkl")) generator = GEN(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.0 / BATCH_SIZE, param=param, initdelta=INIT_DELTA, learning_rate=0.001) discriminator = DIS(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.1 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=0.001) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) # 训练自己的神经网络的时候,无一例外的就是都会加上一句 sess.run(tf.global_variables_initializer()) ,这行代码的官方解释是 初始化模型的参数 print("gen ", simple_test(sess, generator)) print("dis ", simple_test(sess, discriminator)) dis_log = open(workdir + 'dis_log.txt', 'w') gen_log = open(workdir + 'gen_log.txt', 'w') # minimax training best = 0. for epoch in range(15): if epoch >= 0: for d_epoch in range(100): # Train D if d_epoch % 5 == 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_user, input_item, input_label = ut.get_batch_data( DIS_TRAIN_FILE, index, BATCH_SIZE) else: input_user, input_item, input_label = ut.get_batch_data( DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.u: input_user, discriminator.i: input_item, discriminator.label: input_label }) # Train G for g_epoch in range(50): # 50 for u in user_pos_train: sample_lambda = 0.2 pos = user_pos_train[u] rating = sess.run(generator.all_logits, {generator.u: u}) exp_rating = np.exp(rating) prob = exp_rating / np.sum( exp_rating) # prob is generator distribution p_\theta pn = (1 - sample_lambda) * prob pn[pos] += sample_lambda * 1.0 / len(pos) # Now, pn is the Pn in importance sampling, prob is generator distribution p_\theta sample = np.random.choice(np.arange(ITEM_NUM), 2 * len(pos), p=pn) print(len(sample)) ########################################################################### # Get reward and adapt it with importance sampling ########################################################################### reward = sess.run(discriminator.reward, { discriminator.u: u, discriminator.i: sample }) reward = reward * prob[sample] / pn[sample] ########################################################################### # Update G ########################################################################### _ = sess.run( generator.gan_updates, { generator.u: u, generator.i: sample, generator.reward: reward }) result = simple_test(sess, generator) print("epoch ", epoch, "gen: ", result) buf = '\t'.join([str(x) for x in result]) gen_log.write(str(epoch) + '\t' + buf + '\n') gen_log.flush( ) # 用来刷新缓冲区,将缓冲区中的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入 p_5 = result[1] if p_5 > best: print('best: ', result) best = p_5 generator.save_model(sess, "ml-100k/gan_generator.pkl") gen_log.close() dis_log.close()
def main(): print "load model..." param = cPickle.load(open(workdir + "model_dns_ori.pkl")) #载入model_dns_ori.pkl,导入用户项目等参数?可能是.txt文件相同信息的不同格式 generator = GEN(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.0 / BATCH_SIZE, param=param, initdelta=INIT_DELTA, learning_rate=0.001) discriminator = DIS(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.1 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=0.001) #------------------------------------------------------动态申请显存 config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) #初始化 #----------------------------------------------------------- print "gen ", simple_test(sess, generator)#这里不知道在干什么 print "dis ", simple_test(sess, discriminator) dis_log = open(workdir + 'dis_log.txt', 'w') #pkl文件和txt文件的关系不明确 gen_log = open(workdir + 'gen_log.txt', 'w') ##########################################################################################输入和初始化 # minimax training------------------------------------------------------------------------------D部分生成负样本和正样本结合 best = 0. #??? for epoch in range(15): if epoch >= 0: for d_epoch in range(100): if d_epoch % 5 == 0: generate_for_d(sess, generator, DIS_TRAIN_FILE) #根据分布率得到采样文件,根据用户得到item train_size = ut.file_len(DIS_TRAIN_FILE) #从dis-train.txt得到文件数 ############################################################################################搭建模型前让雷一鸣搞清楚这一段的原因 index = 1 while True: if index > train_size: break if index + BATCH_SIZE <= train_size + 1: #这个判别条件的原理不知道 input_user, input_item, input_label = ut.get_batch_data(DIS_TRAIN_FILE, index, BATCH_SIZE) else: input_user, input_item, input_label = ut.get_batch_data(DIS_TRAIN_FILE, index, train_size - index + 1) index += BATCH_SIZE ########################################################################################################### _ = sess.run(discriminator.d_updates, #猜测是不断修改模型的过程 feed_dict={discriminator.u: input_user, discriminator.i: input_item, discriminator.label: input_label}) #给空出来的的placeholder传输值,定义好整个图之后才会用sess.run??把从G中得到的参数给了D ####################### Train G################################policy gradient生成item更新参数######################### for g_epoch in range(50): # 50 for u in user_pos_train: #u训练数据集 sample_lambda = 0.2 pos = user_pos_train[u] #-----------------------------------------------------------------------------很重要,需要明白一下 rating = sess.run(generator.all_logits, {generator.u: u})#{}中是字典型数据,猜测是在所有的猜测数据中检索字典型数据或者仅仅是把这个数据输入到模型中去,放到图中开始运行,为了取回fetch内容,在参数中加入需要输入的数据 exp_rating = np.exp(rating)#计算e指数 prob = exp_rating / np.sum(exp_rating) # prob is generator distribution p_\theta 分布率 pn = (1 - sample_lambda) * prob pn[pos] += sample_lambda * 1.0 / len(pos) # Now, pn is the Pn in importance sampling, prob is generator distribution p_\theta #不明白---------------------------------但很重要####################p和pn####################3 sample = np.random.choice(np.arange(ITEM_NUM), 2 * len(pos), p=pn) #这里应该是选择文档的过程给出item索引,在索引中随机以p这个概率选取2 * len(pos)个 #得到概率并且抽样了 根据用户的输入得到item ########################################################################### # Get reward and adapt it with importance sampling在D中才有reward ########################################################################### reward = sess.run(discriminator.reward, {discriminator.u: u, discriminator.i: sample}) #用户和抽样的项目放入D中计算反馈,sess实体运行图取回括号中的参数 sess.run(fetches,feed_dict),给placeholder创建出来的变量赋值 reward = reward * prob[sample] / pn[sample] ###########################################################################,挑出来的是训练数据中的采样和重点sample # Update G ########################################################################### _ = sess.run(generator.gan_updates, {generator.u: u, generator.i: sample, generator.reward: reward}) result = simple_test(sess, generator) print "epoch ", epoch, "gen: ", result buf = '\t'.join([str(x) for x in result]) #把结果搞到一起了 gen_log.write(str(epoch) + '\t' + buf + '\n') #输出准确率 gen_log.flush() p_5 = result[1] if p_5 > best: print 'best: ', result best = p_5 generator.save_model(sess, "ml-100k/gan_generator.pkl") gen_log.close() dis_log.close()
def main(): print("load initial model ...") #param_old = pickle.load(open(GAN_MODEL_BEST_FILE_OLD, 'rb+'), encoding="iso-8859-1") #generator_best = GEN(FEATURE_SIZE, G_WEIGHT_DECAY, G_LEARNING_RATE, Oxford_LEN_UNSIGNED, param=None) generator = GEN(FEATURE_SIZE, G_WEIGHT_DECAY, G_LEARNING_RATE, param=None) print('Gen Done!!!') discriminator = DIS(FEATURE_SIZE, D_WEIGHT_DECAY, D_LEARNING_RATE, param=None) print("DIS Done!!!") Test_map_best = 0 #config = tf.ConfigProto() #config.gpu_options.per_process_gpu_memory_fraction = 0.4 #config.gpu_options.allow_growth = True #sess = tf.Session(config=config) sess = tf.Session() sess.run(tf.global_variables_initializer()) print(GAN_MODEL_BEST_FILE) for epoch in range(40): print("epoch" + str(epoch)) # 从PRED_SIZE中随机抽取QUERY_TRAIN_SIZE个样本作为query random_query_D_feature = [] #random_query_D_label = [] generated_data = [] for index_query in range(0, QUERY_TRAIN_SIZE): if index_query % 10 == 0 or index_query == QUERY_TRAIN_SIZE: print("random_query_from_G_for_D " + str(index_query)) # 随机生成query序号 query = random.randint(0, TRAIN_SIZE - 1) random_query_D_feature.append(Landmark_query_train_feature[query]) #random_query_D_label.append(query_train_label[query]) current_query_feature = [] current_query_feature.append(Landmark_query_train_feature[query]) current_query_feature = np.asarray(current_query_feature) # 针对每一个query,计算dataset的得分以及根据softmax对dataset排序 pred_list_score = sess.run(generator.pred_score, feed_dict={ generator.query_data: current_query_feature, generator.pred_data: Landmark_pred_feature }) exp_rating = np.exp(pred_list_score) prob = exp_rating / np.sum(exp_rating) sortlist = combine(Landmark_unsigned_list_pred_index, prob) sortlist.sort(key=lambda x: x[1], reverse=True) # 取排名的前LEN_GAN个加入generated_data query序号 + dataset图片序号 + dataset特征 for i in range(0, LEN_GAN): generated_data.append( (index_query, sortlist[i][0], Landmark_pred_feature[int(sortlist[i][0])])) # Train D print('Training D ...') for d_epoch in range(30): dis_train_loss_sum = 0 print('d_epoch' + str(d_epoch)) for index_query in range(0, QUERY_TRAIN_SIZE): input_query = [] input_query.append(random_query_D_feature[index_query]) input_gan = [] for index_gan in range(0, LEN_GAN): input_gan.append(generated_data[index_query * LEN_GAN + index_gan][2]) _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.query_data: input_query, discriminator.gan_data: input_gan }) dis_train_loss = sess.run(discriminator.loss, feed_dict={ discriminator.query_data: input_query, discriminator.gan_data: input_gan }) dis_train_loss_sum += dis_train_loss dis_train_loss_mean = dis_train_loss_sum / QUERY_TRAIN_SIZE print("epoch:", epoch, "/ 40", "d_epoch:", d_epoch, "/ 30", "dis_train_loss_mean:", dis_train_loss_mean) # Train G print('Training G ...') number_index = np.random.permutation(TRAIN_SIZE) number = 0 for g_epoch in range(30): print('g_epoch' + str(g_epoch)) #从PRED_SIZE中随机抽取QUERY_TRAIN_SIZE个样本作为query random_query_G_feature = [] #random_query_G_label=[] generated_data = [] gen_train_loss_sum = 0 for index_query in range(0, QUERY_TRAIN_SIZE): if index_query % 10 == 0 or index_query == QUERY_TRAIN_SIZE: print("random_query_from_G_for_G " + str(index_query)) # 随机生成query序号 if number == TRAIN_SIZE - 1: number = 0 query = number_index[number] number = number + 1 random_query_G_feature.append( Landmark_query_train_feature[query]) #random_query_G_label.append(query_train_label[query]) current_query_feature_un = [] current_query_feature_un.append( Landmark_query_train_feature[query]) current_query_feature_un = np.asarray(current_query_feature_un) #针对每一个query,计算dataset的得分以及根据softmax对dataset排序 pred_list_score = sess.run(generator.pred_score, feed_dict={ generator.query_data: current_query_feature_un, generator.pred_data: Landmark_pred_feature }) exp_rating = np.exp(pred_list_score) prob = exp_rating / np.sum(exp_rating) sortlist = combine(Landmark_unsigned_list_pred_index, prob) sortlist.sort(key=lambda x: x[1], reverse=True) # 取排名的前LEN_GAN个加入generated_data query序号 + dataset图片序号 + dataset特征 for i in range(0, LEN_GAN): generated_data.append( (index_query, sortlist[i][0], Landmark_pred_feature[int(sortlist[i][0])])) #获取根据query检索出来的图库中的图片特征 gan_list_feature = [] for index_gan in range(0, LEN_GAN): gan_list_feature.append( generated_data[index_query * LEN_GAN + index_gan][2]) gan_list_feature = np.asarray(gan_list_feature) #根据生成的GAN序列和query的特征进行生成reward gan_reward = sess.run(discriminator.reward, feed_dict={ discriminator.query_data: current_query_feature_un, discriminator.gan_data: gan_list_feature }) gan_index = np.random.choice(np.arange( len(Landmark_unsigned_list_pred_index)), size=LEN_GAN, p=prob) _ = sess.run(generator.gan_updates, feed_dict={ generator.query_data: current_query_feature_un, generator.pred_data: Landmark_pred_feature, generator.sample_index: gan_index, generator.reward: gan_reward }) gen_train_loss = sess.run(generator.gan_loss, feed_dict={ generator.query_data: current_query_feature_un, generator.pred_data: Landmark_pred_feature, generator.sample_index: gan_index, generator.reward: gan_reward }) gen_train_loss_sum += gen_train_loss gen_train_loss_mean = gen_train_loss_sum / QUERY_TRAIN_SIZE print("epoch:", epoch, "/ 40", "g_epoch:", g_epoch, "/ 30", "gen_train_loss:", gen_train_loss_mean) Test_map = MAP_test(sess, generator, Oxford_query_test_feature, Oxford_query_test_label, Oxford_pred_label, Oxford_pred_feature, QUERY_TEST_SIZE, Oxford_LEN_UNSIGNED, LEN_GAN) print("Test_map:", Test_map) if Test_map > Test_map_best: Test_map_best = Test_map generator.save_model(sess, GAN_MODEL_BEST_FILE) print("Best_Test_map:", Test_map_best) # test # param_best = pickle.load(open(GAN_MODEL_BEST_FILE, 'rb+'), encoding="iso-8859-1") # assert param_best is not None # generator_best = GEN(FEATURE_SIZE, G_WEIGHT_DECAY, G_LEARNING_RATE, LEN_UNSIGNED, param=param_best) # sess = tf.Session(config=config) # sess.run(tf.initialize_all_variables()) # map_best = MAP_test(sess, generator_best, query_test_feature, query_test_label, pred_label, pred_feature, QUERY_TEST_SIZE, LEN_UNSIGNED,LEN_GAN) # print("GAN_Best MAP ", map_best) print("Train End") print(Test_map_best) sess.close()
def main(): print "load model..." param = cPickle.load(open(workdir + "model_dns_ori.pkl")) generator = GEN(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.0 / BATCH_SIZE, param=param, initdelta=INIT_DELTA, learning_rate=0.01) discriminator = DIS(ITEM_NUM, USER_NUM, EMB_DIM, lamda=0.1 / BATCH_SIZE, param=None, initdelta=INIT_DELTA, learning_rate=0.01) config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) print "gen ", simple_test(sess, generator) print "dis ", simple_test(sess, discriminator) dis_log = open(workdir + 'dis_log.txt', 'w') gen_log = open(workdir + 'gen_log.txt', 'w') # minimax training best = 0. for epoch in range(15): if epoch >= 0: for d_epoch in range(2): # if d_epoch % 5 == 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_user, input_item, input_label = ut.get_batch_data(DIS_TRAIN_FILE, index, BATCH_SIZE) # else: # input_user, input_item, input_label = ut.get_batch_data(DIS_TRAIN_FILE, index, # train_size - index + 1) # index += BATCH_SIZE # # _ = sess.run(discriminator.d_updates, # feed_dict={discriminator.u: input_user, discriminator.i: input_item, # discriminator.label: input_label}) if d_epoch % 5 == 0: generate_for_d(sess, generator, DIS_TRAIN_FILE) user_number = {} with open(DIS_TRAIN_FILE) as fin: for line in fin: line = line.split() uid = int(line[0]) v = user_number.get(uid) if v is None: user_number[uid] = 2 else: user_number[uid] = v + 2 index = 1 uid = 0 while True: if uid >= USER_NUM: break else: input_user, input_item, input_label, index_new = ut.get_batch_data( DIS_TRAIN_FILE, index, uid) index = index_new v = user_number.get(uid) if v != None: discriminator.setCurUserNum(user_number[uid]) # dis_objective = sess.run(discriminator.objectiveF, # {discriminator.u: input_user, discriminator.i: input_item, # discriminator.label: input_label}) # print "pre: uid ", uid, "dis ", dis_objective _ = sess.run(discriminator.d_updates, feed_dict={ discriminator.u: input_user, discriminator.i: input_item, discriminator.label: input_label }) # dis_objective = sess.run(discriminator.objectiveF, {discriminator.u: input_user, discriminator.i: input_item, # discriminator.label: input_label}) # print "post: uid ",uid, "dis ", dis_objective uid += 1 # Train G for g_epoch in range(50): # 50 for u in user_pos_train: sample_lambda = 0.2 pos = user_pos_train[u] rating = sess.run(generator.all_logits, {generator.u: u}) exp_rating = np.exp(rating) prob = exp_rating / np.sum( exp_rating) # prob is generator distribution p_\theta pn = (1 - sample_lambda) * prob pos_item = [] for each in pos: pos_item.append(each[0]) pn[pos_item] += sample_lambda * 1.0 / len(pos) # Now, pn is the Pn in importance sampling, prob is generator distribution p_\theta sample = np.random.choice(np.arange(ITEM_NUM), 2 * len(pos), p=pn) rating_sample = [] for each in sample: v = item_rating_dict.get(each) if v is None: rating_sample.append(2.5) else: rating_sample.append(item_rating_dict[each]) ########################################################################### # Get reward and adapt it with importance sampling ########################################################################### dis_objective = sess.run( discriminator.objectiveF, { discriminator.u: u, discriminator.i: sample, discriminator.label: rating_sample }) fui_objective = sess.run( discriminator.fui, { discriminator.u: u, discriminator.i: sample, discriminator.label: rating_sample }) reward = sess.run( discriminator.reward, { discriminator.u: u, discriminator.i: sample, discriminator.label: rating_sample }) reward = reward * prob[sample] / pn[sample] ########################################################################### # Update G ########################################################################### _ = sess.run( generator.gan_updates, { generator.u: u, generator.i: sample, generator.reward: reward }) print "epoch ", epoch, "dis: ", dis_objective # print "epoch ", epoch, "dis: ", fui_objective result = simple_test(sess, generator) print "epoch ", epoch, "gen: ", result buf = '\t'.join([str(x) for x in result]) gen_log.write(str(epoch) + '\t' + buf + '\n') gen_log.flush() p_5 = result[1] if p_5 > best: print 'best: ', result best = p_5 generator.save_model(sess, "ml-100k/gan_generator.pkl") gen_log.close() dis_log.close()