network_conf.py 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
neural network for word2vec
"""

from __future__ import print_function

Q
Qiao Longfei 已提交
20
import math
21
import numpy as np
Q
Qiao Longfei 已提交
22

23 24
import paddle.fluid as fluid

25 26 27 28 29 30 31 32 33

def skip_gram_word2vec(dict_size,
                       word_frequencys,
                       embedding_size,
                       max_code_length=None,
                       with_hsigmoid=False,
                       with_nce=True):
    def nce_layer(input, label, embedding_size, num_total_classes,
                  num_neg_samples, sampler, custom_dist, sample_weight):
34 35 36 37 38 39 40
        # convert word_frequencys to tensor
        nid_freq_arr = np.array(word_frequencys).astype('float32')
        nid_freq_var = fluid.layers.assign(input=nid_freq_arr)

        w_param_name = "nce_w"
        b_param_name = "nce_b"
        w_param = fluid.default_main_program().global_block().create_parameter(
41 42 43
            shape=[num_total_classes, embedding_size],
            dtype='float32',
            name=w_param_name)
44 45 46
        b_param = fluid.default_main_program().global_block().create_parameter(
            shape=[num_total_classes, 1], dtype='float32', name=b_param_name)

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
        cost = fluid.layers.nce(input=input,
                                label=label,
                                num_total_classes=num_total_classes,
                                sampler=sampler,
                                custom_dist=nid_freq_var,
                                sample_weight=sample_weight,
                                param_attr=fluid.ParamAttr(name=w_param_name),
                                bias_attr=fluid.ParamAttr(name=b_param_name),
                                num_neg_samples=num_neg_samples)

        return cost

    def hsigmoid_layer(input, label, non_leaf_num, max_code_length, data_list):
        hs_cost = None
        ptable = None
        pcode = None
        if max_code_length != None:
            ptable = fluid.layers.data(
                name='ptable', shape=[max_code_length], dtype='int64')
            pcode = fluid.layers.data(
                name='pcode', shape=[max_code_length], dtype='int64')
            data_list.append(pcode)
            data_list.append(ptable)
        else:
            ptable = fluid.layers.data(name='ptable', shape=[40], dtype='int64')
            pcode = fluid.layers.data(name='pcode', shape=[40], dtype='int64')
            data_list.append(pcode)
            data_list.append(ptable)
        if non_leaf_num == None:
            non_leaf_num = dict_size

        cost = fluid.layers.hsigmoid(
            input=emb,
            label=predict_word,
            non_leaf_num=non_leaf_num,
            ptable=ptable,
            pcode=pcode,
            is_costum=True)
85 86

        return cost
Q
Qiao Longfei 已提交
87 88

    input_word = fluid.layers.data(name="input_word", shape=[1], dtype='int64')
89 90 91
    predict_word = fluid.layers.data(
        name='predict_word', shape=[1], dtype='int64')
    cost = None
92
    data_list = [input_word, predict_word]
Q
Qiao Longfei 已提交
93 94 95 96

    emb = fluid.layers.embedding(
        input=input_word,
        size=[dict_size, embedding_size],
97 98 99 100 101 102 103 104 105
        param_attr=fluid.ParamAttr(initializer=fluid.initializer.Normal(
            scale=1 / math.sqrt(dict_size))))

    if with_nce:
        cost = nce_layer(emb, predict_word, embedding_size, dict_size, 5,
                         "uniform", word_frequencys, None)
    if with_hsigmoid:
        cost = hsigmoid_layer(emb, predict_word, dict_size, max_code_length,
                              data_list)
Q
Qiao Longfei 已提交
106 107 108 109

    avg_cost = fluid.layers.reduce_mean(cost)

    return avg_cost, data_list