cls.py 4.6 KB
Newer Older
X
xixiaoyao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- 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 paddle.fluid import layers
W
wangxiao1021 已提交
18
from paddlepalm.head.base_head import Head
X
xixiaoyao 已提交
19 20
import numpy as np
import os
W
wangxiao1021 已提交
21
import json
X
xixiaoyao 已提交
22 23


W
wangxiao1021 已提交
24
class Classify(Head):
X
xixiaoyao 已提交
25
    """
X
xixiaoyao 已提交
26
    classification
X
xixiaoyao 已提交
27
    """
X
xixiaoyao 已提交
28 29 30 31
    # def __init__(self, config, phase, backbone_config=None):
    def __init__(self, num_classes, input_dim, dropout_prob=0.0, \
                 param_initializer_range=0.02, phase='train'):

X
xixiaoyao 已提交
32
        self._is_training = phase == 'train'
X
xixiaoyao 已提交
33 34 35
        self._hidden_size = input_dim

        self.num_classes = num_classes
X
xixiaoyao 已提交
36
    
X
xixiaoyao 已提交
37 38 39
        self._dropout_prob = dropout_prob if phase == 'train' else 0.0
        self._param_initializer = fluid.initializer.TruncatedNormal(
            scale=param_initializer_range)
X
xixiaoyao 已提交
40
        self._preds = []
W
wangxiao1021 已提交
41
        self._probs = []
X
xixiaoyao 已提交
42 43 44

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

    @property
    def outputs_attrs(self):
        if self._is_training:
            return {'loss': [[1], 'float32']}
        else:
W
wangxiao1021 已提交
56 57 58
            return {'logits': [[-1, self.num_classes], 'float32'],
                    'probs': [[-1, self.num_classes], 'float32']}
            
X
xixiaoyao 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    def build(self, inputs, scope_name=''):
        sent_emb = inputs['backbone']['sentence_embedding']
        if self._is_training:
            label_ids = inputs['reader']['label_ids']
            cls_feats = fluid.layers.dropout(
                x=sent_emb,
                dropout_prob=self._dropout_prob,
                dropout_implementation="upscale_in_train")

        logits = fluid.layers.fc(
            input=sent_emb,
            size=self.num_classes,
            param_attr=fluid.ParamAttr(
                name=scope_name+"cls_out_w",
                initializer=self._param_initializer),
            bias_attr=fluid.ParamAttr(
                name=scope_name+"cls_out_b", initializer=fluid.initializer.Constant(0.)))
W
wangxiao1021 已提交
77
        probs = fluid.layers.softmax(logits)
X
xixiaoyao 已提交
78 79
        if self._is_training:
            loss = fluid.layers.cross_entropy(
W
wangxiao1021 已提交
80
                input=probs, label=label_ids)
X
xixiaoyao 已提交
81 82 83
            loss = layers.mean(loss)
            return {"loss": loss}
        else:
W
wangxiao1021 已提交
84 85
            return {"logits":logits,
                    "probs":probs}
X
xixiaoyao 已提交
86

X
xixiaoyao 已提交
87
    def batch_postprocess(self, rt_outputs):
X
xixiaoyao 已提交
88 89
        if not self._is_training:
            logits = rt_outputs['logits']
W
wangxiao1021 已提交
90 91 92
            probs = rt_outputs['probs']
            self._preds.extend(logits.tolist())
            self._probs.extend(probs.tolist())
X
xixiaoyao 已提交
93

W
wangxiao1021 已提交
94
    def epoch_postprocess(self, post_inputs, output_dir=None):
X
xixiaoyao 已提交
95 96
        # there is no post_inputs needed and not declared in epoch_inputs_attrs, hence no elements exist in post_inputs
        if not self._is_training:
W
wangxiao1021 已提交
97
            if output_dir is None:
X
xixiaoyao 已提交
98
                for p in self._preds:
W
wangxiao1021 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
                    print(p)
            else:
                with open(os.path.join(output_dir, 'predictions.json'), 'w') as writer:
                    for p in self._preds:
                        writer.write(str(p)+'\n')
                print('Predictions saved at '+os.path.join(output_dir, 'predictions.json'))

    def epoch_postprocess(self, post_inputs, output_dir=None):
        # there is no post_inputs needed and not declared in epoch_inputs_attrs, hence no elements exist in post_inputs
        if not self._is_training:
            if output_dir is None:
                raise ValueError('argument output_dir not found in config. Please add it into config dict/file.')
            with open(os.path.join(output_dir, 'predictions.json'), 'w') as writer:
                for i in range(len(self._preds)):
                    label = 0 if self._preds[i][0] > self._preds[i][1] else 1
                    result = {'index': i, 'label': label, 'logits': self._preds[i], 'probs': self._preds[i]}
                    result = json.dumps(result)
                    writer.write(result+'\n')
            print('Predictions saved at '+os.path.join(output_dir, 'predictions.json'))
X
xixiaoyao 已提交
118 119