reader.py 4.6 KB
Newer Older
0
0YuanZhang0 已提交
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 27 28 29 30 31 32 33 34 35
# Copyright (c) 2019 PaddlePaddle Authors. 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.
"""Reader for auto dialogue evaluation"""

import sys
import time
import random
import numpy as np

import paddle
import paddle.fluid as fluid


class DataProcessor(object): 
    def __init__(self, data_path, max_seq_length, batch_size): 
        """init"""
        self.data_file = data_path
        self.max_seq_len = max_seq_length
        self.batch_size = batch_size
        self.num_examples = {'train': -1, 'dev': -1, 'test': -1}

    def get_examples(self): 
        """load examples"""
        examples = []
0
0YuanZhang0 已提交
36
        index = 0
0
0YuanZhang0 已提交
37 38
        with open(self.data_file, 'r') as fr: 
            for line in fr: 
0
0YuanZhang0 已提交
39 40 41
                if index !=0 and index % 100 == 0: 
                    print("processing data: %d" % index)
                index += 1
0
0YuanZhang0 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                examples.append(line.strip())
        return examples

    def get_num_examples(self, phase): 
        """Get number of examples for train, dev or test."""
        if phase not in ['train', 'dev', 'test']: 
            raise ValueError(
                "Unknown phase, which should be in ['train', 'dev', 'test'].")
        count = len(open(self.data_file,'rU').readlines())
        self.num_examples[phase] = count
        return self.num_examples[phase]

    def data_generator(self,
                       place,
                       phase="train",
                       shuffle=True,
                       sample_pro=1):
        """
        Generate data for train, dev or test.

        Args:
            phase: string. The phase for which to generate data.
            shuffle: bool. Whether to shuffle examples.
            sample_pro: sample data ratio
        """
        examples = self.get_examples()
        if shuffle: 
            np.random.shuffle(examples)
        
        def batch_reader():  
            """read batch data"""
            batch = []
            for example in examples: 
                if sample_pro < 1:
                    if random.random() > sample_pro:
                        continue
                tokens = example.strip().split('\t')
0
0YuanZhang0 已提交
79 80 81 82 83 84
                
                if len(tokens) != 3: 
                    print("data format error: %s" % example.strip())
                    print("please input data: context \t response \t label")
                    continue

0
0YuanZhang0 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                context = [int(x) for x in tokens[0].split()[: self.max_seq_len]]
                response = [int(x) for x in tokens[1].split()[: self.max_seq_len]]
                label = [int(tokens[2])]
                instance = (context, response, label)

                if len(batch) < self.batch_size:
                    batch.append(instance)
                else:
                    if len(batch) == self.batch_size:
                        yield batch
                    batch = [instance]

            if len(batch) > 0: 
                yield batch

        def create_lodtensor(data_ids, place): 
            """create LodTensor for input ids"""
            cur_len = 0
            lod = [cur_len]
            seq_lens = [len(ids) for ids in data_ids]
            for l in seq_lens: 
                cur_len += l
                lod.append(cur_len)
            flattened_data = np.concatenate(data_ids, axis=0).astype("int64")
            flattened_data = flattened_data.reshape([len(flattened_data), 1])
            res = fluid.LoDTensor()
            res.set(flattened_data, place)
            res.set_lod([lod])
            return res

        def wrapper(): 
            """yield batch data to network""" 
            for batch_data in batch_reader(): 
                context_ids = [batch[0] for batch in batch_data]
                response_ids = [batch[1] for batch in batch_data]
                label_ids = [batch[2] for batch in batch_data]
                context_res = create_lodtensor(context_ids, place)
                response_res = create_lodtensor(response_ids, place)
                label_ids = np.array(label_ids).astype("int64").reshape([-1, 1])
                input_batch = [context_res, response_res, label_ids]
                yield input_batch
        
        return wrapper