match.py 2.9 KB
Newer Older
X
xixiaoyao 已提交
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
# -*- coding: UTF-8 -*-
#   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.

import paddle.fluid as fluid
from paddlepalm.interface import task_paradigm
from paddle.fluid import layers

class TaskParadigm(task_paradigm):
    '''
    matching
    '''
    def __init__(self, config, phase, backbone_config=None):
        self._is_training = phase == 'train'
        self._hidden_size = backbone_config['hidden_size']
X
xixiaoyao 已提交
27 28 29 30 31 32 33 34 35 36 37

        if 'initializer_range' in config:
            self._param_initializer = config['initializer_range']
        else:
            self._param_initializer = fluid.initializer.TruncatedNormal(
                scale=backbone_config.get('initializer_range', 0.02))
        if 'dropout_prob' in config:
            self._dropout_prob = config['dropout_prob']
        else:
            self._dropout_prob = backbone_config.get('hidden_dropout_prob', 0.0)

X
xixiaoyao 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    
    @property
    def inputs_attrs(self):
        if self._is_training:
            reader = {"label_ids": [[-1, 1], 'int64']}
        else:
            reader = {}
        bb = {"sentence_pair_embedding": [[-1, self._hidden_size], 'float32']}
        return {'reader': reader, 'backbone': bb}

    @property
    def outputs_attrs(self):
        if self._is_training:
            return {"loss": [[1], 'float32']}
        else:
            return {"logits": [[-1, 1], 'float32']}

    def build(self, inputs):
X
xixiaoyao 已提交
56 57
        if self._is_training:
            labels = inputs["reader"]["label_ids"] 
X
xixiaoyao 已提交
58 59
        cls_feats = inputs["backbone"]["sentence_pair_embedding"]

X
xixiaoyao 已提交
60 61 62 63 64 65
        if self._is_training:
            cls_feats = fluid.layers.dropout(
                x=cls_feats,
                dropout_prob=self._dropout_prob,
                dropout_implementation="upscale_in_train")

X
xixiaoyao 已提交
66 67 68 69 70
        logits = fluid.layers.fc(
            input=cls_feats,
            size=2,
            param_attr=fluid.ParamAttr(
                name="cls_out_w",
X
xixiaoyao 已提交
71
                initializer=self._param_initializer),
X
xixiaoyao 已提交
72 73 74 75 76
            bias_attr=fluid.ParamAttr(
                name="cls_out_b",
                initializer=fluid.initializer.Constant(0.)))

        if self._is_training:
X
xixiaoyao 已提交
77 78 79
            ce_loss, probs = fluid.layers.softmax_with_cross_entropy(
                logits=logits, label=labels, return_softmax=True)
            loss = fluid.layers.mean(x=ce_loss)
X
xixiaoyao 已提交
80 81 82 83
            return {'loss': loss}
        else:
            return {'logits': logits}