network_conf.py 1.3 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 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
import paddle.fluid as fluid
import math


def skip_gram_word2vec(dict_size, embedding_size):
    input_word = fluid.layers.data(name="input_word", shape=[1], dtype='int64')

    emb = fluid.layers.embedding(
        input=input_word,
        size=[dict_size, embedding_size],
        param_attr=fluid.ParamAttr(initializer=fluid.initializer.Normal(
            scale=1 / math.sqrt(dict_size))))

    predict_word = fluid.layers.data(
        name='predict_word', shape=[1], dtype='int64')

    data_list = [input_word, predict_word]

    w_param_name = "nce_w"
    fluid.default_main_program().global_block().create_parameter(
        shape=[dict_size, embedding_size], dtype='float32', name=w_param_name)

    b_param_name = "nce_b"
    fluid.default_main_program().global_block().create_parameter(
        shape=[dict_size, 1], dtype='float32', name=b_param_name)

    cost = fluid.layers.nce(input=emb,
                            label=predict_word,
                            num_total_classes=dict_size,
                            param_attr=fluid.ParamAttr(name=w_param_name),
                            bias_attr=fluid.ParamAttr(name=b_param_name),
                            num_neg_samples=5)
    avg_cost = fluid.layers.reduce_mean(cost)

    return avg_cost, data_list