nce_conf.py 2.4 KB
Newer Older
P
pakchoi 已提交
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
# -*- encoding:utf-8 -*-
import math
import paddle.v2 as paddle


def network_conf(hidden_size, embedding_size, dict_size, is_train):

    first_word = paddle.layer.data(
        name="firstw", type=paddle.data_type.integer_value(dict_size))
    second_word = paddle.layer.data(
        name="secondw", type=paddle.data_type.integer_value(dict_size))
    third_word = paddle.layer.data(
        name="thirdw", type=paddle.data_type.integer_value(dict_size))
    fourth_word = paddle.layer.data(
        name="fourthw", type=paddle.data_type.integer_value(dict_size))
    next_word = paddle.layer.data(
        name="fifthw", type=paddle.data_type.integer_value(dict_size))

    embed_param_attr = paddle.attr.Param(
        name="_proj", initial_std=0.001, learning_rate=1, l2_rate=0)
    first_embedding = paddle.layer.embedding(
        input=first_word, size=embedding_size, param_attr=embed_param_attr)
    second_embedding = paddle.layer.embedding(
        input=second_word, size=embedding_size, param_attr=embed_param_attr)
    third_embedding = paddle.layer.embedding(
        input=third_word, size=embedding_size, param_attr=embed_param_attr)
    fourth_embedding = paddle.layer.embedding(
        input=fourth_word, size=embedding_size, param_attr=embed_param_attr)

    context_embedding = paddle.layer.concat(input=[
        first_embedding, second_embedding, third_embedding, fourth_embedding
    ])

    hidden_layer = paddle.layer.fc(
        input=context_embedding,
        size=hidden_size,
        act=paddle.activation.Tanh(),
        bias_attr=paddle.attr.Param(learning_rate=1),
        param_attr=paddle.attr.Param(
            initial_std=1. / math.sqrt(embedding_size * 8), learning_rate=1))

    if is_train == True:
        cost = paddle.layer.nce(
            input=hidden_layer,
            label=next_word,
            num_classes=dict_size,
            param_attr=paddle.attr.Param(name='nce_w'),
            bias_attr=paddle.attr.Param(name='nce_b'),
            act=paddle.activation.Sigmoid(),
            num_neg_samples=25,
            neg_distribution=None)
        return cost
    else:
        with paddle.layer.mixed(
                size=dict_size,
                act=paddle.activation.Softmax(),
                bias_attr=paddle.attr.Param(name='nce_b')) as prediction:
            prediction += paddle.layer.trans_full_matrix_projection(
                input=hidden_layer, param_attr=paddle.attr.Param(name='nce_w'))

        return prediction