utils.py 2.9 KB
Newer Older
R
root 已提交
1 2 3 4 5 6 7 8
import paddle.fluid as fluid
import paddle.v2 as paddle
import numpy as np
import sys
import time
import light_imdb
import tiny_imdb

G
gmcather 已提交
9

R
root 已提交
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 36 37 38 39 40 41 42 43 44 45
def to_lodtensor(data, place):
    """
    convert to LODtensor
    """
    seq_lens = [len(seq) for seq in data]
    cur_len = 0
    lod = [cur_len]
    for l in seq_lens:
        cur_len += l
        lod.append(cur_len)
    flattened_data = np.concatenate(data, 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 load_vocab(filename):
    """
    load imdb vocabulary
    """
    vocab = {}
    with open(filename) as f:
        wid = 0
        for line in f:
            vocab[line.strip()] = wid
            wid += 1
    vocab["<unk>"] = len(vocab)
    return vocab


def data2tensor(data, place):
    """
    data2tensor
    """
G
gmcather 已提交
46
    input_seq = to_lodtensor(map(lambda x: x[0], data), place)
R
root 已提交
47 48 49 50 51
    y_data = np.array(map(lambda x: x[1], data)).astype("int64")
    y_data = y_data.reshape([-1, 1])
    return {"words": input_seq, "label": y_data}


G
gmcather 已提交
52 53 54 55
def prepare_data(data_type="imdb",
                 self_dict=False,
                 batch_size=128,
                 buf_size=50000):
R
root 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    """
    prepare data
    """
    if self_dict:
        word_dict = load_vocab(data_type + ".vocab")
    else:
        if data_type == "imdb":
            word_dict = paddle.dataset.imdb.word_dict()
        elif data_type == "light_imdb":
            word_dict = light_imdb.word_dict()
        elif data_type == "tiny_imdb":
            word_dict = tiny_imdb.word_dict()
        else:
            raise RuntimeError("No such dataset")

    if data_type == "imdb":
        train_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
74 75
                paddle.dataset.imdb.train(word_dict), buf_size=buf_size),
            batch_size=batch_size)
G
gmcather 已提交
76

R
root 已提交
77 78
        test_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
79 80
                paddle.dataset.imdb.test(word_dict), buf_size=buf_size),
            batch_size=batch_size)
R
root 已提交
81 82 83 84
    
    elif data_type == "light_imdb":
        train_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
85 86
                light_imdb.train(word_dict), buf_size=buf_size),
            batch_size=batch_size)
G
gmcather 已提交
87

R
root 已提交
88 89
        test_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
90 91
                light_imdb.test(word_dict), buf_size=buf_size),
            batch_size=batch_size)
R
root 已提交
92 93

    elif data_type == "tiny_imdb":
G
gmcather 已提交
94
        train_reader=paddle.batch(
R
root 已提交
95
            paddle.reader.shuffle(
G
gmcather 已提交
96 97
                tiny_imdb.train(word_dict), buf_size=buf_size),
            batch_size=batch_size)
G
gmcather 已提交
98

R
root 已提交
99 100
        test_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
101 102
                tiny_imdb.test(word_dict), buf_size=buf_size),
            batch_size=batch_size)
R
root 已提交
103 104 105 106
    else:
        raise RuntimeError("no such dataset")

    return word_dict, train_reader, test_reader