test_understand_sentiment_lstm.py 3.4 KB
Newer Older
Q
Qiao Longfei 已提交
1
import numpy as np
Y
Yang Yang(Tony) 已提交
2
import paddle.v2 as paddle
3
import paddle.v2.fluid as fluid
Y
Yang Yang(Tony) 已提交
4 5 6


def lstm_net(dict_dim, class_dim=2, emb_dim=32, seq_len=80, batch_size=50):
7
    data = fluid.layers.data(
Y
Yang Yang(Tony) 已提交
8 9 10
        name="words",
        shape=[seq_len * batch_size, 1],
        append_batch_size=False,
F
fengjiayi 已提交
11
        dtype="int64")
12
    label = fluid.layers.data(
Y
Yang Yang(Tony) 已提交
13 14 15
        name="label",
        shape=[batch_size, 1],
        append_batch_size=False,
F
fengjiayi 已提交
16
        dtype="int64")
Y
Yang Yang(Tony) 已提交
17

18 19 20
    emb = fluid.layers.embedding(input=data, size=[dict_dim, emb_dim])
    emb = fluid.layers.reshape(x=emb, shape=[batch_size, seq_len, emb_dim])
    emb = fluid.layers.transpose(x=emb, axis=[1, 0, 2])
Y
Yang Yang(Tony) 已提交
21

22
    c_pre_init = fluid.layers.fill_constant(
F
fengjiayi 已提交
23
        dtype=emb.dtype, shape=[batch_size, emb_dim], value=0.0)
24 25 26
    layer_1_out = fluid.layers.lstm(
        emb, c_pre_init=c_pre_init, hidden_dim=emb_dim)
    layer_1_out = fluid.layers.transpose(x=layer_1_out, axis=[1, 0, 2])
Y
Yang Yang(Tony) 已提交
27

28 29 30 31
    prediction = fluid.layers.fc(input=layer_1_out,
                                 size=class_dim,
                                 act="softmax")
    cost = fluid.layers.cross_entropy(input=prediction, label=label)
Y
Yang Yang(Tony) 已提交
32

33 34 35 36
    avg_cost = fluid.layers.mean(x=cost)
    adam_optimizer = fluid.optimizer.Adam(learning_rate=0.002)
    adam_optimizer.minimize(avg_cost)
    acc = fluid.layers.accuracy(input=prediction, label=label)
Y
Yang Yang(Tony) 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49

    return avg_cost, acc


def to_lodtensor(data, place):
    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])
50
    res = fluid.LoDTensor()
Y
Yang Yang(Tony) 已提交
51 52 53 54 55
    res.set(flattened_data, place)
    res.set_lod([lod])
    return res


56
def chop_data(data, chop_len=80, batch_size=50):
Y
Yang Yang(Tony) 已提交
57 58
    data = [(x[0][:chop_len], x[1]) for x in data if len(x[0]) >= chop_len]

59
    return data[:batch_size]
Y
Yang Yang(Tony) 已提交
60 61 62 63 64 65


def prepare_feed_data(data, place):
    tensor_words = to_lodtensor(map(lambda x: x[0], data), place)

    label = np.array(map(lambda x: x[1], data)).astype("int64")
66
    label = label.reshape([len(label), 1])
67
    tensor_label = fluid.LoDTensor()
Y
Yang Yang(Tony) 已提交
68 69 70 71 72 73
    tensor_label.set(label, place)

    return tensor_words, tensor_label


def main():
74 75
    BATCH_SIZE = 100
    PASS_NUM = 5
Y
Yang Yang(Tony) 已提交
76

77 78 79 80
    word_dict = paddle.dataset.imdb.word_dict()
    print "load word dict successfully"
    dict_dim = len(word_dict)
    class_dim = 2
Y
Yang Yang(Tony) 已提交
81

82
    cost, acc = lstm_net(dict_dim=dict_dim, class_dim=class_dim)
Y
Yang Yang(Tony) 已提交
83

84 85 86 87
    train_data = paddle.batch(
        paddle.reader.shuffle(
            paddle.dataset.imdb.train(word_dict), buf_size=BATCH_SIZE * 10),
        batch_size=BATCH_SIZE)
88 89
    place = fluid.CPUPlace()
    exe = fluid.Executor(place)
90

91
    exe.run(fluid.default_startup_program())
Y
Yang Yang(Tony) 已提交
92

93 94 95 96 97
    for pass_id in xrange(PASS_NUM):
        for data in train_data():
            chopped_data = chop_data(data)
            tensor_words, tensor_label = prepare_feed_data(chopped_data, place)

98
            outs = exe.run(fluid.default_main_program(),
99 100 101 102 103 104 105 106 107 108
                           feed={"words": tensor_words,
                                 "label": tensor_label},
                           fetch_list=[cost, acc])
            cost_val = np.array(outs[0])
            acc_val = np.array(outs[1])

            print("cost=" + str(cost_val) + " acc=" + str(acc_val))
            if acc_val > 0.7:
                exit(0)
    exit(1)
Y
Yang Yang(Tony) 已提交
109 110 111 112


if __name__ == '__main__':
    main()