net.py 5.5 KB
Newer Older
S
slf12 已提交
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
import math
import numpy as np
B
Bai Yifan 已提交
20
import paddle
S
slf12 已提交
21 22 23 24 25 26
import paddle.fluid as fluid


def skip_gram_word2vec(dict_size, embedding_size, is_sparse=False, neg_num=5):

    datas = []
B
Bai Yifan 已提交
27 28 29 30 31 32
    input_word = paddle.static.data(
        name="input_word", shape=[None, 1], dtype='int64')
    true_word = paddle.static.data(
        name='true_label', shape=[None, 1], dtype='int64')
    neg_word = paddle.static.data(
        name="neg_label", shape=[None, neg_num], dtype='int64')
S
slf12 已提交
33 34 35 36 37 38 39 40 41

    datas.append(input_word)
    datas.append(true_word)
    datas.append(neg_word)

    py_reader = fluid.layers.create_py_reader_by_data(
        capacity=64, feed_list=datas, name='py_reader', use_double_buffer=True)

    words = fluid.layers.read_file(py_reader)
B
Bai Yifan 已提交
42 43
    words[0] = paddle.reshape(words[0], [-1])
    words[1] = paddle.reshape(words[1], [-1])
S
slf12 已提交
44
    init_width = 0.5 / embedding_size
B
Bai Yifan 已提交
45
    input_emb = paddle.static.nn.embedding(
S
slf12 已提交
46 47 48
        input=words[0],
        is_sparse=is_sparse,
        size=[dict_size, embedding_size],
B
Bai Yifan 已提交
49
        param_attr=paddle.ParamAttr(
S
slf12 已提交
50
            name='emb',
B
Bai Yifan 已提交
51
            initializer=paddle.nn.initializer.Uniform(-init_width, init_width)))
S
slf12 已提交
52

B
Bai Yifan 已提交
53
    true_emb_w = paddle.static.nn.embedding(
S
slf12 已提交
54 55 56
        input=words[1],
        is_sparse=is_sparse,
        size=[dict_size, embedding_size],
B
Bai Yifan 已提交
57 58 59
        param_attr=paddle.ParamAttr(
            name='emb_w',
            initializer=paddle.nn.initializer.Constant(value=0.0)))
S
slf12 已提交
60

B
Bai Yifan 已提交
61
    true_emb_b = paddle.static.nn.embedding(
S
slf12 已提交
62 63 64
        input=words[1],
        is_sparse=is_sparse,
        size=[dict_size, 1],
B
Bai Yifan 已提交
65 66 67 68
        param_attr=paddle.ParamAttr(
            name='emb_b',
            initializer=paddle.nn.initializer.Constant(value=0.0)))
    neg_word_reshape = paddle.reshape(words[2], shape=[-1])
S
slf12 已提交
69 70
    neg_word_reshape.stop_gradient = True

B
Bai Yifan 已提交
71
    neg_emb_w = paddle.static.nn.embedding(
S
slf12 已提交
72 73 74
        input=neg_word_reshape,
        is_sparse=is_sparse,
        size=[dict_size, embedding_size],
B
Bai Yifan 已提交
75
        param_attr=paddle.ParamAttr(
S
slf12 已提交
76 77
            name='emb_w', learning_rate=1.0))

B
Bai Yifan 已提交
78
    neg_emb_w_re = paddle.reshape(
S
slf12 已提交
79
        neg_emb_w, shape=[-1, neg_num, embedding_size])
B
Bai Yifan 已提交
80
    neg_emb_b = paddle.static.nn.embedding(
S
slf12 已提交
81 82 83
        input=neg_word_reshape,
        is_sparse=is_sparse,
        size=[dict_size, 1],
B
Bai Yifan 已提交
84
        param_attr=paddle.ParamAttr(
S
slf12 已提交
85 86
            name='emb_b', learning_rate=1.0))

B
Bai Yifan 已提交
87 88 89 90 91
    neg_emb_b_vec = paddle.reshape(neg_emb_b, shape=[-1, neg_num])
    true_logits = paddle.add(paddle.mean(
        paddle.multiply(input_emb, true_emb_w), axis=1, keepdim=True),
                             true_emb_b)
    input_emb_re = paddle.reshape(input_emb, shape=[-1, 1, embedding_size])
S
slf12 已提交
92 93
    neg_matmul = fluid.layers.matmul(
        input_emb_re, neg_emb_w_re, transpose_y=True)
B
Bai Yifan 已提交
94 95
    neg_matmul_re = paddle.reshape(neg_matmul, shape=[-1, neg_num])
    neg_logits = paddle.add(neg_matmul_re, neg_emb_b_vec)
S
slf12 已提交
96 97 98 99 100 101 102 103 104 105 106
    #nce loss

    label_ones = fluid.layers.fill_constant_batch_size_like(
        true_logits, shape=[-1, 1], value=1.0, dtype='float32')
    label_zeros = fluid.layers.fill_constant_batch_size_like(
        true_logits, shape=[-1, neg_num], value=0.0, dtype='float32')

    true_xent = fluid.layers.sigmoid_cross_entropy_with_logits(true_logits,
                                                               label_ones)
    neg_xent = fluid.layers.sigmoid_cross_entropy_with_logits(neg_logits,
                                                              label_zeros)
B
Bai Yifan 已提交
107 108 109
    cost = paddle.add(paddle.sum(true_xent, axis=1),
                      paddle.sum(neg_xent, axis=1))
    avg_cost = paddle.mean(cost)
S
slf12 已提交
110 111 112 113
    return avg_cost, py_reader


def infer_network(vocab_size, emb_size):
B
Bai Yifan 已提交
114 115 116 117 118 119 120 121 122 123
    analogy_a = paddle.static.data(
        name="analogy_a", shape=[None, 1], dtype='int64')
    analogy_b = paddle.static.data(
        name="analogy_b", shape=[None, 1], dtype='int64')
    analogy_c = paddle.static.data(
        name="analogy_c", shape=[None, 1], dtype='int64')
    all_label = paddle.static.data(
        name="all_label", shape=[vocab_size, 1], dtype='int64')
    all_label = paddle.reshape(all_label, [-1])
    emb_all_label = paddle.static.nn.embedding(
S
slf12 已提交
124 125
        input=all_label, size=[vocab_size, emb_size], param_attr="emb")

B
Bai Yifan 已提交
126 127
    analogy_a = paddle.reshape(analogy_a, [-1])
    emb_a = paddle.static.nn.embedding(
S
slf12 已提交
128
        input=analogy_a, size=[vocab_size, emb_size], param_attr="emb")
B
Bai Yifan 已提交
129 130
    analogy_b = paddle.reshape(analogy_b, [-1])
    emb_b = paddle.static.nn.embedding(
S
slf12 已提交
131
        input=analogy_b, size=[vocab_size, emb_size], param_attr="emb")
B
Bai Yifan 已提交
132 133
    analogy_c = paddle.reshape(analogy_c, [-1])
    emb_c = paddle.static.nn.embedding(
S
slf12 已提交
134
        input=analogy_c, size=[vocab_size, emb_size], param_attr="emb")
B
Bai Yifan 已提交
135
    target = paddle.add(paddle.add(emb_b, -emb_a), emb_c)
S
slf12 已提交
136 137
    emb_all_label_l2 = fluid.layers.l2_normalize(x=emb_all_label, axis=1)
    dist = fluid.layers.matmul(x=target, y=emb_all_label_l2, transpose_y=True)
B
Bai Yifan 已提交
138
    values, pred_idx = paddle.topk(x=dist, k=4)
S
slf12 已提交
139
    return values, pred_idx