|
| 1 | +# Copyright (c) 2019 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 | +import sys |
| 15 | +import yaml |
| 16 | +import six |
| 17 | +import os |
| 18 | +import copy |
| 19 | +import paddle.distributed.fleet as fleet |
| 20 | +import logging |
| 21 | +import numpy as np |
| 22 | + |
| 23 | +logging.basicConfig( |
| 24 | + format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO) |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +fea_dict = {} |
| 28 | + |
| 29 | + |
| 30 | +class Reader(fleet.MultiSlotStringDataGenerator): |
| 31 | + def init(self, config): |
| 32 | + self.config = config |
| 33 | + padding = "0" |
| 34 | + #sparse_slots = "click 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26" |
| 35 | + self.slots = self.slot_reader(slot_num=408) |
| 36 | + self.slot2index = {} |
| 37 | + self.visit = {} |
| 38 | + for i in range(len(self.slots)): |
| 39 | + self.slot2index[self.slots[i]] = i |
| 40 | + self.visit[self.slots[i]] = False |
| 41 | + self.padding = padding |
| 42 | + logger.info("pipe init success") |
| 43 | + |
| 44 | + def slot_reader(self, slot_num=0, slot_file='./slot'): |
| 45 | + slots = [] |
| 46 | + # slot is not 0, label=1, |
| 47 | + if slot_num > 0: |
| 48 | + for i in range(slot_num + 2): |
| 49 | + if i == 0: |
| 50 | + continue |
| 51 | + slots.append(str(i)) |
| 52 | + else: |
| 53 | + with open(slot_file, "r") as rf: |
| 54 | + for line in rf.readlines(): |
| 55 | + slots.append(line.strip()) |
| 56 | + return slots |
| 57 | + |
| 58 | + def line_process(self, line): |
| 59 | + ins_id, line = line.strip().split("\t") |
| 60 | + line = line.strip().split(" ") |
| 61 | + output = [(i, []) for i in self.slots] |
| 62 | + for i in line: |
| 63 | + slot_feasign = i.split(":") |
| 64 | + if len(slot_feasign) < 2: |
| 65 | + print(i) |
| 66 | + slot = slot_feasign[1] |
| 67 | + if slot not in self.slots: |
| 68 | + continue |
| 69 | + feasign = int(slot_feasign[0]) |
| 70 | + if feasign not in fea_dict: |
| 71 | + fea_dict[feasign] = str(len(fea_dict)) |
| 72 | + output[self.slot2index[slot]][1].append(fea_dict[feasign]) |
| 73 | + self.visit[slot] = True |
| 74 | + for i in self.visit: |
| 75 | + slot = i |
| 76 | + if not self.visit[slot]: |
| 77 | + output[self.slot2index[i]][1].extend([self.padding]) |
| 78 | + else: |
| 79 | + self.visit[slot] = False |
| 80 | + output = [("ins_id", [ins_id])] + output |
| 81 | + return output |
| 82 | + #return [label] + sparse_feature + [dense_feature] |
| 83 | + |
| 84 | + def generate_sample(self, line): |
| 85 | + "Dataset Generator" |
| 86 | + |
| 87 | + def reader(): |
| 88 | + output_dict = self.line_process(line) |
| 89 | + # {key, value} dict format: {'labels': [1], 'sparse_slot1': [2, 3], 'sparse_slot2': [4, 5, 6, 8], 'dense_slot': [1,2,3,4]} |
| 90 | + # dict must match static_model.create_feed() |
| 91 | + yield output_dict |
| 92 | + |
| 93 | + return reader |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + yaml_path = sys.argv[1] |
| 98 | + utils_path = sys.argv[2] |
| 99 | + sys.path.append(utils_path) |
| 100 | + import common |
| 101 | + yaml_helper = common.YamlHelper() |
| 102 | + config = yaml_helper.load_yaml(yaml_path) |
| 103 | + |
| 104 | + r = Reader() |
| 105 | + r.init(config) |
| 106 | + r.run_from_stdin() |
0 commit comments