示例#1
0
def main(_):
    print('#label={}'.format(config.num_label))
    gen_t = GEN(flags, is_training=True)
    scope = tf.get_variable_scope()
    scope.reuse_variables()
    gen_v = GEN(flags, is_training=False)

    # train_filename = 'yfcc10k_{}.train.tfrecord'.format(flags.model_name)
    # train_tfrecord = path.join(config.tfrecord_dir, train_filename)
    # valid_filename = 'yfcc10k_{}.valid.tfrecord'.format(flags.model_name)
    # valid_tfrecord = path.join(config.tfrecord_dir, valid_filename)

    data_sources_t = utils.get_data_sources(flags, config.train_file, 500)
    data_sources_v = utils.get_data_sources(flags, config.valid_file, 1)
    ts_list_t = utils.decode_tfrecord(flags, data_sources_t, shuffle=True)
    ts_list_v = utils.decode_tfrecord(flags, data_sources_v, shuffle=False)
    # check_ts_list(ts_list_t)
    # check_ts_list(ts_list_v)
    bt_list_t = utils.generate_batch(ts_list_t, config.train_batch_size)
    bt_list_v = utils.generate_batch(ts_list_v, config.valid_batch_size)
    # check_bt_list(bt_list_t, config.train_batch_size)
    # check_bt_list(bt_list_v, config.valid_batch_size)

    user_bt_t, image_bt_t, text_bt_t, label_bt_t, image_file_bt_t = bt_list_t
    user_bt_v, image_bt_v, text_bt_v, label_bt_v, image_file_bt_v = bt_list_v

    best_hit_v = -np.inf
    init_op = tf.global_variables_initializer()
    start = time.time()
    with tf.Session() as sess:
        writer = tf.summary.FileWriter(config.logs_dir, graph=tf.get_default_graph())
        sess.run(init_op)
        with slim.queues.QueueRunners(sess):
            for batch_t in range(num_batch_t):
                image_np_t, label_np_t = sess.run([image_bt_t, label_bt_t])
                feed_dict = {gen_t.image_ph:image_np_t, gen_t.label_ph:label_np_t}
                _, summary = sess.run([gen_t.train_op, gen_t.summary_op], feed_dict=feed_dict)
                writer.add_summary(summary, batch_t)

                if (batch_t + 1) % int(config.train_data_size / config.train_batch_size) != 0:
                    continue

                hit_v = []
                image_file_v = set()
                for batch_v in range(num_batch_v):
                    image_np_v, label_np_v, image_file_np_v = sess.run([image_bt_v, label_bt_v, image_file_bt_v])
                    feed_dict = {gen_v.image_ph:image_np_v}
                    logit_np_v, = sess.run([gen_v.logits], feed_dict=feed_dict)
                    for image_file in image_file_np_v:
                        image_file_v.add(image_file)
                    hit_bt = compute_hit(logit_np_v, label_np_v, flags.cutoff)
                    hit_v.append(hit_bt)
                hit_v = np.mean(hit_v)

                total_time = time.time() - start
                avg_batch = total_time / (batch_t + 1)
                avg_epoch = avg_batch * (config.train_data_size / config.train_batch_size)
                s = '{0} hit={1:.4f} tot={2:.0f}s avg={3:.0f}s'
                s = s.format(batch_t, hit_v, total_time, avg_epoch)
                print(s)
示例#2
0
def main():
    # print "load model..."
    param = cPickle.load(open(workdir + "gan_generator.pkl"))
    # print param
    # print len(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()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)
    sess.run(tf.global_variables_initializer())
    # result = simple_test(sess, generator)
    model = generator
    user = 1
    user_batch_rating = sess.run(model.all_rating, {model.u: [user]})
    user_rating = user_batch_rating[0]
    N = 10
    simple_test_one_user((user_rating, user), N)
示例#3
0
def main():
    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)

    # 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,
                         Oxford_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, Oxford_LEN_UNSIGNED, LEN_GAN)
    print("GAN_Best MAP ", map_best)
    c = time.ctime()
    print(c)
    sess.close()
示例#4
0
tf.app.flags.DEFINE_string('dis_model_ckpt', None, '')
tf.app.flags.DEFINE_string('gen_model_ckpt', None, '')
tf.app.flags.DEFINE_string('model_name', None, '')

flags = tf.app.flags.FLAGS
flags.num_epochs_per_decay *= flags.num_dis_epoch

train_data_size = utils.get_tn_size(flags.dataset)
num_batch_t = int(flags.num_epoch * train_data_size / config.train_batch_size)
eval_interval = int(train_data_size / config.train_batch_size)
print('tn:\t#batch=%d\neval:\t#interval=%d' % (num_batch_t, eval_interval))

