Example #1
0
def infer_epoch(args, vocab_size, test_reader, use_cuda, i2w):
    """ inference function """
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    exe = fluid.Executor(place)
    emb_size = args.emb_size
    batch_size = args.batch_size
    with fluid.scope_guard(fluid.Scope()):
        main_program = fluid.Program()
        with fluid.program_guard(main_program):
            values, pred = net.infer_network(vocab_size, emb_size)
            for epoch in range(start_index, last_index + 1):
                copy_program = main_program.clone()
                model_path = model_dir + "/pass-" + str(epoch)
                fluid.load(copy_program, model_path, exe)
                accum_num = 0
                accum_num_sum = 0.0
                t0 = time.time()
                step_id = 0
                for data in test_reader():
                    step_id += 1
                    b_size = len([dat[0] for dat in data])
                    wa = np.array([dat[0] for dat in data
                                   ]).astype("int64").reshape(b_size)
                    wb = np.array([dat[1] for dat in data
                                   ]).astype("int64").reshape(b_size)
                    wc = np.array([dat[2] for dat in data
                                   ]).astype("int64").reshape(b_size)

                    label = [dat[3] for dat in data]
                    input_word = [dat[4] for dat in data]
                    para = exe.run(copy_program,
                                   feed={
                                       "analogy_a":
                                       wa,
                                       "analogy_b":
                                       wb,
                                       "analogy_c":
                                       wc,
                                       "all_label":
                                       np.arange(vocab_size).reshape(
                                           vocab_size).astype("int64"),
                                   },
                                   fetch_list=[pred.name, values],
                                   return_numpy=False)
                    pre = np.array(para[0])
                    val = np.array(para[1])
                    for ii in range(len(label)):
                        top4 = pre[ii]
                        accum_num_sum += 1
                        for idx in top4:
                            if int(idx) in input_word[ii]:
                                continue
                            if int(idx) == int(label[ii][0]):
                                accum_num += 1
                            break
                    if step_id % 1 == 0:
                        print("step:%d %d " % (step_id, accum_num))

                print("epoch:%d \t acc:%.3f " %
                      (epoch, 1.0 * accum_num / accum_num_sum))
Example #2
0
def main(result_file_path, checkpoint_path):
    word_to_id, id_to_word = BuildWord_IdMap(FLAGS.dict_path)
    questions = read_analogies(word_to_id)
    vocab_size = len(id_to_word)
    pred_idx, analogy_a, analogy_b, analogy_c = infer_network(
        vocab_size, FLAGS.embedding_size)

    res = {}
    if os.path.exists(result_file_path):
        with open(result_file_path) as f:
            res = json.load(f)

    while True:
        with tf.Session() as sess:
            saver = tf.train.Saver()
            ckpt = tf.train.get_checkpoint_state(checkpoint_path)
            all_models = filter_checkpoint(checkpoint_path)
            if ckpt and all_models:
                for path in all_models:
                    global_step = path.split('/')[-1].split('-')[-1]
                    if global_step in res:
                        continue
                    print("Start to inference ==> %s" % (path))
                    saver.restore(sess, path)
                    acc = eval(sess, questions, pred_idx, analogy_a, analogy_b,
                               analogy_c)
                    res[global_step] = acc
                    with open(result_file_path, 'w') as f:
                        json.dump(res, f)
            else:
                print('No checkpoint file found')
        print("sleeping 300s")
        time.sleep(300)
Example #3
0
def infer(args, vocab_size, test_reader, use_cuda):
    """ inference function """
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    exe = fluid.Executor(place)
    hid_size = args.hid_size
    batch_size = args.batch_size
    with fluid.scope_guard(fluid.core.Scope()):
        main_program = fluid.Program()
        with fluid.program_guard(main_program):
            acc = net.infer_network(vocab_size, batch_size, hid_size)
            for epoch in range(start_index, last_index + 1):
                copy_program = main_program.clone()
                model_path = model_dir + "/epoch_" + str(epoch)
                fluid.io.load_params(executor=exe,
                                     dirname=model_path,
                                     main_program=copy_program)
                accum_num_recall = 0.0
                accum_num_sum = 0.0
                t0 = time.time()
                step_id = 0
                for data in test_reader():
                    step_id += 1
                    label_data = [dat[1] for dat in data]
                    ls, lp = utils.to_lodtensor_bpr_test(
                        data, vocab_size, place)
                    para = exe.run(copy_program,
                                   feed={
                                       "src":
                                       ls,
                                       "all_label":
                                       np.arange(vocab_size).reshape(
                                           vocab_size, 1),
                                       "pos_label":
                                       lp
                                   },
                                   fetch_list=[acc.name],
                                   return_numpy=False)

                    acc_ = np.array(para[0])[0]
                    data_length = len(
                        np.concatenate(label_data, axis=0).astype("int64"))
                    accum_num_sum += (data_length)
                    accum_num_recall += (data_length * acc_)
                    if step_id % 1 == 0:
                        print("step:%d  " % (step_id),
                              accum_num_recall / accum_num_sum)
                t1 = time.time()
                print("model:%s recall@20:%.4f time_cost(s):%.2f" %
                      (model_path, accum_num_recall / accum_num_sum, t1 - t0))