Beispiel #1
0
def main(_):
    hps = HParams(
        nb_users=1083,
        nb_items=9989,
        nb_categories=233,
        topk_categoey=130,
        batch_size=1,
        min_lr=0.0001,
        lr=0.001,
        hidden_size=64,
        enc_timesteps=0,
        mode="train",
        max_grad_norm=5,
    )
    batcher, enc_timesteps = Batcher.from_path(train_data_path, hps)
    hps = hps._replace(enc_timesteps=enc_timesteps)

    if FLAGS.train_or_test == "train":
        _train(hps, batcher)
    if FLAGS.train_or_test == "test":
        _test(hps, 0)
Beispiel #2
0
def main(unused_argv):
    if len(unused_argv
           ) != 1:  # prints a message if you've entered flags incorrectly
        raise Exception("Problem with flags: %s" % unused_argv)

    # choose what level of logging you want
    tf.logging.set_verbosity(tf.logging.INFO)
    tf.logging.info('Starting seq2seq_attention in %s mode...', (FLAGS.mode))

    # Change log_root to FLAGS.log_root/FLAGS.exp_name and create the dir if
    # necessary
    FLAGS.log_root = os.path.join(FLAGS.log_root, FLAGS.exp_name)
    if not os.path.exists(FLAGS.log_root):
        if FLAGS.mode == "train":
            os.makedirs(FLAGS.log_root)
        else:
            raise Exception(
                "Logdir %s doesn't exist. Run in train mode to create it." %
                (FLAGS.log_root))

    vocab = Vocab(FLAGS.vocab_path, FLAGS.vocab_size)  # create a vocabulary

    # If in decode mode, set batch_size = beam_size
    # Reason: in decode mode, we decode one example at a time.
    # On each step, we have beam_size-many hypotheses in the beam, so we need
    # to make a batch of these hypotheses.
    if FLAGS.mode == 'decode':
        FLAGS.batch_size = FLAGS.beam_size

    # If single_pass=True, check we're in decode mode
    if FLAGS.single_pass and FLAGS.mode != 'decode':
        raise Exception(
            "The single_pass flag should only be True in decode mode")

    # Make a namedtuple hps, containing the values of the hyperparameters that
    # the model needs
    hparam_list = [
        'mode', 'lr', 'adagrad_init_acc', 'rand_unif_init_mag',
        'trunc_norm_init_std', 'max_grad_norm', 'hidden_dim', 'emb_dim',
        'batch_size', 'max_dec_steps', 'max_enc_steps', 'coverage',
        'cov_loss_wt', 'pointer_gen', 'min_lr', 'max_abstract_len',
        'min_abstract_len', 'max_article_sents', 'max_section_len',
        'min_section_len', 'use_sections', 'max_article_len', 'max_intro_len',
        'max_conclusion_len', 'max_intro_sents', 'max_conclusion_sents',
        'max_section_sents', 'enc_layers', 'optimizer', 'multi_layer_encoder',
        'num_sections', 'hier', 'phased_lstm', 'output_weight_sharing',
        'use_do', 'do_prob', 'embeddings_path', 'pretrained_embeddings',
        'pubmed', 'num_gpus', 'split_intro', 'temperature'
    ]
    hps_dict = {}
    for key, val in list(FLAGS.__flags.items()):  # for each flag
        if key in hparam_list:  # if it's in the list
            hps_dict[key] = val.value  # add it to the dict
    hps = namedtuple("HParams", list(hps_dict.keys()))(**hps_dict)

    # Create a batcher object that will create minibatches of data
    batcher = Batcher(FLAGS.data_path, vocab, hps, FLAGS.single_pass,
                      FLAGS.article_id_key, FLAGS.article_key,
                      FLAGS.abstract_key, FLAGS.labels_key,
                      FLAGS.section_names_key, FLAGS.sections_key)

    tf.set_random_seed(111)  # a seed value for randomness

    if hps.mode == 'train':
        print("creating model...")
        model = SummarizationModel(hps, vocab, num_gpus=FLAGS.num_gpus)
        setup_training(model, batcher)
    elif hps.mode == 'eval':
        model = SummarizationModel(hps, vocab, num_gpus=FLAGS.num_gpus)
        run_eval(model, batcher, vocab, hps.hier)
    elif hps.mode == 'decode':
        decode_model_hps = hps  # This will be the hyperparameters for the decoder model
        # The model is configured with max_dec_steps=1 because we only ever run
        # one step of the decoder at a time (to do beam search). Note that the
        # batcher is initialized with max_dec_steps equal to e.g. 100 because
        # the batches need to contain the full summaries
        decode_model_hps = hps._replace(max_dec_steps=1)
        model = SummarizationModel(decode_model_hps,
                                   vocab,
                                   num_gpus=FLAGS.num_gpus)
        decoder = BeamSearchDecoder(model, batcher, vocab)
        decoder.decode(
        )  # decode indefinitely (unless single_pass=True, in which case deocde the dataset exactly once)
    else:
        raise ValueError("The 'mode' flag must be one of train/eval/decode")
