generate_synthetic_data.py 3.6 KB
Newer Older
T
tangwei 已提交
1
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved
M
malin10 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#
# 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.

import random

T
for mat  
tangwei 已提交
17

M
malin10 已提交
18 19 20 21
class Dataset:
    def __init__(self):
        pass

T
for mat  
tangwei 已提交
22

M
malin10 已提交
23
class SyntheticDataset(Dataset):
T
tangwei 已提交
24 25 26 27 28
    def __init__(self,
                 sparse_feature_dim,
                 query_slot_num,
                 title_slot_num,
                 dataset_size=10000):
M
malin10 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        # ids are randomly generated
        self.ids_per_slot = 10
        self.sparse_feature_dim = sparse_feature_dim
        self.query_slot_num = query_slot_num
        self.title_slot_num = title_slot_num
        self.dataset_size = dataset_size

    def _reader_creator(self, is_train):
        def generate_ids(num, space):
            return [random.randint(0, space - 1) for i in range(num)]

        def reader():
            for i in range(self.dataset_size):
                query_slots = []
                pos_title_slots = []
                neg_title_slots = []
                for i in range(self.query_slot_num):
                    qslot = generate_ids(self.ids_per_slot,
                                         self.sparse_feature_dim)
T
for mat  
tangwei 已提交
48
                    qslot = [str(fea) + ':' + str(i) for fea in qslot]
M
malin10 已提交
49 50 51 52
                    query_slots += qslot
                for i in range(self.title_slot_num):
                    pt_slot = generate_ids(self.ids_per_slot,
                                           self.sparse_feature_dim)
T
tangwei 已提交
53 54 55 56
                    pt_slot = [
                        str(fea) + ':' + str(i + self.query_slot_num)
                        for fea in pt_slot
                    ]
M
malin10 已提交
57 58 59 60 61
                    pos_title_slots += pt_slot
                if is_train:
                    for i in range(self.title_slot_num):
                        nt_slot = generate_ids(self.ids_per_slot,
                                               self.sparse_feature_dim)
T
tangwei 已提交
62 63 64 65 66
                        nt_slot = [
                            str(fea) + ':' +
                            str(i + self.query_slot_num + self.title_slot_num)
                            for fea in nt_slot
                        ]
M
malin10 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
                        neg_title_slots += nt_slot
                    yield query_slots + pos_title_slots + neg_title_slots
                else:
                    yield query_slots + pos_title_slots

        return reader

    def train(self):
        return self._reader_creator(True)

    def valid(self):
        return self._reader_creator(True)

    def test(self):
        return self._reader_creator(False)

T
for mat  
tangwei 已提交
83

M
malin10 已提交
84 85 86 87 88
if __name__ == '__main__':
    sparse_feature_dim = 1000001
    query_slots = 1
    title_slots = 1
    dataset_size = 10
T
tangwei 已提交
89 90
    dataset = SyntheticDataset(sparse_feature_dim, query_slots, title_slots,
                               dataset_size)
M
malin10 已提交
91 92
    train_reader = dataset.train()
    test_reader = dataset.test()
T
for mat  
tangwei 已提交
93

M
malin10 已提交
94 95 96 97 98 99 100 101 102
    with open("data/train/train.txt", 'w') as fout:
        for data in train_reader():
            fout.write(' '.join(data))
            fout.write("\n")

    with open("data/test/test.txt", 'w') as fout:
        for data in test_reader():
            fout.write(' '.join(data))
            fout.write("\n")