def main(args):
    # loading configurations
    with open(args.config) as f:
        config = yaml.safe_load(f)["configuration"]

    work_space = config["workspace"]
    infer_model_dir = '%s/infer_model' % work_space
    vocab_size = config["embeddings"]["vocab_size"]
    vocab_file = '%s/data/%s-%s' % (work_space, "vocab", vocab_size)

    (is_beam_search, beam_size, batch_size, infer_source_file,
     infer_source_max_length, output_path, gpu_fraction,
     gpu_id) = get_infer_config(config)

    # Set up session
    gpu_fraction = config["training"]["gpu_fraction"]
    gpu_id = config["training"]["gpu_id"]
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
                                visible_device_list=gpu_id)
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False,
                                            gpu_options=gpu_options))

    print("loading model ...")
    #seq2seq_with_attn = tf.saved_model.loader.load(sess, tf.saved_model.tag_constants.SERVING, infer_model_dir)
    # test *.pb
    with tf.gfile.GFile('seq2seq_attn.pb', "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')

    encoder_inputs = sess.graph.get_tensor_by_name(
        'seq2seq_placeholder/encoder_inputs:0')
    encoder_length = sess.graph.get_tensor_by_name(
        'seq2seq_placeholder/encoder_length:0')
    infer_outputs = sess.graph.get_tensor_by_name(
        'seq2seq_decoder/transpose:0')
    print("\tDone.")

    # ##### Inference #####
    # Load data
    print("Loading inference data ...")

    # Load vocabularies.
    vocab_table, reverse_vocab_table = create_vocab_tables(vocab_file)

    src_dataset = prepare_infer_data(infer_source_file,
                                     vocab_table,
                                     max_length=infer_source_max_length)
    print("\tDone.")

    # Inference
    print("Start inferring ...")
    final_result = []

    for ith in range(int(len(src_dataset) / batch_size)):
        start = ith
        end = ith + 1
        batch = get_infer_batch(src_dataset, start, end,
                                infer_source_max_length)
        sentence = token_to_str(batch[0][0], reverse_vocab_table)

        start_time = time.time()

        feed_dict = {encoder_inputs: batch[0], encoder_length: batch[1]}
        result = sess.run([infer_outputs], feed_dict=feed_dict)

        duration = round((time.time() - start_time), 3)
        print("sentence:%s, cost:%s s" % (ith, duration))

        #res = "src:{}\n".format(sentence)
        #if is_beam_search is True:
        #    for idx, i in enumerate(result[0][0]):
        #        reply = token_to_str(i, reverse_vocab_table)
        #        res += "\tpred %s:%s\n" % (idx, reply)
        #    res += "\n"
        #else:
        #    reply = result[0][0]
        #    reply = token_to_str(reply, reverse_vocab_table)
        #    res += "\tpred:%s\n\n" % reply
        #print(res)
        #final_result.append(res)

        reply = token_to_str(result[0][0][0], reverse_vocab_table)
        reply = reply.replace('</s>', '')
        print('{}\t{}'.format(sentence, reply))
        final_result.append('{}\t{}'.format(sentence, reply))

    with open(config["inference"]["output_path"], 'w') as f:
        for i in final_result:
            f.write(i + '\n')
    print("\tDone.")
Example #2
0
def main(args):

    # loading configurations
    with open(args.config) as f:
        config = yaml.safe_load(f)["configuration"]

    # set up workspace
    work_space = config["workspace"]
    tf_board = config["tf_board"]
    setup_workpath(work_space)
    name = config["Name"]

    # Construct or load embeddings
    print("Initializing embeddings ...")
    vocab_size = config["embeddings"]["vocab_size"]
    embed_size = config["embeddings"]["embed_size"]

    vocab_file = '%s/data/%s-%s' % (work_space, "vocab", vocab_size)
    print("\tDone.")

    # Build the model and compute losses
    (enc_num_layers, enc_num_units, enc_cell_type, enc_bidir, attn_num_units,
     dec_num_layers, dec_num_units, dec_cell_type, state_pass, infer_max_iter,
     l2_regularize, learning_rate) = get_model_config(config)

    (train_s_file, train_t_file, dev_s_file, dev_t_file, max_length,
     gpu_fraction, gpu_id, checkpoint_every, max_checkpoints, print_every,
     train_steps, is_beam_search, batch_size,
     beam_size) = get_training_config(config)

    print("Building model architecture ...")
    train_model = Seq2SeqModel(mode='train',
                               model_name=name,
                               vocab_size=vocab_size,
                               embedding_size=embed_size,
                               enc_num_layers=enc_num_layers,
                               enc_num_units=enc_num_units,
                               enc_cell_type=enc_cell_type,
                               enc_bidir=enc_bidir,
                               attn_num_units=attn_num_units,
                               dec_num_layers=dec_num_layers,
                               dec_num_units=dec_num_units,
                               dec_cell_type=dec_cell_type,
                               batch_size=batch_size,
                               beam_search=is_beam_search,
                               beam_size=beam_size,
                               infer_max_iter=infer_max_iter,
                               l2_regularize=l2_regularize,
                               learning_rate=learning_rate,
                               max_to_keep=max_checkpoints)

    print("\tDone.")

    logdir = '%s/nn_models/' % work_space
    restore_from = '%s/nn_models/' % work_space

    is_overwritten_training = logdir != restore_from  # 判断两个文件件是否相同

    # Set up session
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
                                visible_device_list=gpu_id)
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False,
                                            gpu_options=gpu_options))
    init = tf.global_variables_initializer()
    sess.run(init)

    # tensorbord
    train_writer = tf.summary.FileWriter(tf_board + 'train/', sess.graph)
    test_writer = tf.summary.FileWriter(tf_board + 'test/', sess.graph)

    try:
        saved_global_step = load(train_model.saver, sess, restore_from)
        if is_overwritten_training or saved_global_step is None:
            # The first training step will be saved_global_step + 1,
            # therefore we put -1 here for new or overwritten trainings.
            saved_global_step = -1

    except Exception:
        print("Something went wrong while restoring checkpoint. "
              "Training is terminated to avoid the overwriting.")
        raise

    # ##### Training #####
    # Load data
    print("Loading data ...")

    # Load vocabularies.
    if os.path.exists(vocab_file):
        vocab_table, reverse_vocab_table = create_vocab_tables(vocab_file)
    else:
        create_vocab_file(train_s_file, train_t_file, dev_s_file, dev_t_file,
                          vocab_file, vocab_size)
        vocab_table, reverse_vocab_table = create_vocab_tables(vocab_file)

    train_set, dev_set = prepare_train_dev_data(train_s_file, train_t_file,
                                                dev_s_file, dev_t_file,
                                                vocab_table, max_length)

    # Training
    last_saved_step = saved_global_step
    num_steps = saved_global_step + train_steps
    losses = []
    steps = []

    print("Start training ...")
    try:
        for step in range(saved_global_step + 1, num_steps):
            start_time = time.time()

            batch = get_train_batch(train_set, max_length, batch_size)

            loss_value = train_model.train(sess, batch)

            losses.append(loss_value)
            duration = (time.time() - start_time)
            if step % print_every == 0 and step != 0:
                # train perplexity
                t_perp = train_model.compute_perplexity(sess, batch)
                add_summary(train_writer, step, 'train perplexity', t_perp)

                # eval perplexity
                dev_str = ""
                if dev_set is not None:
                    eval_batch = get_train_batch(dev_set, max_length,
                                                 batch_size)

                    eval_perp = train_model.compute_perplexity(
                        sess, eval_batch)
                    add_summary(test_writer, step, 'eval perplexity',
                                eval_perp)
                    dev_str += "val_prep: {:.3f}\n".format(eval_perp)

                steps.append(step)
                info = 'step {:d}, loss = {:.6f},perp: {:.3f}\n{}({:.3f} sec/step)'
                print(info.format(step, loss_value, t_perp, dev_str, duration))

            if step % checkpoint_every == 0:
                save(train_model.saver, sess, logdir, step)
                last_saved_step = step

    except KeyboardInterrupt:
        # Introduce a line break after ^C so save message is on its own line.
        print()

    finally:
        if step > last_saved_step:
            save(train_model.saver, sess, logdir, step)
