infer.py 2.1 KB
Newer Older
Q
Qiao Longfei 已提交
1 2
import argparse

Q
Qiao Longfei 已提交
3
import numpy as np
Q
Qiao Longfei 已提交
4 5 6
import paddle
import paddle.fluid as fluid

Q
Qiao Longfei 已提交
7
import reader
Q
Qiao Longfei 已提交
8
from network_conf import ctr_dnn_model
Q
Qiao Longfei 已提交
9 10 11 12 13


def parse_args():
    parser = argparse.ArgumentParser(description="PaddlePaddle DeepFM example")
    parser.add_argument(
Q
Qiao Longfei 已提交
14
        '--model_path',
Q
Qiao Longfei 已提交
15 16 17 18 19 20 21 22 23
        type=str,
        required=True,
        help="The path of model parameters gz file")
    parser.add_argument(
        '--data_path',
        type=str,
        required=True,
        help="The path of the dataset to infer")
    parser.add_argument(
Q
Qiao Longfei 已提交
24
        '--embedding_size',
Q
Qiao Longfei 已提交
25 26
        type=int,
        default=10,
Q
Qiao Longfei 已提交
27
        help="The size for embedding layer (default:10)")
Q
Qiao Longfei 已提交
28 29 30 31 32 33 34

    return parser.parse_args()


def infer():
    args = parse_args()

Q
Qiao Longfei 已提交
35 36
    place = fluid.CPUPlace()
    inference_scope = fluid.core.Scope()
Q
Qiao Longfei 已提交
37 38

    dataset = reader.Dataset()
Q
Qiao Longfei 已提交
39
    test_reader = paddle.batch(dataset.train([args.data_path]), batch_size=1000)
Q
Qiao Longfei 已提交
40 41 42 43

    startup_program = fluid.framework.Program()
    test_program = fluid.framework.Program()
    with fluid.framework.program_guard(test_program, startup_program):
Q
Qiao Longfei 已提交
44
        loss, data_list, auc_var, batch_auc_var = ctr_dnn_model(args.embedding_size)
Q
Qiao Longfei 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    exe = fluid.Executor(place)

    feeder = fluid.DataFeeder(feed_list=data_list, place=place)

    with fluid.scope_guard(inference_scope):
        [inference_program, _, fetch_targets] = fluid.io.load_inference_model(args.model_path, exe)
        print(fetch_targets)

        def set_zero(var_name):
            param = inference_scope.var(var_name).get_tensor()
            param_array = np.zeros(param._get_dims()).astype("int64")
            param.set(param_array, place)

        auc_states_names = ['_generated_var_2', '_generated_var_3']
        for name in auc_states_names:
            set_zero(name)

        batch_id = 0
        for data in test_reader():
            loss_val, auc_val = exe.run(inference_program,
                feed=feeder.feed(data),
                fetch_list=fetch_targets)
            if batch_id % 100 == 0:
                print("loss: " + str(loss_val) + " auc_val:" + str(auc_val))
            batch_id += 1
Q
Qiao Longfei 已提交
71 72 73 74


if __name__ == '__main__':
    infer()