global_step = tf.train.create_global_step()
dis_t = DIS(flags, is_training=True)
gen_t = GEN(flags, is_training=True)
scope = tf.get_variable_scope()
scope.reuse_variables()
dis_v = DIS(flags, is_training=False)
gen_v = GEN(flags, is_training=False)


def gan_dis_sample(label_dat, label_gen):
    # print('{0} {1:.2f}'.format(label_dat.shape, label_dat.sum()))
    # print('{0} {1:.2f}'.format(label_gen.shape, label_gen.sum()))
    sample_np, label_np = [], []
    for batch, (label_d, label_g) in enumerate(zip(label_dat, label_gen)):
        num_sample = np.count_nonzero(label_d)
        # print(batch, label_d.shape, label_g.shape, num_sample)
        num_positive = num_sample * flags.num_positive
        sample_d = np.random.choice(config.num_label, num_positive, p=label_d)
示例#5
0
def main():
    print("loading model...")
    generator = GEN(ITEM_NUM,
                    USER_NUM,
                    EMB_DIM,
                    lamda=0.0 / BATCH_SIZE,
                    param=None,
                    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
    with tf.Session(config=config) as sess:
        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')

        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
                                     })

                for g_epoch in range(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)

                        pn = (1 - sample_lambda) * prob
                        pn[pos] += sample_lambda * 1.0 / len(pos)

                        sample = np.random.choice(np.arange(ITEM_NUM),
                                                  2 * len(pos),
                                                  p=pn)

                        reward = sess.run(discriminator.reward, {
                            discriminator.u: u,
                            discriminator.i: sample
                        })
                        reward = reward * prob[sample] / pn[sample]

                        _ = 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

                gen_log.close()
                dis_log.close()
示例#6
0
def main(_):
    gen_t = GEN(flags, is_training=True)
    scope = tf.get_variable_scope()
    scope.reuse_variables()
    gen_v = GEN(flags, is_training=False)

    tf.summary.scalar(gen_t.learning_rate.name, gen_t.learning_rate)
    tf.summary.scalar(gen_t.pre_loss.name, gen_t.pre_loss)
    summary_op = tf.summary.merge_all()
    init_op = tf.global_variables_initializer()

    for variable in tf.trainable_variables():
        num_params = 1
        for dim in variable.shape:
            num_params *= dim.value
        print('%-50s (%d params)' % (variable.name, num_params))

    data_sources_t = utils.get_data_sources(flags, is_training=True)
    data_sources_v = utils.get_data_sources(flags, is_training=False)
    print('tn: #tfrecord=%d\nvd: #tfrecord=%d' %
          (len(data_sources_t), len(data_sources_v)))

    ts_list_t = utils.decode_tfrecord(flags, data_sources_t, shuffle=True)
    ts_list_v = utils.decode_tfrecord(flags, data_sources_v, shuffle=False)
    bt_list_t = utils.generate_batch(ts_list_t, flags.batch_size)
    bt_list_v = utils.generate_batch(ts_list_v, config.valid_batch_size)
    user_bt_t, image_bt_t, text_bt_t, label_bt_t, file_bt_t = bt_list_t
    user_bt_v, image_bt_v, text_bt_v, label_bt_v, file_bt_v = bt_list_v

    figure_data = []
    best_hit_v = -np.inf
    start = time.time()
    with tf.Session() as sess:
        sess.run(init_op)
        writer = tf.summary.FileWriter(config.logs_dir,
                                       graph=tf.get_default_graph())
        with slim.queues.QueueRunners(sess):
            for batch_t in range(num_batch_t):
                image_np_t, label_np_t = sess.run([image_bt_t, label_bt_t])
                feed_dict = {
                    gen_t.image_ph: image_np_t,
                    gen_t.hard_label_ph: label_np_t
                }
                _, summary = sess.run([gen_t.pre_update, summary_op],
                                      feed_dict=feed_dict)
                writer.add_summary(summary, batch_t)

                batch = batch_t + 1
                remain = (batch * flags.batch_size) % train_data_size
                epoch = (batch * flags.batch_size) // train_data_size
                if remain == 0:
                    pass
                    # print('%d\t%d\t%d' % (epoch, batch, remain))
                elif (train_data_size - remain) < flags.batch_size:
                    epoch = epoch + 1
                    # print('%d\t%d\t%d' % (epoch, batch, remain))
                else:
                    continue
                # if (batch_t + 1) % eval_interval != 0:
                #     continue

                hit_v = []
                for batch_v in range(num_batch_v):
                    image_np_v, label_np_v = sess.run([image_bt_v, label_bt_v])
                    feed_dict = {gen_v.image_ph: image_np_v}
                    logit_np_v, = sess.run([gen_v.logits], feed_dict=feed_dict)
                    hit_bt = metric.compute_hit(logit_np_v, label_np_v,
                                                flags.cutoff)
                    hit_v.append(hit_bt)
                hit_v = np.mean(hit_v)

                figure_data.append((epoch, hit_v, batch_t))

                if hit_v < best_hit_v:
                    continue
                tot_time = time.time() - start
                best_hit_v = hit_v
                print('#%03d curbst=%.4f time=%.0fs' %
                      (epoch, hit_v, tot_time))
                gen_t.saver.save(sess, flags.gen_model_ckpt)
    print('bsthit=%.4f' % (best_hit_v))

    utils.create_if_nonexist(os.path.dirname(flags.gen_figure_data))
    fout = open(flags.gen_figure_data, 'w')
    for epoch, hit_v, batch_t in figure_data:
        fout.write('%d\t%.4f\t%d\n' % (epoch, hit_v, batch_t))
    fout.close()