Beispiel #3
0
def _valid(hps, step):
    # hps = hps._replace(enc_timesteps=40)
    batcher_test, enc_timesteps = Batcher.from_path(valid_data_path, hps)
    hps = hps._replace(mode="train", enc_timesteps=enc_timesteps)
    ckpt = tf.train.get_checkpoint_state(save_dir)

    with tf.Graph().as_default(), tf.Session() as sess:
        with tf.variable_scope("Model"):

            test_model = CategoryModel(hps)
            test_model.build_graph()
            # writer = tf.summary.FileWriter("./graph", sess.graph)
            sess.run(tf.global_variables_initializer())
            saver = tf.train.Saver(tf.global_variables(), max_to_keep=0)
            saver.restore(sess, ckpt.model_checkpoint_path)
            # choose
            # saver.restore(sess, os.path.join(save_dir, "model.ckpt-18"))

            losses = 0
            count = 0
            all_rec = float(0)
            all_pre_5 = float(0)
            all_rec_5 = float(0)
            all_pre_10 = float(0)
            all_rec_10 = float(0)
            all_pre_15 = float(0)
            all_rec_15 = float(0)
            # print("start to test")
            pre_user_state_dict = defaultdict(list)
            for user in range(hps.nb_users):
                state = sess.run(test_model.initial_state)
                batch_user, current_poi, enc_ve_cat, enc_time, real_lenth, target_batch = batcher_test.nextBatch_Cat(
                    user)
                if (enc_ve_cat is not None):
                    loss, topk_cat, final_output = test_model.run_train_step(
                        sess, batch_user, enc_ve_cat, real_lenth, target_batch,
                        state)

                    losses += loss
                    count += 1
                    _, rec_z = compute_pre_rec_one(topk_cat, target_batch,
                                                   hps.topk_categoey)
                    pre_5, rec_5 = compute_pre_rec_one(topk_cat, target_batch,
                                                       5)
                    pre_10, rec_10 = compute_pre_rec_one(
                        topk_cat, target_batch, 10)
                    pre_15, rec_15 = compute_pre_rec_one(
                        topk_cat, target_batch, 15)
                    all_pre_5 += float(pre_5)
                    all_rec_5 += float(rec_5)
                    all_pre_10 += float(pre_10)
                    all_rec_10 += float(rec_10)
                    all_pre_15 += float(pre_15)
                    all_rec_15 += float(rec_15)
                    all_rec += float(rec_z)
                    # pre_user_state_dict[user].append(final_output)

            print(
                "step:{}, valid_loss: {:.5f},valid_pre5:{:.5f}, valid_rec5:{:.5f},valid_pre10:{:.5f}, valid_rec10:{:.5f},valid_pre15:{:.5f}, valid_rec15:{:.5f},all_rec:{:.5f}"
                .format(step, losses / count, all_pre_5 / count,
                        all_rec_5 / count, all_pre_10 / count,
                        all_rec_10 / count, all_pre_15 / count,
                        all_rec_15 / count, all_rec / count))

            checkpoint_path = os.path.join(save_dir, "model.ckpt")
            saver.save(sess, checkpoint_path, global_step=step)
            # _valid(hps,step)
            _test(hps, step)