nets.py 8.6 KB
Newer Older
Q
Qiao Longfei 已提交
1
# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved
D
dongdaxiang 已提交
2 3 4 5 6 7 8 9 10 11 12 13
#
# 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.
D
dongdaxiang 已提交
14

D
dongdaxiang 已提交
15
import paddle.fluid as fluid
16 17 18 19
import paddle.fluid.layers.tensor as tensor
import paddle.fluid.layers.control_flow as cf
import paddle.fluid.layers.io as io

D
dongdaxiang 已提交
20 21 22

class BowEncoder(object):
    """ bow-encoder """
23

D
dongdaxiang 已提交
24 25 26 27
    def __init__(self):
        self.param_name = ""

    def forward(self, emb):
Z
zhang wenhui 已提交
28
        return fluid.layers.sequence_pool(input=emb, pool_type='sum')
29

D
dongdaxiang 已提交
30 31 32

class CNNEncoder(object):
    """ cnn-encoder"""
33 34

    def __init__(self,
Z
zhangwenhui03 已提交
35
                 param_name="cnn",
D
dongdaxiang 已提交
36 37 38 39 40 41 42 43 44
                 win_size=3,
                 ksize=128,
                 act='tanh',
                 pool_type='max'):
        self.param_name = param_name
        self.win_size = win_size
        self.ksize = ksize
        self.act = act
        self.pool_type = pool_type
45

D
dongdaxiang 已提交
46 47 48 49 50 51 52
    def forward(self, emb):
        return fluid.nets.sequence_conv_pool(
            input=emb,
            num_filters=self.ksize,
            filter_size=self.win_size,
            act=self.act,
            pool_type=self.pool_type,
Z
zhangwenhui03 已提交
53 54
            param_attr=self.param_name + ".param",
            bias_attr=self.param_name + ".bias")
D
dongdaxiang 已提交
55

56

D
dongdaxiang 已提交
57 58
class GrnnEncoder(object):
    """ grnn-encoder """
59

Z
zhangwenhui03 已提交
60
    def __init__(self, param_name="grnn", hidden_size=128):
D
dongdaxiang 已提交
61
        self.param_name = param_name
D
dongdaxiang 已提交
62
        self.hidden_size = hidden_size
63

D
dongdaxiang 已提交
64
    def forward(self, emb):
Z
zhang wenhui 已提交
65 66 67 68
        fc0 = fluid.layers.fc(input=emb,
                              size=self.hidden_size * 3,
                              param_attr=self.param_name + "_fc.w",
                              bias_attr=False)
Z
zhang wenhui 已提交
69

Z
zhang wenhui 已提交
70
        gru_h = fluid.layers.dynamic_gru(
D
dongdaxiang 已提交
71
            input=fc0,
72 73
            size=self.hidden_size,
            is_reverse=False,
Z
zhangwenhui03 已提交
74 75
            param_attr=self.param_name + ".param",
            bias_attr=self.param_name + ".bias")
Z
zhang wenhui 已提交
76
        return fluid.layers.sequence_pool(input=gru_h, pool_type='max')
77

D
dongdaxiang 已提交
78

D
dongdaxiang 已提交
79 80
'''this is a very simple Encoder factory
most default argument values are used'''
81 82


D
dongdaxiang 已提交
83 84 85 86
class SimpleEncoderFactory(object):
    def __init__(self):
        pass

D
dongdaxiang 已提交
87
    ''' create an encoder through create function '''
88

D
dongdaxiang 已提交
89 90 91 92 93 94 95 96 97 98 99
    def create(self, enc_type, enc_hid_size):
        if enc_type == "bow":
            bow_encode = BowEncoder()
            return bow_encode
        elif enc_type == "cnn":
            cnn_encode = CNNEncoder(ksize=enc_hid_size)
            return cnn_encode
        elif enc_type == "gru":
            rnn_encode = GrnnEncoder(hidden_size=enc_hid_size)
            return rnn_encode

100

D
dongdaxiang 已提交
101 102
class MultiviewSimnet(object):
    """ multi-view simnet """
