prediction.py 2.0 KB
Newer Older
Z
zhangjinchao01 已提交
1
#!/bin/env python2
2
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
#
# 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

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")
31 32
    network = swig_paddle.GradientMachine.createFromConfigProto(
        conf.model_config)
Z
zhangjinchao01 已提交
33 34
    assert isinstance(network, swig_paddle.GradientMachine)
    network.loadParameters(model_path)
35
    with open('./data/meta.bin', 'rb') as f:
Z
zhangjinchao01 已提交
36
        meta = pickle.load(f)
Y
Yu Yang 已提交
37 38
        headers = [h[1] for h in meta_to_header(meta, 'movie')]
        headers.extend([h[1] for h in meta_to_header(meta, 'user')])
39
        cvt = DataProviderConverter(headers)
Z
zhangjinchao01 已提交
40 41 42
        while True:
            movie_id = int(raw_input("Input movie_id: "))
            user_id = int(raw_input("Input user_id: "))
43
            movie_meta = meta['movie'][movie_id]  # Query Data From Meta.
Z
zhangjinchao01 已提交
44 45 46 47 48
            user_meta = meta['user'][user_id]
            data = [movie_id - 1]
            data.extend(movie_meta)
            data.append(user_id - 1)
            data.extend(user_meta)
49 50 51
            print "Prediction Score is %.2f" % (
                (network.forwardTest(cvt.convert([data]))[0]['value'][0][0] + 5)
                / 2)