Example #3
0
def main(args):
    # loading configurations
    with open(args.config) as f:
        config = yaml.safe_load(f)["configuration"]

    work_space = config["workspace"]
    infer_model_dir = '%s/infer_model' % work_space
    vocab_size = config["embeddings"]["vocab_size"]
    vocab_file = '%s/data/%s-%s' % (work_space, "vocab", vocab_size)

    (is_beam_search, beam_size, batch_size, infer_source_file,
     infer_target_file, infer_emotion_category_file, infer_source_max_length,
     output_path, output_choice_path, gpu_fraction,
     gpu_id) = get_ecm_infer_config(config)

    # Set up session
    gpu_fraction = config["training"]["gpu_fraction"]
    gpu_id = config["training"]["gpu_id"]
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
                                visible_device_list=gpu_id)
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False,
                                            gpu_options=gpu_options))

    print("loading model ...")
    ecm_model = tf.saved_model.loader.load(
        sess, tf.saved_model.tag_constants.SERVING, infer_model_dir)
    # print('global_variables:\n')
    # glob_var = tf.global_variables()
    # pprint(glob_var)

    # print('all nodes:')
    # all_nodes = [n.name for n in tf.get_default_graph().as_graph_def().node]
    # pprint(all_nodes)

    encoder_inputs = sess.graph.get_tensor_by_name(
        'seq2seq_placeholder/encoder_inputs:0')
    encoder_length = sess.graph.get_tensor_by_name(
        'seq2seq_placeholder/encoder_length:0')
    emo_cat = sess.graph.get_tensor_by_name(
        'seq2seq_placeholder/emotion_category:0')
    infer_outputs = sess.graph.get_tensor_by_name(
        'seq2seq_decoder/infer_outputs:0')
    print("\tDone.")

    # ##### Inference #####
    # Load data
    print("Loading inference data ...")

    # Load vocabularies.
    vocab_table, reverse_vocab_table = create_vocab_tables(vocab_file)

    src_dataset = prepare_ecm_infer_data(infer_source_file,
                                         infer_emotion_category_file,
                                         vocab_table,
                                         max_length=infer_source_max_length)
    print("\tDone.")

    # Inference
    print("Start inferring ...")
    final_result = []

    for ith in range(int(len(src_dataset) / batch_size)):
        start = ith
        end = ith + 1
        batch = get_ecm_infer_batch(src_dataset, start, end,
                                    infer_source_max_length)

        sentence = token_to_str(batch[0][0], reverse_vocab_table)
        # target = token_to_str(tgt_dataset[ith], reverse_vocab_table)
        emo_category = p_map[batch[2][0]]

        start_time = time.time()

        feed_dict = {
            encoder_inputs: batch[0],
            encoder_length: batch[1],
            emo_cat: batch[2]
        }
        result = sess.run([infer_outputs], feed_dict=feed_dict)

        duration = round((time.time() - start_time), 3)
        print("sentence:%s, cost:%s s" % (ith, duration))

        # res = "src:{}  emotion:{}\ntgt:{}\n".format(sentence, emo_category, target)
        res = "src:{}  emotion:{}\n".format(sentence, emo_category)
        if is_beam_search is True:
            for idx, i in enumerate(result[0][0]):
                reply = token_to_str(i, reverse_vocab_table)
                res += "\tpred %s:%s\n" % (idx, reply)
            res += "\n"
        else:
            reply = result[0][0]
            reply = token_to_str(reply, reverse_vocab_table)
            res += "\tpred:%s\n\n" % reply
        print(res)
        final_result.append(res)

    with open(config["inference"]["output_path"], 'w') as f:
        for i in final_result:
            f.write(i + '\n')
    print("\tDone.")
