prediction.py 1.9 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/bin/env python2
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

16
from py_paddle import swig_paddle, DataProviderConverter
Z
zhangjinchao01 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

from common_utils import *
from paddle.trainer.config_parser import parse_config

try:
    import cPickle as pickle
except ImportError:
    import pickle
import sys

if __name__ == '__main__':
    model_path = sys.argv[1]
    swig_paddle.initPaddle('--use_gpu=0')
    conf = parse_config("trainer_config.py", "is_predict=1")
    network = swig_paddle.GradientMachine.createFromConfigProto(conf.model_config)
    assert isinstance(network, swig_paddle.GradientMachine)
    network.loadParameters(model_path)
34
    with open('./data/meta.bin', 'rb') as f:
Z
zhangjinchao01 已提交
35 36 37
        meta = pickle.load(f)
        headers = list(meta_to_header(meta, 'movie'))
        headers.extend(list(meta_to_header(meta, 'user')))
38
        cvt = DataProviderConverter(headers)
Z
zhangjinchao01 已提交
39 40 41 42 43 44 45 46 47
        while True:
            movie_id = int(raw_input("Input movie_id: "))
            user_id = int(raw_input("Input user_id: "))
            movie_meta = meta['movie'][movie_id]    # Query Data From Meta.
            user_meta = meta['user'][user_id]
            data = [movie_id - 1]
            data.extend(movie_meta)
            data.append(user_id - 1)
            data.extend(user_meta)
48 49
            print "Prediction Score is %.2f" % ((network.forwardTest(
                cvt.convert([data]))[0]['value'][0][0] + 5) / 2)