utils.py 2.9 KB
Newer Older
R
root 已提交
1 2
import sys
import time
G
gmcather 已提交
3 4 5 6 7
import numpy as np

import paddle.fluid as fluid
import paddle.v2 as paddle

R
root 已提交
8 9 10
import light_imdb
import tiny_imdb

G
gmcather 已提交
11

R
root 已提交
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 46 47
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 已提交
48
    input_seq = to_lodtensor(map(lambda x: x[0], data), place)
R
root 已提交
49 50 51 52 53
    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 已提交
54 55 56 57
def prepare_data(data_type="imdb",
                 self_dict=False,
                 batch_size=128,
                 buf_size=50000):
R
root 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    """
    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 已提交
76 77
                paddle.dataset.imdb.train(word_dict), buf_size=buf_size),
            batch_size=batch_size)
G
gmcather 已提交
78

R
root 已提交
79 80
        test_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
81 82
                paddle.dataset.imdb.test(word_dict), buf_size=buf_size),
            batch_size=batch_size)
G
gmcather 已提交
83

R
root 已提交
84 85 86
    elif data_type == "light_imdb":
        train_reader = paddle.batch(
            paddle.reader.shuffle(
G
gmcather 已提交
87 88
                light_imdb.train(word_dict), buf_size=buf_size),
            batch_size=batch_size)
G
gmcather 已提交
89

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

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

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

    return word_dict, train_reader, test_reader