Example #4
0
File: rnn.py Project: zeal4u/NER
                                                                                "predict.")
    parser.add_argument("--data", default=config.FLAGS.pred_file, type=str, help="predict data path")

    args = parser.parse_args()
    config.FLAGS.model_path = args.modelpath
    config.FLAGS.action = args.action
    config.FLAGS.pred_file = args.data

    action = config.FLAGS.action

    # 获取词的总数。
    vocab_size = get_src_vocab_size()
    src_unknown_id = tgt_unknown_id = vocab_size
    src_padding = vocab_size + 1

    src_vocab_table, tgt_vocab_table = create_vocab_tables(src_vocab_file, tgt_vocab_file, src_unknown_id,
                                                           tgt_unknown_id)
    embedding = load_word2vec_embedding(vocab_size)

    if action == 'train':
        iterator = get_iterator(src_vocab_table, tgt_vocab_table, vocab_size, BATCH_SIZE)
    elif action == 'predict':
        BATCH_SIZE = 1
        DROPOUT_RATE = 1.0
        iterator = get_predict_iterator(src_vocab_table, vocab_size, BATCH_SIZE)
    else:
        print('Only support train and predict actions.')
        exit(0)

    tag_table = tag_to_id_table()
    net = NER_net("ner", iterator, embedding, BATCH_SIZE)
    with tf.Session() as sess:
Example #5
0
def main(args):
    # loading configurations
    with open(args.config) as f:
        config = yaml.safe_load(f)["configuration"]

    work_space = config["workspace"]
    name = config["Name"]

    # Construct or load embeddings
    print("Initializing embeddings ...")
    vocab_size = config["embeddings"]["vocab_size"]
    embed_size = config["embeddings"]["embed_size"]
    vocab_file = '%s/data/%s-%s' % (work_space, "vocab", vocab_size)
    print("\tDone.")

    (enc_num_layers, enc_num_units, enc_cell_type, enc_bidir, attn_num_units,
     dec_num_layers, dec_num_units, dec_cell_type, state_pass, infer_max_iter,
     emo_cat_emb_size, emo_internal_memory_units, num_emotion, l2_regularize,
     learning_rate) = get_ecm_model_config(config)

    (is_beam_search, beam_size, batch_size, infer_source_file,
     infer_target_file, infer_emotion_category_file, infer_source_max_length,
     output_path, output_choice_path, gpu_fraction,
     gpu_id) = get_ecm_infer_config(config)

    print("Building model architecture ...")
    ecm_model = ECMModel(mode='infer',
                         model_name=name,
                         vocab_size=vocab_size,
                         embedding_size=embed_size,
                         enc_num_layers=enc_num_layers,
                         enc_num_units=enc_num_units,
                         enc_cell_type=enc_cell_type,
                         enc_bidir=enc_bidir,
                         attn_num_units=attn_num_units,
                         dec_num_layers=dec_num_layers,
                         dec_num_units=dec_num_units,
                         dec_cell_type=dec_cell_type,
                         emo_cat_emb_size=emo_cat_emb_size,
                         emo_internal_memory_units=emo_internal_memory_units,
                         num_emotion=num_emotion,
                         batch_size=batch_size,
                         beam_search=is_beam_search,
                         beam_size=beam_size,
                         infer_max_iter=infer_max_iter,
                         l2_regularize=l2_regularize,
                         learning_rate=learning_rate)
    print("\tDone.")

    # Set up session
    restore_from = '%s/nn_models' % work_space
    gpu_fraction = config["training"]["gpu_fraction"]
    gpu_id = config["training"]["gpu_id"]
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_fraction,
                                visible_device_list=gpu_id)
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=False,
                                            gpu_options=gpu_options))

    init = tf.global_variables_initializer()
    sess.run(init)
    # print('global_variables:\n')
    # glob_var = tf.global_variables()
    # pprint(glob_var)
    # model_path = '%s/model.ckpt-53000' % restore_from

    try:
        saved_global_step = load(ecm_model.saver, sess, restore_from)
        if saved_global_step is None:
            raise ValueError("Cannot find the checkpoint to restore from.")

    except Exception:
        print("Something went wrong while restoring checkpoint. ")
        raise

    # ##### Inference #####
    # Load data
    print("Loading inference data ...")

    # Load vocabularies.
    vocab_table, reverse_vocab_table = create_vocab_tables(vocab_file)

    src_dataset = prepare_ecm_infer_data(infer_source_file,
                                         infer_emotion_category_file,
                                         vocab_table,
                                         max_length=infer_source_max_length)
    print("\tDone.")

    # Inference
    print("Start inferring ...")
    final_result = []

    for ith in range(int(len(src_dataset) / batch_size)):
        start = ith
        end = ith + 1
        batch = get_ecm_infer_batch(src_dataset, start, end,
                                    infer_source_max_length)

        sentence = token_to_str(batch[0][0], reverse_vocab_table)
        # target = token_to_str(tgt_dataset[ith], reverse_vocab_table)
        emo_category = p_map[batch[2][0]]

        start_time = time.time()
        result = ecm_model.infer(sess, batch)
        duration = round((time.time() - start_time), 3)
        print("sentence:%s, cost:%s s" % (ith, duration))

        # res = "src:{}  emotion:{}\ntgt:{}\n".format(sentence, emo_category, target)
        res = "src:{}  emotion:{}\n".format(sentence, emo_category)
        if is_beam_search is True:
            for idx, i in enumerate(result[0][0]):
                reply = token_to_str(i, reverse_vocab_table)
                res += "\tpred %s:%s\n" % (idx, reply)
            res += "\n"
        else:
            reply = result[0][0]
            reply = token_to_str(reply, reverse_vocab_table)
            res += "\tpred:%s\n\n" % reply
        print(res)
        final_result.append(res)

    with open(config["inference"]["output_path"], 'w') as f:
        for i in final_result:
            f.write(i + '\n')
    print("\tDone.")