103 104

    def __init__(self, embedding_size, embedding_dim, hidden_size):
D
dongdaxiang 已提交
105 106
        self.embedding_size = embedding_size
        self.embedding_dim = embedding_dim
107
        self.emb_shape = [self.embedding_size, self.embedding_dim]
D
dongdaxiang 已提交
108 109 110 111 112 113 114 115 116 117
        self.hidden_size = hidden_size
        self.margin = 0.1

    def set_query_encoder(self, encoders):
        self.query_encoders = encoders

    def set_title_encoder(self, encoders):
        self.title_encoders = encoders

    def get_correct(self, x, y):
118
        less = tensor.cast(cf.less_than(x, y), dtype='float32')
Z
zhang wenhui 已提交
119
        correct = fluid.layers.reduce_sum(less)
D
dongdaxiang 已提交
120 121 122 123
        return correct

    def train_net(self):
        # input fields for query, pos_title, neg_title
124
        q_slots = [
Z
zhang wenhui 已提交
125 126
            fluid.data(
                name="q%d" % i, shape=[None, 1], lod_level=1, dtype='int64')
127 128 129
            for i in range(len(self.query_encoders))
        ]
        pt_slots = [
Z
zhang wenhui 已提交
130 131
            fluid.data(
                name="pt%d" % i, shape=[None, 1], lod_level=1, dtype='int64')
132 133 134
            for i in range(len(self.title_encoders))
        ]
        nt_slots = [
Z
zhang wenhui 已提交
135 136
            fluid.data(
                name="nt%d" % i, shape=[None, 1], lod_level=1, dtype='int64')
137 138
            for i in range(len(self.title_encoders))
        ]
D
dongdaxiang 已提交
139 140

        # lookup embedding for each slot
141
        q_embs = [
Z
zhang wenhui 已提交
142
            fluid.embedding(
Z
zhangwenhui03 已提交
143
                input=query, size=self.emb_shape, param_attr="emb")
144 145 146
            for query in q_slots
        ]
        pt_embs = [
Z
zhang wenhui 已提交
147
            fluid.embedding(
Z
zhangwenhui03 已提交
148
                input=title, size=self.emb_shape, param_attr="emb")
149 150 151
            for title in pt_slots
        ]
        nt_embs = [
Z
zhang wenhui 已提交
152
            fluid.embedding(
Z
zhangwenhui03 已提交
153
                input=title, size=self.emb_shape, param_attr="emb")
154 155 156
            for title in nt_slots
        ]

D
dongdaxiang 已提交
157
        # encode each embedding field with encoder
158 159 160 161 162 163 164 165 166
        q_encodes = [
            self.query_encoders[i].forward(emb) for i, emb in enumerate(q_embs)
        ]
        pt_encodes = [
            self.title_encoders[i].forward(emb) for i, emb in enumerate(pt_embs)
        ]
        nt_encodes = [
            self.title_encoders[i].forward(emb) for i, emb in enumerate(nt_embs)
        ]
D
dongdaxiang 已提交
167 168

        # concat multi view for query, pos_title, neg_title
Z
zhang wenhui 已提交
169 170 171
        q_concat = fluid.layers.concat(q_encodes)
        pt_concat = fluid.layers.concat(pt_encodes)
        nt_concat = fluid.layers.concat(nt_encodes)
D
dongdaxiang 已提交
172 173

        # projection of hidden layer
Z
zhang wenhui 已提交
174 175 176 177 178 179 180 181 182 183 184 185
        q_hid = fluid.layers.fc(q_concat,
                                size=self.hidden_size,
                                param_attr='q_fc.w',
                                bias_attr='q_fc.b')
        pt_hid = fluid.layers.fc(pt_concat,
                                 size=self.hidden_size,
                                 param_attr='t_fc.w',
                                 bias_attr='t_fc.b')
        nt_hid = fluid.layers.fc(nt_concat,
                                 size=self.hidden_size,
                                 param_attr='t_fc.w',
                                 bias_attr='t_fc.b')
