reader.py 4.7 KB
Newer Older
H
hetianjian 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#  Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#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.

H
hetianjian 已提交
15 16 17 18 19 20 21 22 23 24 25
import numpy as np
import copy
import random
import pickle


class Data():
    def __init__(self, path, shuffle=False):
        data = pickle.load(open(path, 'rb'))
        self.shuffle = shuffle
        self.length = len(data[0])
H
hetianjian 已提交
26
        self.input = list(zip(data[0], data[1]))
H
hetianjian 已提交
27 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 56 57 58 59

    def make_data(self, cur_batch, batch_size):
        cur_batch = [list(e) for e in cur_batch]
        max_seq_len = 0
        for e in cur_batch:
            max_seq_len = max(max_seq_len, len(e[0]))
        last_id = []
        for e in cur_batch:
            last_id.append(len(e[0]) - 1)
            e[0] += [0] * (max_seq_len - len(e[0]))

        max_uniq_len = 0
        for e in cur_batch:
            max_uniq_len = max(max_uniq_len, len(np.unique(e[0])))

        items, adj_in, adj_out, seq_index, last_index = [], [], [], [], []
        mask, label = [], []

        id = 0
        for e in cur_batch:
            node = np.unique(e[0])
            items.append(node.tolist() + (max_uniq_len - len(node)) * [0])
            adj = np.zeros((max_uniq_len, max_uniq_len))

            for i in np.arange(len(e[0]) - 1):
                if e[0][i + 1] == 0:
                    break
                u = np.where(node == e[0][i])[0][0]
                v = np.where(node == e[0][i + 1])[0][0]
                adj[u][v] = 1

            u_deg_in = np.sum(adj, 0)
            u_deg_in[np.where(u_deg_in == 0)] = 1
60
            adj_in.append(np.divide(adj, u_deg_in).transpose())
H
hetianjian 已提交
61 62 63

            u_deg_out = np.sum(adj, 1)
            u_deg_out[np.where(u_deg_out == 0)] = 1
64
            adj_out.append(np.divide(adj.transpose(), u_deg_out).transpose())
H
hetianjian 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78

            seq_index.append(
                [np.where(node == i)[0][0] + id * max_uniq_len for i in e[0]])
            last_index.append(
                np.where(node == e[0][last_id[id]])[0][0] + id * max_uniq_len)
            label.append(e[1] - 1)
            mask.append([[1] * (last_id[id] + 1) + [0] *
                         (max_seq_len - last_id[id] - 1)])
            id += 1

        items = np.array(items).astype("int64").reshape((batch_size, -1, 1))
        seq_index = np.array(seq_index).astype("int32").reshape(
            (batch_size, -1))
        last_index = np.array(last_index).astype("int32").reshape(
79
            (batch_size))
H
hetianjian 已提交
80 81 82 83 84 85 86 87 88
        adj_in = np.array(adj_in).astype("float32").reshape(
            (batch_size, max_uniq_len, max_uniq_len))
        adj_out = np.array(adj_out).astype("float32").reshape(
            (batch_size, max_uniq_len, max_uniq_len))
        mask = np.array(mask).astype("float32").reshape((batch_size, -1, 1))
        label = np.array(label).astype("int64").reshape((batch_size, 1))
        return zip(items, seq_index, last_index, adj_in, adj_out, mask, label)

    def reader(self, batch_size, batch_group_size, train=True):
89 90 91 92 93 94 95 96 97 98 99 100
        def _reader():
            if self.shuffle:
                random.shuffle(self.input)
            group_remain = self.length % batch_group_size
            for bg_id in range(0, self.length - group_remain, batch_group_size):
                cur_bg = copy.deepcopy(self.input[bg_id:bg_id + batch_group_size])
                if train:
                    cur_bg = sorted(cur_bg, key=lambda x: len(x[0]), reverse=True)
                for i in range(0, batch_group_size, batch_size):
                    cur_batch = cur_bg[i:i + batch_size]
                    yield self.make_data(cur_batch, batch_size)

H
hutuxian 已提交
101 102
            #deal with the last batch group
            if group_remain == 0:
103 104
                return
            remain_data = copy.deepcopy(self.input[-group_remain:])
H
hetianjian 已提交
105
            if train:
106 107
                remain_data = sorted(
                    remain_data, key=lambda x: len(x[0]), reverse=True)
H
hutuxian 已提交
108 109
            for i in range(0, group_remain, batch_size):
                if i + batch_size <= group_remain:
110 111
                    cur_batch = remain_data[i:i + batch_size]
                    yield self.make_data(cur_batch, batch_size)
H
hutuxian 已提交
112 113 114
                else:
                    cur_batch = remain_data[i:]
                    yield self.make_data(cur_batch, group_remain % batch_size)
115
        return _reader
H
hetianjian 已提交
116 117 118 119 120 121


def read_config(path):
    with open(path, "r") as fin:
        item_num = int(fin.readline())
    return item_num