testPyDataWrapper.py 3.8 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
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 27 28 29 30 31 32 33
#
# 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

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)

34

Z
zhangjinchao01 已提交
35 36 37 38 39 40 41 42 43 44 45
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__()
46 47


Z
zhangjinchao01 已提交
48 49 50 51
# SparseValueSlot
def sparse_value_creator(_):
    rand = IDRandomer()
    return [(rand(), val_randomer()) for _ in xrange(sparse_count_randomer())]
52 53


Z
zhangjinchao01 已提交
54 55
sparse_value = map(sparse_value_creator, range(seq_count_randomer()))

56

Z
zhangjinchao01 已提交
57 58 59
# DenseSlot
def dense_creator(_):
    return [val_randomer() for _ in xrange(SPARSE_ID_LIMIT)]
60 61


Z
zhangjinchao01 已提交
62 63
dense = map(dense_creator, range(seq_count_randomer()))

64

Z
zhangjinchao01 已提交
65 66 67 68
# SparseNonValueSlot
def sparse_creator(_):
    rand = IDRandomer()
    return [rand() for _ in xrange(sparse_count_randomer())]
69 70


Z
zhangjinchao01 已提交
71 72 73 74 75
sparse_nonvalue = map(sparse_creator, range(seq_count_randomer()))

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

76

Z
zhangjinchao01 已提交
77
# StringSlot
78
def random_str(size=8, chars=string.ascii_letters + string.digits):
Z
zhangjinchao01 已提交
79
    return ''.join(random.choice(chars) for _ in range(size))
80 81


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

84

Z
zhangjinchao01 已提交
85 86 87
def processSeqAndGenerateDataInit(obj, *args, **kwargs):
    obj.json_filename = kwargs.get("load_data_args", "test_data.json")

88 89 90 91 92 93 94 95 96

@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 已提交
97 98 99 100 101 102 103 104 105 106 107
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")

108 109 110 111 112 113 114 115 116

@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 已提交
117 118 119 120 121 122 123 124
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

125

Z
zhangjinchao01 已提交
126 127 128 129 130
if __name__ == "__main__":
    pvd = processSeqAndGenerateData("_")
    print pvd.getNextBatch(100)
    pvd = processSubSeqAndGenerateData("_")
    print pvd.getNextBatch(1)