get_slot_data.py 3.3 KB
Newer Older
X
fix  
xujiaqi01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. 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.

Y
yaoxuefeng 已提交
15 16
import yaml, os

X
fix  
xujiaqi01 已提交
17 18
from paddlerec.core.reader import Reader
from paddlerec.core.utils import envs
Y
yaoxuefeng 已提交
19
import paddle.fluid.incubate.data_generator as dg
X
fix  
xujiaqi01 已提交
20 21 22 23 24
try:
    import cPickle as pickle
except ImportError:
    import pickle

T
tangwei 已提交
25 26

class TrainReader(dg.MultiSlotDataGenerator):
X
fix  
xujiaqi01 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    def __init__(self, config):
        dg.MultiSlotDataGenerator.__init__(self)

        if os.path.isfile(config):
            with open(config, 'r') as rb:
                _config = yaml.load(rb.read(), Loader=yaml.FullLoader)
        else:
            raise ValueError("reader config only support yaml")

    def init(self):
        self.cont_min_ = [0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        self.cont_max_ = [
            5775, 257675, 65535, 969, 23159456, 431037, 56311, 6047, 29019, 46,
            231, 4008, 7393
        ]
        self.cont_diff_ = [
            self.cont_max_[i] - self.cont_min_[i]
            for i in range(len(self.cont_min_))
        ]
        self.continuous_range_ = range(1, 14)
        self.categorical_range_ = range(14, 40)
        # load preprocessed feature dict 
Y
yaoxuefeng 已提交
49
        self.feat_dict_name = "sample_data/feat_dict_10.pkl2"
T
tangwei 已提交
50
        self.feat_dict_ = pickle.load(open(self.feat_dict_name, 'rb'))
X
fix  
xujiaqi01 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

    def _process_line(self, line):
        features = line.rstrip('\n').split('\t')
        feat_idx = []
        feat_value = []
        for idx in self.continuous_range_:
            if features[idx] == '':
                feat_idx.append(0)
                feat_value.append(0.0)
            else:
                feat_idx.append(self.feat_dict_[idx])
                feat_value.append(
                    (float(features[idx]) - self.cont_min_[idx - 1]) /
                    self.cont_diff_[idx - 1])
        for idx in self.categorical_range_:
            if features[idx] == '' or features[idx] not in self.feat_dict_:
                feat_idx.append(0)
                feat_value.append(0.0)
            else:
                feat_idx.append(self.feat_dict_[features[idx]])
                feat_value.append(1.0)
        label = [int(features[0])]
        return feat_idx, feat_value, label
T
for mat  
tangwei 已提交
74

X
fix  
xujiaqi01 已提交
75 76 77 78
    def generate_sample(self, line):
        """
        Read the data line by line and process it as a dictionary
        """
T
for mat  
tangwei 已提交
79

X
fix  
xujiaqi01 已提交
80 81 82
        def data_iter():
            feat_idx, feat_value, label = self._process_line(line)
            s = ""
T
tangwei 已提交
83 84
            for i in [('feat_idx', feat_idx), ('feat_value', feat_value),
                      ('label', label)]:
X
fix  
xujiaqi01 已提交
85 86 87 88 89 90
                k = i[0]
                v = i[1]
                for j in v:
                    s += " " + k + ":" + str(j)
            print s.strip()
            yield None
T
tangwei 已提交
91

X
fix  
xujiaqi01 已提交
92 93
        return data_iter

T
tangwei 已提交
94

X
fix  
xujiaqi01 已提交
95 96 97
reader = TrainReader("../config.yaml")
reader.init()
reader.run_from_stdin()