D
dongdaxiang 已提交
186 187

        # cosine of hidden layers
Z
zhang wenhui 已提交
188 189
        cos_pos = fluid.layers.cos_sim(q_hid, pt_hid)
        cos_neg = fluid.layers.cos_sim(q_hid, nt_hid)
190

D
dongdaxiang 已提交
191
        # pairwise hinge_loss
Z
zhang wenhui 已提交
192
        loss_part1 = fluid.layers.elementwise_sub(
Z
zhang wenhui 已提交
193 194
            fluid.layers.fill_constant(
                shape=[fluid.layers.shape(cos_pos)[0], 1],
195 196 197 198
                value=self.margin,
                dtype='float32'),
            cos_pos)

Z
zhang wenhui 已提交
199
        loss_part2 = fluid.layers.elementwise_add(loss_part1, cos_neg)
200

Z
zhang wenhui 已提交
201
        loss_part3 = fluid.layers.elementwise_max(
Z
zhang wenhui 已提交
202 203 204 205
            fluid.layers.fill_constant(
                shape=[fluid.layers.shape(loss_part2)[0], 1],
                value=0.0,
                dtype='float32'),
206 207
            loss_part2)

Z
zhang wenhui 已提交
208
        avg_cost = fluid.layers.mean(loss_part3)
D
dongdaxiang 已提交
209
        correct = self.get_correct(cos_neg, cos_pos)
D
dongdaxiang 已提交
210 211

        return q_slots + pt_slots + nt_slots, avg_cost, correct
212 213 214

    def pred_net(self, query_fields, pos_title_fields, neg_title_fields):
        q_slots = [
Z
zhang wenhui 已提交
215 216
            fluid.data(
                name="q%d" % i, shape=[None, 1], lod_level=1, dtype='int64')
217 218 219
            for i in range(len(self.query_encoders))
        ]
        pt_slots = [
Z
zhang wenhui 已提交
220 221
            fluid.data(
                name="pt%d" % i, shape=[None, 1], lod_level=1, dtype='int64')
222 223
            for i in range(len(self.title_encoders))
        ]
D
dongdaxiang 已提交
224
        # lookup embedding for each slot
225
        q_embs = [
Z
zhang wenhui 已提交
226
            fluid.embedding(
Z
zhangwenhui03 已提交
227
                input=query, size=self.emb_shape, param_attr="emb")
228 229 230
            for query in q_slots
        ]
        pt_embs = [
Z
zhang wenhui 已提交
231
            fluid.embedding(
Z
zhangwenhui03 已提交
232
                input=title, size=self.emb_shape, param_attr="emb")
233 234
            for title in pt_slots
        ]
D
dongdaxiang 已提交
235
        # encode each embedding field with encoder
236 237 238 239 240 241
        q_encodes = [
            self.query_encoder[i].forward(emb) for i, emb in enumerate(q_embs)
        ]
        pt_encodes = [
            self.title_encoders[i].forward(emb) for i, emb in enumerate(pt_embs)
        ]
D
dongdaxiang 已提交
242
        # concat multi view for query, pos_title, neg_title
Z
zhang wenhui 已提交
243 244
        q_concat = fluid.layers.concat(q_encodes)
        pt_concat = fluid.layers.concat(pt_encodes)
D
dongdaxiang 已提交
245
        # projection of hidden layer
Z
zhang wenhui 已提交
246 247 248 249 250 251 252 253 254
        q_hid = fluid.layers.fc(q_concat,
                                size=self.hidden_size,
                                param_attr='q_fc.w',
                                bias_attr='q_fc.b')
        pt_hid = fluid.layers.fc(pt_concat,
                                 size=self.hidden_size,
                                 param_attr='t_fc.w',
                                 bias_attr='t_fc.b')

D
dongdaxiang 已提交
255
        # cosine of hidden layers
Z
zhang wenhui 已提交
256
        cos = fluid.layers.cos_sim(q_hid, pt_hid)
D
dongdaxiang 已提交
257
        return cos