test_word2vec.py 3.7 KB
Newer Older
Q
QI JUN 已提交
1
import paddle.v2 as paddle
Q
Qiao Longfei 已提交
2 3 4
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer
5
import paddle.v2.fluid.framework as framework
Q
Qiao Longfei 已提交
6
from paddle.v2.fluid.executor import Executor
Q
QI JUN 已提交
7 8 9

import numpy as np

10 11 12
PASS_NUM = 100
EMBED_SIZE = 32
HIDDEN_SIZE = 256
Q
QI JUN 已提交
13
N = 5
14 15
BATCH_SIZE = 32
IS_SPARSE = True
Q
QI JUN 已提交
16 17 18 19 20 21 22

word_dict = paddle.dataset.imikolov.build_dict()
dict_size = len(word_dict)

first_word = layers.data(
    name='firstw',
    shape=[1],
23
    data_type='int64')
Q
QI JUN 已提交
24 25 26
second_word = layers.data(
    name='secondw',
    shape=[1],
27
    data_type='int64')
Q
QI JUN 已提交
28 29 30
third_word = layers.data(
    name='thirdw',
    shape=[1],
31
    data_type='int64')
Q
QI JUN 已提交
32 33 34
forth_word = layers.data(
    name='forthw',
    shape=[1],
35
    data_type='int64')
Q
QI JUN 已提交
36 37 38
next_word = layers.data(
    name='nextw',
    shape=[1],
39
    data_type='int64')
Q
QI JUN 已提交
40 41 42

embed_first = layers.embedding(
    input=first_word,
43
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
44
    data_type='float32',
45 46
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
47 48
embed_second = layers.embedding(
    input=second_word,
49
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
50
    data_type='float32',
51 52
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
53 54
embed_third = layers.embedding(
    input=third_word,
55
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
56
    data_type='float32',
57 58
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
59 60
embed_forth = layers.embedding(
    input=forth_word,
61
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
62
    data_type='float32',
63 64
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
65 66 67

concat_embed = layers.concat(
    input=[embed_first, embed_second, embed_third, embed_forth],
68
    axis=1)
Q
QI JUN 已提交
69
hidden1 = layers.fc(input=concat_embed,
70 71
                    size=HIDDEN_SIZE,
                    act='sigmoid')
Q
QI JUN 已提交
72 73
predict_word = layers.fc(input=hidden1,
                         size=dict_size,
74
                         act='softmax')
Q
QI JUN 已提交
75 76
cost = layers.cross_entropy(
    input=predict_word,
77 78
    label=next_word)
avg_cost = layers.mean(x=cost)
Q
QI JUN 已提交
79
sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001)
80
opts = sgd_optimizer.minimize(avg_cost)
Q
QI JUN 已提交
81 82

train_reader = paddle.batch(
83
    paddle.dataset.imikolov.train(word_dict, N), BATCH_SIZE)
Q
QI JUN 已提交
84 85 86 87

place = core.CPUPlace()
exe = Executor(place)

T
update  
typhoonzero 已提交
88 89 90 91
# fix https://github.com/PaddlePaddle/Paddle/issues/5434 then remove
# below exit line.
exit(0)

92 93
exe.run(framework.default_startup_program())

Q
QI JUN 已提交
94 95 96
for pass_id in range(PASS_NUM):
    for data in train_reader():
        input_data = [[data_idx[idx] for data_idx in data] for idx in xrange(5)]
97
        input_data = map(lambda x: np.array(x).astype("int64"), input_data)
Q
QI JUN 已提交
98 99 100 101 102 103
        input_data = map(lambda x: np.expand_dims(x, axis=1), input_data)

        first_data = input_data[0]
        first_tensor = core.LoDTensor()
        first_tensor.set(first_data, place)

104
        second_data = input_data[1]
Q
QI JUN 已提交
105 106 107
        second_tensor = core.LoDTensor()
        second_tensor.set(second_data, place)

108
        third_data = input_data[2]
Q
QI JUN 已提交
109 110 111
        third_tensor = core.LoDTensor()
        third_tensor.set(third_data, place)

112
        forth_data = input_data[3]
Q
QI JUN 已提交
113 114 115
        forth_tensor = core.LoDTensor()
        forth_tensor.set(forth_data, place)

116
        next_data = input_data[4]
Q
QI JUN 已提交
117 118 119
        next_tensor = core.LoDTensor()
        next_tensor.set(next_data, place)

120
        outs = exe.run(framework.default_main_program(),
Q
QI JUN 已提交
121 122 123 124 125 126 127 128 129 130 131 132
                       feed={
                           'firstw': first_tensor,
                           'secondw': second_tensor,
                           'thirdw': third_tensor,
                           'forthw': forth_tensor,
                           'nextw': next_tensor
                       },
                       fetch_list=[avg_cost])
        out = np.array(outs[0])
        if out[0] < 10.0:
            exit(0)  # if avg cost less than 10.0, we think our code is good.
exit(1)