|
| 1 | +# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import numpy as np |
| 17 | +import paddle.fluid.incubate.data_generator as dg |
| 18 | +try: |
| 19 | + import cPickle as pickle |
| 20 | +except ImportError: |
| 21 | + import pickle |
| 22 | + |
| 23 | +import paddle.fluid.incubate.data_generator as dg |
| 24 | + |
| 25 | + |
| 26 | +class Reader(dg.MultiSlotDataGenerator): |
| 27 | + def __init__(self, config): |
| 28 | + dg.MultiSlotDataGenerator.__init__(self) |
| 29 | + |
| 30 | + def init(self): |
| 31 | + # DCN_v2 use log normalize the 13 continuous features |
| 32 | + # log(x+4)for dense-feature-2, log(x+1) for others |
| 33 | + |
| 34 | + # self.cont_min_ = [0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 35 | + # self.cont_max_ = [ |
| 36 | + # 5775, 257675, 65535, 969, 23159456, 431037, 56311, 6047, 29019, 46, |
| 37 | + # 231, 4008, 7393 |
| 38 | + # ] |
| 39 | + # self.cont_diff_ = [ |
| 40 | + # self.cont_max_[i] - self.cont_min_[i] |
| 41 | + # for i in range(len(self.cont_min_)) |
| 42 | + # ] |
| 43 | + |
| 44 | + self.continuous_range_ = range(1, 14) |
| 45 | + self.categorical_range_ = range(14, 40) |
| 46 | + # load preprocessed feature dict |
| 47 | + self.feat_dict_name = "deepfm%2Ffeat_dict_10.pkl2" # |
| 48 | + self.feat_dict_ = pickle.load(open(self.feat_dict_name, 'rb')) |
| 49 | + |
| 50 | + def _process_line(self, line): |
| 51 | + features = line.rstrip('\n').split('\t') |
| 52 | + feat_idx = [] |
| 53 | + feat_value = [] |
| 54 | + # log normalize |
| 55 | + for idx in self.continuous_range_: |
| 56 | + if features[idx] == '': |
| 57 | + # feat_idx.append(0) |
| 58 | + feat_value.append(0.0) |
| 59 | + else: |
| 60 | + # feat_idx.append(self.feat_dict_[idx]) |
| 61 | + if idx == 2: # log(x+4) |
| 62 | + feat_value.append(np.log(float(features[idx]) + 4)) |
| 63 | + else: # log(x+1) |
| 64 | + feat_value.append(np.log(float(features[idx]) + 1)) |
| 65 | + |
| 66 | + # feat_idx.append(self.feat_dict_[idx]) |
| 67 | + # feat_value.append( |
| 68 | + # (float(features[idx]) - self.cont_min_[idx - 1]) / |
| 69 | + # self.cont_diff_[idx - 1]) |
| 70 | + |
| 71 | + for idx in self.categorical_range_: |
| 72 | + if features[idx] == '' or features[idx] not in self.feat_dict_: |
| 73 | + feat_idx.append(0) |
| 74 | + # feat_value.append(0.0) |
| 75 | + else: |
| 76 | + feat_idx.append(self.feat_dict_[features[idx]]) |
| 77 | + # feat_value.append(1.0) |
| 78 | + label = [int(features[0])] |
| 79 | + return label, feat_value, feat_idx |
| 80 | + |
| 81 | + def generate_sample(self, line): |
| 82 | + """ |
| 83 | + Read the data line by line and process it as a dictionary |
| 84 | + """ |
| 85 | + |
| 86 | + def data_iter(): |
| 87 | + label, feat_value, feat_idx = self._process_line(line) |
| 88 | + s = "" |
| 89 | + for i in [('click', label), ('dense_feature', feat_value), |
| 90 | + ('feat_idx', feat_idx)]: |
| 91 | + k = i[0] |
| 92 | + v = i[1] |
| 93 | + for n, j in enumerate(v): |
| 94 | + if k == "feat_idx": |
| 95 | + s += " " + str(n + 1) + ":" + str(j) |
| 96 | + else: |
| 97 | + s += " " + k + ":" + str(j) |
| 98 | + print(s.strip()) # add print for data preprocessing |
| 99 | + yield None |
| 100 | + |
| 101 | + return data_iter |
| 102 | + |
| 103 | + |
| 104 | +reader = Reader("../config.yaml") |
| 105 | +reader.init() |
| 106 | +reader.run_from_stdin() |
0 commit comments