|
| 1 | +# Copyright (c) 2021 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 | +from odps import ODPS |
| 16 | +from odps.models import Schema, Column, Partition |
| 17 | + |
| 18 | +import paddle |
| 19 | +import paddle.distributed.fleet as fleet |
| 20 | +import os |
| 21 | +import sys |
| 22 | + |
| 23 | +cont_min_ = [0, -3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
| 24 | +cont_max_ = [20, 600, 100, 50, 64000, 500, 100, 50, 500, 10, 10, 10, 50] |
| 25 | +cont_diff_ = [20, 603, 100, 50, 64000, 500, 100, 50, 500, 10, 10, 10, 50] |
| 26 | +hash_dim_ = 1000001 |
| 27 | +continuous_range_ = range(1, 14) |
| 28 | +categorical_range_ = range(14, 40) |
| 29 | + |
| 30 | + |
| 31 | +class WideDeepDatasetReader(fleet.MultiSlotDataGenerator): |
| 32 | + def line_process(self, record): |
| 33 | + label = [record.label] |
| 34 | + dense_feature = [] |
| 35 | + sparse_feature = [] |
| 36 | + for idx in continuous_range_: |
| 37 | + dense_feature.append((float(record[idx]) - cont_min_[idx - 1]) / |
| 38 | + cont_diff_[idx - 1]) |
| 39 | + for idx in categorical_range_: |
| 40 | + sparse_feature.append([hash(str(idx) + record[idx]) % hash_dim_]) |
| 41 | + return [dense_feature] + sparse_feature + [label] |
| 42 | + |
| 43 | + def generate_sample(self, line): |
| 44 | + def wd_reader(): |
| 45 | + for record in table_reader: |
| 46 | + input_data = self.line_process(record) |
| 47 | + feature_name = ["dense_input"] |
| 48 | + for idx in categorical_range_: |
| 49 | + feature_name.append("C" + str(idx - 13)) |
| 50 | + feature_name.append("label") |
| 51 | + yield zip(feature_name, input_data) |
| 52 | + |
| 53 | + return wd_reader |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + my_data_generator = WideDeepDatasetReader() |
| 58 | + #my_data_generator.set_batch(16) |
| 59 | + |
| 60 | + from config import * |
| 61 | + # config should include flowing configuration |
| 62 | + # access_id |
| 63 | + # secret_key |
| 64 | + # project |
| 65 | + # endpoint |
| 66 | + |
| 67 | + o = ODPS(access_id, secret_key, project, endpoint=endpoint) |
| 68 | + |
| 69 | + table_name = 'wide_and_deep' |
| 70 | + |
| 71 | + table = o.get_table(table_name) #.to_df() |
| 72 | + |
| 73 | + table_reader = table.open_reader() |
| 74 | + |
| 75 | + my_data_generator.run_from_memory() |
0 commit comments