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

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

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

Q
Qiao Longfei 已提交
19 20 21 22 23
first_word = layers.data(name='firstw', shape=[1], data_type='int64')
second_word = layers.data(name='secondw', shape=[1], data_type='int64')
third_word = layers.data(name='thirdw', shape=[1], data_type='int64')
forth_word = layers.data(name='forthw', shape=[1], data_type='int64')
next_word = layers.data(name='nextw', shape=[1], data_type='int64')
Q
QI JUN 已提交
24 25 26

embed_first = layers.embedding(
    input=first_word,
27
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
28
    data_type='float32',
29 30
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
31 32
embed_second = layers.embedding(
    input=second_word,
33
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
34
    data_type='float32',
35 36
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
37 38
embed_third = layers.embedding(
    input=third_word,
39
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
40
    data_type='float32',
41 42
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
43 44
embed_forth = layers.embedding(
    input=forth_word,
45
    size=[dict_size, EMBED_SIZE],
Q
QI JUN 已提交
46
    data_type='float32',
47 48
    is_sparse=IS_SPARSE,
    param_attr={'name': 'shared_w'})
Q
QI JUN 已提交
49 50

concat_embed = layers.concat(
Q
Qiao Longfei 已提交
51 52 53 54
    input=[embed_first, embed_second, embed_third, embed_forth], axis=1)
hidden1 = layers.fc(input=concat_embed, size=HIDDEN_SIZE, act='sigmoid')
predict_word = layers.fc(input=hidden1, size=dict_size, act='softmax')
cost = layers.cross_entropy(input=predict_word, label=next_word)
55
avg_cost = layers.mean(x=cost)
Q
Qiao Longfei 已提交
56
sgd_optimizer = SGDOptimizer(learning_rate=0.001)
57
opts = sgd_optimizer.minimize(avg_cost)
Q
QI JUN 已提交
58 59

train_reader = paddle.batch(
60
    paddle.dataset.imikolov.train(word_dict, N), BATCH_SIZE)
Q
QI JUN 已提交
61 62 63 64

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

T
update  
typhoonzero 已提交
65 66 67 68
# fix https://github.com/PaddlePaddle/Paddle/issues/5434 then remove
# below exit line.
exit(0)

69 70
exe.run(framework.default_startup_program())

Q
QI JUN 已提交
71 72 73
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)]
74
        input_data = map(lambda x: np.array(x).astype("int64"), input_data)
Q
QI JUN 已提交
75 76 77 78 79 80
        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)

81
        second_data = input_data[1]
Q
QI JUN 已提交
82 83 84
        second_tensor = core.LoDTensor()
        second_tensor.set(second_data, place)

85
        third_data = input_data[2]
Q
QI JUN 已提交
86 87 88
        third_tensor = core.LoDTensor()
        third_tensor.set(third_data, place)

89
        forth_data = input_data[3]
Q
QI JUN 已提交
90 91 92
        forth_tensor = core.LoDTensor()
        forth_tensor.set(forth_data, place)

93
        next_data = input_data[4]
Q
QI JUN 已提交
94 95 96
        next_tensor = core.LoDTensor()
        next_tensor.set(next_data, place)

97
        outs = exe.run(framework.default_main_program(),
Q
QI JUN 已提交
98 99 100 101 102 103 104 105 106 107 108 109
                       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)