define_paradigm.py 5.7 KB
Newer Older
0
0YuanZhang0 已提交
1
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. 
Y
Yibing Liu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# 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.
"""define network paradigm"""

import sys
import re

import paddle
import paddle.fluid as fluid


23
class Paradigm(object):
Y
Yibing Liu 已提交
24 25 26
    """
    define network paradigm
    """
27 28

    def __init__(self, task_name):
Y
Yibing Liu 已提交
29 30 31 32 33
        """
        init
        """
        self.task_name = task_name

34
    def create_cls(self, transformer_inst, params):
Y
Yibing Liu 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        """
        create classify paradigm network
        """
        cls_feats = transformer_inst.get_pooled_output()
        cls_feats = fluid.layers.dropout(
            x=cls_feats,
            dropout_prob=0.1,
            dropout_implementation="upscale_in_train")
        logits = fluid.layers.fc(
            input=cls_feats,
            size=params['num_labels'],
            param_attr=fluid.ParamAttr(
                name="cls_out_w",
                initializer=fluid.initializer.TruncatedNormal(scale=0.02)),
            bias_attr=fluid.ParamAttr(
                name="cls_out_b", initializer=fluid.initializer.Constant(0.)))
51 52
        
        if not params['is_training']: 
Y
Yibing Liu 已提交
53
            probs = fluid.layers.softmax(logits)
0
0YuanZhang0 已提交
54
            results = {"probs": probs}
Y
Yibing Liu 已提交
55 56 57 58
            return results

        ce_loss, probs = fluid.layers.softmax_with_cross_entropy(
            logits=logits, label=params['labels'], return_softmax=True)
P
pkpk 已提交
59
        loss = fluid.layers.mean(x=ce_loss)
Y
Yibing Liu 已提交
60
        num_seqs = fluid.layers.create_tensor(dtype='int64')
61 62
        accuracy = fluid.layers.accuracy(
            input=probs, label=params['labels'], total=num_seqs)
Y
Yibing Liu 已提交
63

64 65 66 67 68 69
        results = {
            "loss": loss,
            "probs": probs,
            "accuracy": accuracy,
            "num_seqs": num_seqs
        }
Y
Yibing Liu 已提交
70 71
        return results

72
    def create_multi_cls(self, transformer_inst, params):
Y
Yibing Liu 已提交
73 74 75 76 77
        """
        create multi classify paradigm network
        """
        cls_feats = transformer_inst.get_pooled_output()
        cls_feats = fluid.layers.dropout(
78 79 80
            x=cls_feats,
            dropout_prob=0.1,
            dropout_implementation="upscale_in_train")
Y
Yibing Liu 已提交
81 82 83 84 85 86 87 88 89 90
        logits = fluid.layers.fc(
            input=cls_feats,
            size=params['num_labels'],
            param_attr=fluid.ParamAttr(
                name="cls_out_w",
                initializer=fluid.initializer.TruncatedNormal(scale=0.02)),
            bias_attr=fluid.ParamAttr(
                name="cls_out_b", initializer=fluid.initializer.Constant(0.)))

        labels_onehot = fluid.layers.cast(params["labels"], dtype='float32')
91 92 93
        ce_loss = fluid.layers.reduce_sum(
            fluid.layers.sigmoid_cross_entropy_with_logits(
                x=logits, label=labels_onehot))
P
pkpk 已提交
94
        loss = fluid.layers.mean(x=ce_loss)
Y
Yibing Liu 已提交
95 96
        probs = fluid.layers.sigmoid(logits)

0
0YuanZhang0 已提交
97 98
        if not params['is_training']:
            results = {"probs": probs}
Y
Yibing Liu 已提交
99 100
            return results

101 102
        num_seqs = fluid.layers.tensor.fill_constant(
            shape=[1], dtype='int64', value=1)
Y
Yibing Liu 已提交
103

104
        results = {"loss": loss, "probs": probs, "num_seqs": num_seqs}
Y
Yibing Liu 已提交
105 106
        return results

107
    def create_sequence_tagging(self, transformer_inst, params):
Y
Yibing Liu 已提交
108 109 110 111 112 113 114 115 116
        """
        create sequence tagging paradigm
        """
        output_layer = transformer_inst.get_sequence_output()
        hidden_size = output_layer.shape[-1]
        output_layer = fluid.layers.stack(output_layer, axis=1)
        output_layer = fluid.layers.reshape(output_layer, [-1, hidden_size])

        logits = fluid.layers.fc(input=output_layer, size=params['num_labels'])
117 118 119
        probs = fluid.layers.cast(
            fluid.layers.argmax(
                logits, axis=1), dtype='int32')
Y
Yibing Liu 已提交
120

0
0YuanZhang0 已提交
121 122
        if not params['is_training']:
            results = {"probs": probs}
Y
Yibing Liu 已提交
123
            return results
124 125 126 127 128

        num_seqs = fluid.layers.tensor.fill_constant(
            shape=[1], dtype='int64', value=1)
        y_label_reshape = fluid.layers.cast(
            fluid.layers.reshape(params['labels'], [-1]), dtype='int32')
Y
Yibing Liu 已提交
129
        correct_prediction = fluid.layers.equal(probs, y_label_reshape)
130 131 132
        accuracy = fluid.layers.mean(
            fluid.layers.cast(
                correct_prediction, dtype='float32'))
Y
Yibing Liu 已提交
133 134
        ce_loss = fluid.layers.softmax_with_cross_entropy(logits=logits, \
                label=fluid.layers.reshape(params['labels'], [-1, 1]))
P
pkpk 已提交
135
        loss = fluid.layers.mean(x=ce_loss)
136 137 138 139 140 141 142

        results = {
            "loss": loss,
            "probs": probs,
            "accuracy": accuracy,
            "num_seqs": num_seqs
        }
Y
Yibing Liu 已提交
143 144
        return results

145
    def paradigm(self, transformer_inst, params):
Y
Yibing Liu 已提交
146 147 148 149
        """
        run paradigm
        """
        results = None
150
        if self.task_name == 'udc':
Y
Yibing Liu 已提交
151 152 153
            results = self.create_cls(transformer_inst, params)
        elif self.task_name == 'swda':
            results = self.create_cls(transformer_inst, params)
154
        elif self.task_name == 'mrda':
Y
Yibing Liu 已提交
155
            results = self.create_cls(transformer_inst, params)
156
        elif self.task_name == 'atis_intent':
Y
Yibing Liu 已提交
157
            results = self.create_cls(transformer_inst, params)
158
        elif self.task_name == 'atis_slot':
Y
Yibing Liu 已提交
159 160 161 162
            results = self.create_sequence_tagging(transformer_inst, params)
        elif self.task_name == 'dstc2':
            results = self.create_multi_cls(transformer_inst, params)
        return results