示例#7
0
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()
示例#8
0
文件: gane.py 项目: zhh0998/GANE
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()
示例#9
0
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()
示例#10
0
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()
示例#11
0
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()
示例#12
0
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()
示例#13
0
def main(_):
    global_step = tf.train.create_global_step()
    gen_t = GEN(flags, is_training=True)
    scope = tf.get_variable_scope()
    scope.reuse_variables()
    gen_v = GEN(flags, is_training=False)

    for variable in tf.trainable_variables():
        num_params = 1
        for dim in variable.shape:
            num_params *= dim.value
        print('{}\t({} params)'.format(variable.name, num_params))

    data_sources_t = utils.get_data_sources(flags, is_training=True)
    data_sources_v = utils.get_data_sources(flags, is_training=False)
    print('tn: #tfrecord=%d\nvd: #tfrecord=%d' %
          (len(data_sources_t), len(data_sources_v)))

    ts_list_t = utils.decode_tfrecord(flags, data_sources_t, shuffle=True)
    ts_list_v = utils.decode_tfrecord(flags, data_sources_v, shuffle=False)
    bt_list_t = utils.generate_batch(ts_list_t, config.train_batch_size)
    bt_list_v = utils.generate_batch(ts_list_v, config.valid_batch_size)
    user_bt_t, image_bt_t, text_bt_t, label_bt_t, file_bt_t = bt_list_t
    user_bt_v, image_bt_v, text_bt_v, label_bt_v, file_bt_v = bt_list_v

    tf.summary.scalar('learning_rate', gen_t.learning_rate)
    tf.summary.scalar('pre_loss', gen_t.pre_loss)
    summary_op = tf.summary.merge_all()

    start = time.time()
    best_hit_v = -np.inf
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init_op)
        writer = tf.summary.FileWriter(config.logs_dir,
                                       graph=tf.get_default_graph())
        with slim.queues.QueueRunners(sess):
            for batch_t in range(num_batch_t):
                image_np_t, label_np_t = sess.run([image_bt_t, label_bt_t])
                feed_dict = {
                    gen_t.image_ph: image_np_t,
                    gen_t.label_ph: label_np_t
                }
                _, summary = sess.run([gen_t.train_op, summary_op],
                                      feed_dict=feed_dict)
                writer.add_summary(summary, batch_t)

                if (batch_t + 1) % eval_interval != 0:
                    continue

                hit_v = []
                for batch_v in range(num_batch_v):
                    image_np_v, label_np_v = sess.run([image_bt_v, label_bt_v])
                    feed_dict = {gen_v.image_ph: image_np_v}
                    logit_np_v, = sess.run([gen_v.logits], feed_dict=feed_dict)
                    hit_bt = metric.compute_hit(logit_np_v, label_np_v,
                                                flags.cutoff)
                    hit_v.append(hit_bt)
                hit_v = np.mean(hit_v)

                tot_time = time.time() - start
                print('#{0} hit={1:.4f} {2:.0f}s'.format(
                    batch_t, hit_v, tot_time))

                if hit_v < best_hit_v:
                    continue
                best_hit_v = hit_v
                gen_t.saver.save(sess, flags.gen_model_ckpt)
    print('best hit={0:.4f}'.format(best_hit_v))
示例#14
0
        for i in range(len(pos)):
            data.append(str(u) + '\t' + str(pos[i]) + '\t' + str(neg[i]))

    with open(filename, 'w') as fout:
        fout.write('\n'.join(data))


print("load model...")

