dataprovider.py 3.0 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# 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.

from paddle.trainer.PyDataProvider2 import *
import common_utils  # parse

18

Y
Yu Yang 已提交
19 20 21 22 23 24 25 26
def __list_to_map__(lst):
    ret_val = dict()
    for each in lst:
        k, v = each
        ret_val[k] = v
    return ret_val


Z
zhangjinchao01 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
def hook(settings, meta, **kwargs):
    """
    Init hook is invoked before process data. It will set obj.slots and store
    data meta.

    :param obj: global object. It will passed to process routine.
    :type obj: object
    :param meta: the meta file object, which passed from trainer_config. Meta
                 file record movie/user features.
    :param kwargs: unused other arguments.
    """
    del kwargs  # unused kwargs

    # Header define slots that used for paddle.
    #    first part is movie features.
    #    second part is user features.
    #    final part is rating score.
    # header is a list of [USE_SEQ_OR_NOT?, SlotType]
Y
Yu Yang 已提交
45 46 47 48 49 50 51
    movie_headers = list(common_utils.meta_to_header(meta, 'movie'))
    settings.movie_names = [h[0] for h in movie_headers]
    headers = movie_headers
    user_headers = list(common_utils.meta_to_header(meta, 'user'))
    settings.user_names = [h[0] for h in user_headers]
    headers.extend(user_headers)
    headers.append(("rating", dense_vector(1)))  # Score
Z
zhangjinchao01 已提交
52 53

    # slot types.
Y
Yu Yang 已提交
54
    settings.input_types = __list_to_map__(headers)
Z
zhangjinchao01 已提交
55 56
    settings.meta = meta

57

Z
zhangjinchao01 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
@provider(init_hook=hook, cache=CacheType.CACHE_PASS_IN_MEM)
def process(settings, filename):
    with open(filename, 'r') as f:
        for line in f:
            # Get a rating from file.
            user_id, movie_id, score = map(int, line.split('::')[:-1])

            # Scale score to [-5, +5]
            score = float(score) * 2 - 5.0

            # Get movie/user features by movie_id, user_id
            movie_meta = settings.meta['movie'][movie_id]
            user_meta = settings.meta['user'][user_id]

Y
Yu Yang 已提交
72
            outputs = [('movie_id', movie_id - 1)]
Z
zhangjinchao01 已提交
73 74

            # Then add movie features
Y
Yu Yang 已提交
75 76
            for i, each_meta in enumerate(movie_meta):
                outputs.append((settings.movie_names[i + 1], each_meta))
Z
zhangjinchao01 已提交
77 78

            # Then add user id.
Y
Yu Yang 已提交
79
            outputs.append(('user_id', user_id - 1))
Z
zhangjinchao01 已提交
80 81

            # Then add user features.
Y
Yu Yang 已提交
82 83
            for i, each_meta in enumerate(user_meta):
                outputs.append((settings.user_names[i + 1], each_meta))
Z
zhangjinchao01 已提交
84 85

            # Finally, add score
Y
Yu Yang 已提交
86
            outputs.append(('rating', [score]))
Z
zhangjinchao01 已提交
87
            # Return data to paddle
Y
Yu Yang 已提交
88
            yield __list_to_map__(outputs)