infer.py 2.7 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
Q
Qiao Longfei 已提交
13 14


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


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

    return parser.parse_args()


def infer():
    args = parse_args()

Q
Qiao Longfei 已提交
55 56
    place = fluid.CPUPlace()
    inference_scope = fluid.core.Scope()
57 58 59 60
    
    dataset = reader.CriteoDataset(args.sparse_feature_dim)
    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):
64 65
        loss, data_list, auc_var, batch_auc_var = ctr_dnn_model(args.embedding_size, args.sparse_feature_dim)
        
Q
Qiao Longfei 已提交
66
    exe = fluid.Executor(place)
67
    
Q
Qiao Longfei 已提交
68
    feeder = fluid.DataFeeder(feed_list=data_list, place=place)
69
    
Q
Qiao Longfei 已提交
70 71 72 73 74 75 76 77 78 79 80 81
    with fluid.scope_guard(inference_scope):
        [inference_program, _, fetch_targets] = fluid.io.load_inference_model(args.model_path, exe)

        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 已提交
82
        for batch_id, data in enumerate(test_reader()):
Q
Qiao Longfei 已提交
83 84 85 86
            loss_val, auc_val = exe.run(inference_program,
                feed=feeder.feed(data),
                fetch_list=fetch_targets)
            if batch_id % 100 == 0:
Q
Qiao Longfei 已提交
87
                logger.info("TEST --> batch: {} loss: {} auc: {}".format(batch_id, loss_val/args.batch_size, auc_val))
Q
Qiao Longfei 已提交
88 89 90 91


if __name__ == '__main__':
    infer()