ade_net.py 3.8 KB
Newer Older
0
0YuanZhang0 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# Copyright (c) 2019 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.
"""Network for auto dialogue evaluation"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle
import paddle.fluid as fluid


P
pkpk 已提交
24 25 26 27 28 29 30
def create_net(is_training,
               model_input,
               args,
               clip_value=10.0,
               word_emb_name="shared_word_emb",
               lstm_W_name="shared_lstm_W",
               lstm_bias_name="shared_lstm_bias"):
0
0YuanZhang0 已提交
31 32 33 34 35 36

    context_wordseq = model_input.context_wordseq
    response_wordseq = model_input.response_wordseq
    label = model_input.labels

    #emb
0
0YuanZhang0 已提交
37
    context_emb = fluid.input.embedding(
0
0YuanZhang0 已提交
38 39 40 41 42 43 44
        input=context_wordseq,
        size=[args.vocab_size, args.emb_size],
        is_sparse=True,
        param_attr=fluid.ParamAttr(
            name=word_emb_name,
            initializer=fluid.initializer.Normal(scale=0.1)))

0
0YuanZhang0 已提交
45
    response_emb = fluid.input.embedding(
0
0YuanZhang0 已提交
46 47 48 49 50 51 52 53
        input=response_wordseq,
        size=[args.vocab_size, args.emb_size],
        is_sparse=True,
        param_attr=fluid.ParamAttr(
            name=word_emb_name,
            initializer=fluid.initializer.Normal(scale=0.1)))

    #fc to fit dynamic LSTM
P
pkpk 已提交
54 55 56 57
    context_fc = fluid.layers.fc(input=context_emb,
                                 size=args.hidden_size * 4,
                                 param_attr=fluid.ParamAttr(name='fc_weight'),
                                 bias_attr=fluid.ParamAttr(name='fc_bias'))
0
0YuanZhang0 已提交
58

P
pkpk 已提交
59 60 61 62
    response_fc = fluid.layers.fc(input=response_emb,
                                  size=args.hidden_size * 4,
                                  param_attr=fluid.ParamAttr(name='fc_weight'),
                                  bias_attr=fluid.ParamAttr(name='fc_bias'))
0
0YuanZhang0 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    #LSTM
    context_rep, _ = fluid.layers.dynamic_lstm(
        input=context_fc,
        size=args.hidden_size * 4,
        param_attr=fluid.ParamAttr(name=lstm_W_name),
        bias_attr=fluid.ParamAttr(name=lstm_bias_name))
    context_rep = fluid.layers.sequence_last_step(context_rep)

    response_rep, _ = fluid.layers.dynamic_lstm(
        input=response_fc,
        size=args.hidden_size * 4,
        param_attr=fluid.ParamAttr(name=lstm_W_name),
        bias_attr=fluid.ParamAttr(name=lstm_bias_name))
    response_rep = fluid.layers.sequence_last_step(input=response_rep)

    logits = fluid.layers.bilinear_tensor_product(
        context_rep, response_rep, size=1)

P
pkpk 已提交
82
    if args.loss_type == 'CLS':
0
0YuanZhang0 已提交
83 84 85 86 87 88 89 90 91 92 93 94
        label = fluid.layers.cast(x=label, dtype='float32')
        loss = fluid.layers.sigmoid_cross_entropy_with_logits(logits, label)
        loss = fluid.layers.reduce_mean(
            fluid.layers.clip(
                loss, min=-clip_value, max=clip_value))
    elif args.loss_type == 'L2':
        norm_score = 2 * fluid.layers.sigmoid(logits)
        label = fluid.layers.cast(x=label, dtype='float32')
        loss = fluid.layers.square_error_cost(norm_score, label) / 4
        loss = fluid.layers.reduce_mean(loss)
    else:
        raise ValueError
P
pkpk 已提交
95 96

    if is_training:
0
0YuanZhang0 已提交
97
        return loss
P
pkpk 已提交
98
    else:
0
0YuanZhang0 已提交
99 100 101 102 103 104 105
        return logits


def set_word_embedding(word_emb, place, word_emb_name="shared_word_emb"):
    """
    Set word embedding
    """
P
pkpk 已提交
106
    word_emb_param = fluid.global_scope().find_var(word_emb_name).get_tensor()
0
0YuanZhang0 已提交
107
    word_emb_param.set(word_emb, place)