print(USER_NUM, ITEM_NUM)
# param: param = [user_matrix, item_matrix, bias]    matrix is constructed from graph
param = pickle.load(open('embeddings_' + ratio + '.pkl', 'rb'))
generator = GEN(ITEM_NUM,
                USER_NUM,
                EMB_DIM,
                lamda=0.0 / BATCH_SIZE,
                param=param,
                initdelta=INIT_DELTA,
                learning_rate=0.002)
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())
示例#15
0
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()
示例#16
0
                                      train_size=flags.train_size,
                                      valid_size=flags.valid_size,
                                      reshape=True)
gen_datagen = AffineGenerator(gen_mnist)

tn_size = int(
    (dis_mnist.train.num_examples + gen_mnist.train.num_examples) / 2)
vd_size = int((dis_mnist.test.num_examples + gen_mnist.test.num_examples) / 2)
print('tn size=%d vd size=%d' % (tn_size, vd_size))
tn_num_batch = int(flags.num_epoch * tn_size / flags.batch_size)
print('tn #batch=%d' % (tn_num_batch))
eval_interval = int(tn_size / flags.batch_size)
print('ev #interval=%d' % (eval_interval))

tn_dis = DIS(flags, dis_mnist.train, is_training=True)
tn_gen = GEN(flags, gen_mnist.train, is_training=True)
dis_summary_op = tf.summary.merge([
    tf.summary.scalar(tn_dis.learning_rate.name, tn_dis.learning_rate),
    tf.summary.scalar(tn_dis.gan_loss.name, tn_dis.gan_loss),
])
gen_summary_op = tf.summary.merge([
    tf.summary.scalar(tn_gen.learning_rate.name, tn_gen.learning_rate),
    tf.summary.scalar(tn_gen.gan_loss.name, tn_gen.gan_loss),
])
init_op = tf.global_variables_initializer()

scope = tf.get_variable_scope()
scope.reuse_variables()
vd_dis = DIS(flags, dis_mnist.test, is_training=False)
vd_gen = GEN(flags, gen_mnist.test, is_training=False)
示例#17
0
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()
示例#18
0
import math
import os
import pickle
import time
import numpy as np
import tensorflow as tf
from os import path
from tensorflow.contrib import slim

tn_size = utils.get_tn_size(flags.dataset)
eval_interval = int(tn_size / flags.batch_size)
print('#tn_size=%d' % (tn_size))

tn_dis = DIS(flags, is_training=True)
tn_gen = GEN(flags, is_training=True)
tn_tch = TCH(flags, is_training=True)
scope = tf.get_variable_scope()
scope.reuse_variables()
vd_dis = DIS(flags, is_training=False)
vd_gen = GEN(flags, is_training=False)
vd_tch = TCH(flags, is_training=False)

for variable in tf.trainable_variables():
  num_params = 1
  for dim in variable.shape:
    num_params *= dim.value
  print('%-50s (%d params)' % (variable.name, num_params))

dis_summary_op = tf.summary.merge([
  tf.summary.scalar(tn_dis.learning_rate.name, tn_dis.learning_rate),
示例#19
0
tf.app.flags.DEFINE_float('tch_num_epochs_per_decay', 2.0, '')
tf.app.flags.DEFINE_float('end_learning_rate', 0.0001, '')
tf.app.flags.DEFINE_string('learning_rate_decay_type', 'exponential', 'fixed|polynomial')
flags = tf.app.flags.FLAGS

mnist = data_utils.read_data_sets(flags.dataset_dir,
    one_hot=True,
    train_size=flags.train_size,
    valid_size=flags.valid_size,
    reshape=True)
print('tn size=%d vd size=%d' % (mnist.train.num_examples, mnist.test.num_examples))
tn_num_batch = int(flags.num_epoch * mnist.train.num_examples / flags.batch_size)
print('tn #batch=%d' % (tn_num_batch))

tn_tch = TCH(flags, mnist.train, is_training=True)
tn_gen = GEN(flags, mnist.train, is_training=True)
tn_sl_gen = GEN(flags, mnist.train, is_training=True, gen_scope='sl_gen')
tn_kd_gen = GEN(flags, mnist.train, is_training=True, gen_scope='kd_gen')
scope = tf.get_variable_scope()
scope.reuse_variables()
vd_tch = TCH(flags, mnist.test, is_training=False)
vd_gen = GEN(flags, mnist.test, is_training=False)
vd_sl_gen = GEN(flags, mnist.test, is_training=False, gen_scope='sl_gen')
vd_kd_gen = GEN(flags, mnist.test, is_training=False, gen_scope='kd_gen')

learning_rate_diff = tn_sl_gen.learning_rate - tn_kd_gen.learning_rate
# sl_summary_op = tf.summary.merge([
#   tf.summary.scalar('accuracy', vd_sl_gen.accuracy)
# ])
# kd_summary_op = tf.summary.merge([
#   tf.summary.scalar('accuracy', vd_kd_gen.accuracy)