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

Q
Qiao Longfei 已提交
4
import numpy as np
5 6 7
# disable gpu training for this example 
import os
os.environ["CUDA_VISIBLE_DEVICES"] = ""
Q
Qiao Longfei 已提交
8 9 10
import paddle
import paddle.fluid as fluid

Q
Qiao Longfei 已提交
11
import reader
Q
Qiao Longfei 已提交
12
from network_conf import ctr_dnn_model
13
import utils
Q
Qiao Longfei 已提交
14

Y
Yibing Liu 已提交
15
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
Q
Qiao Longfei 已提交
16 17
logger = logging.getLogger("fluid")
logger.setLevel(logging.INFO)
Q
Qiao Longfei 已提交
18 19


Q
Qiao Longfei 已提交
20 21 22
def parse_args():
    parser = argparse.ArgumentParser(description="PaddlePaddle DeepFM example")
    parser.add_argument(
Q
Qiao Longfei 已提交
23
        '--model_path',
Q
Qiao Longfei 已提交
24 25 26 27 28 29 30 31 32
        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 已提交
33
        '--embedding_size',
Q
Qiao Longfei 已提交
34 35
        type=int,
        default=10,
Q
Qiao Longfei 已提交
36
        help="The size for embedding layer (default:10)")
37 38 39 40 41
    parser.add_argument(
        '--sparse_feature_dim',
        type=int,
        default=1000001,
        help="The size for embedding layer (default:1000001)")
Q
Qiao Longfei 已提交
42 43 44 45 46
    parser.add_argument(
        '--batch_size',
        type=int,
        default=1000,
        help="The size of mini-batch (default:1000)")
Q
Qiao Longfei 已提交
47 48 49 50 51 52 53

    return parser.parse_args()


def infer():
    args = parse_args()

Q
Qiao Longfei 已提交
54
    place = fluid.CPUPlace()
Y
Yibing Liu 已提交
55 56
    inference_scope = fluid.Scope()

57
    dataset = reader.CriteoDataset(args.sparse_feature_dim)
Y
Yibing Liu 已提交
58 59 60
    test_reader = paddle.batch(
        dataset.test([args.data_path]), batch_size=args.batch_size)

Q
Qiao Longfei 已提交
61 62 63
    startup_program = fluid.framework.Program()
    test_program = fluid.framework.Program()
    with fluid.framework.program_guard(test_program, startup_program):
Y
Yibing Liu 已提交
64 65 66
        loss, auc_var, batch_auc_var, _, data_list = ctr_dnn_model(
            args.embedding_size, args.sparse_feature_dim, False)

Q
Qiao Longfei 已提交
67 68 69 70
        exe = fluid.Executor(place)

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

Y
Yibing Liu 已提交
71 72 73 74
        fluid.io.load_persistables(
            executor=exe,
            dirname=args.model_path,
            main_program=fluid.default_main_program())
Q
Qiao Longfei 已提交
75 76 77 78 79 80 81 82 83 84

        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)

Q
Qiao Longfei 已提交
85
        for batch_id, data in enumerate(test_reader()):
Q
Qiao Longfei 已提交
86
            loss_val, auc_val = exe.run(test_program,
Y
Yibing Liu 已提交
87 88
                                        feed=feeder.feed(data),
                                        fetch_list=[loss, auc_var])
Q
Qiao Longfei 已提交
89
            if batch_id % 100 == 0:
Y
Yibing Liu 已提交
90 91
                logger.info("TEST --> batch: {} loss: {} auc: {}".format(
                    batch_id, loss_val / args.batch_size, auc_val))
Q
Qiao Longfei 已提交
92 93 94


if __name__ == '__main__':
95
    utils.check_version()
Q
Qiao Longfei 已提交
96
    infer()