testPyDataWrapper.py 4.5 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Copyright (c) 2016 Baidu, Inc. 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.

import sys
sys.path.append("../")

from paddle.trainer.PyDataProviderWrapper import *
import random
import json
import string


24 25 26 27
@provider(slots=[
    SparseNonValueSlot(10), DenseSlot(2), SparseValueSlot(10), StringSlot(1),
    IndexSlot(3)
])
Z
zhangjinchao01 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
def processNonSequenceData(obj, filename):
    with open(filename, "rb") as f:
        for line in f:
            slots_str = line.split(';')
            index = int(slots_str[0])
            non_values = map(int, slots_str[1].split()[1:])
            dense = map(float, slots_str[2].split()[1:])
            strs = slots_str[4].strip().split(' ', 1)[1]

            def __values_mapper__(s):
                s = s.split(":")
                return int(s[0]), float(s[1])

            values = map(__values_mapper__, slots_str[3].split()[1:])
            yield [non_values, dense, values, strs, index]


SPARSE_ID_LIMIT = 1000
SPARSE_ID_COUNT = 100
SEQUENCE_LIMIT = 50
STRING_LIMIT = 10

sparse_id_randomer = lambda: random.randrange(0, SPARSE_ID_LIMIT - 1)
sparse_count_randomer = lambda: random.randrange(1, SPARSE_ID_COUNT)
val_randomer = lambda: random.uniform(-1.0, 1.0)
seq_count_randomer = lambda: random.randrange(1, SEQUENCE_LIMIT)
str_count_randomer = lambda: random.randrange(1, STRING_LIMIT)

56

Z
zhangjinchao01 已提交
57 58 59 60 61 62 63 64 65 66 67
class IDRandomer():  # A random generator, return unique id
    def __init__(self):
        self.id_set = set()

    def __call__(self):
        idx = sparse_id_randomer()
        if idx not in self.id_set:
            self.id_set.add(idx)
            return idx
        else:
            return self.__call__()
68 69


Z
zhangjinchao01 已提交
70 71 72 73
# SparseValueSlot
def sparse_value_creator(_):
    rand = IDRandomer()
    return [(rand(), val_randomer()) for _ in xrange(sparse_count_randomer())]
74 75


Z
zhangjinchao01 已提交
76 77
sparse_value = map(sparse_value_creator, range(seq_count_randomer()))

78

Z
zhangjinchao01 已提交
79 80 81
# DenseSlot
def dense_creator(_):
    return [val_randomer() for _ in xrange(SPARSE_ID_LIMIT)]
82 83


Z
zhangjinchao01 已提交
84 85
dense = map(dense_creator, range(seq_count_randomer()))

86

Z
zhangjinchao01 已提交
87 88 89 90
# SparseNonValueSlot
def sparse_creator(_):
    rand = IDRandomer()
    return [rand() for _ in xrange(sparse_count_randomer())]
91 92


Z
zhangjinchao01 已提交
93 94 95 96 97
sparse_nonvalue = map(sparse_creator, range(seq_count_randomer()))

# IndexSlot
ids = [sparse_id_randomer() for _ in range(seq_count_randomer())]

98

Z
zhangjinchao01 已提交
99
# StringSlot
100
def random_str(size=8, chars=string.ascii_letters + string.digits):
Z
zhangjinchao01 已提交
101
    return ''.join(random.choice(chars) for _ in range(size))
102 103


Z
zhangjinchao01 已提交
104 105
strs = [random_str(str_count_randomer()) for _ in range(seq_count_randomer())]

106

Z
zhangjinchao01 已提交
107 108 109
def processSeqAndGenerateDataInit(obj, *args, **kwargs):
    obj.json_filename = kwargs.get("load_data_args", "test_data.json")

110 111 112 113 114 115 116 117 118

@provider(
    slots=[
        SparseValueSlot(SPARSE_ID_LIMIT), DenseSlot(SPARSE_ID_LIMIT),
        SparseNonValueSlot(SPARSE_ID_LIMIT), IndexSlot(SPARSE_ID_LIMIT),
        StringSlot(SPARSE_ID_LIMIT)
    ],
    use_seq=True,
    init_hook=processSeqAndGenerateDataInit)
Z
zhangjinchao01 已提交
119 120 121 122 123 124 125 126 127 128 129
def processSeqAndGenerateData(obj, name):
    retv = [sparse_value, dense, sparse_nonvalue, ids, strs]
    # Write to protoseq.
    with open(obj.json_filename, "w") as f:
        json.dump(retv, f)
    yield retv


def processSubSeqAndGenerateDataInit(obj, *args, **kwargs):
    obj.json_filename = kwargs.get("load_data_args", "test_data.json")

130 131 132 133 134 135 136 137 138

@provider(
    slots=[
        SparseValueSlot(SPARSE_ID_LIMIT), DenseSlot(SPARSE_ID_LIMIT),
        SparseNonValueSlot(SPARSE_ID_LIMIT), IndexSlot(SPARSE_ID_LIMIT),
        StringSlot(SPARSE_ID_LIMIT)
    ],
    use_seq=True,
    init_hook=processSubSeqAndGenerateDataInit)
Z
zhangjinchao01 已提交
139 140 141 142 143 144 145 146
def processSubSeqAndGenerateData(obj, name):
    retv_json = [sparse_value, dense, sparse_nonvalue, ids, strs]
    retv_wrapper = [[sparse_value], [dense], [sparse_nonvalue], [ids], [strs]]
    # Write to protoseq.
    with open(obj.json_filename, "w") as f:
        json.dump(retv_json, f)
    yield retv_wrapper

147

Z
zhangjinchao01 已提交
148 149 150 151 152 153 154
if __name__ == "__main__":
    pvd = processNonSequenceData("test.txt")
    print pvd.getNextBatch(100)
    pvd = processSeqAndGenerateData("_")
    print pvd.getNextBatch(100)
    pvd = processSubSeqAndGenerateData("_")
    print pvd.getNextBatch(1)