basemodel.py 4.2 KB
Newer Older
Y
yinhaofeng 已提交
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#   Copyright (c) 2020 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 paddlerec.core.utils import envs
from paddlerec.core.model import ModelBase
from paddlerec.core.metrics import RecallK


class Model(ModelBase):
    def __init__(self, config):
        ModelBase.__init__(self, config)
        self.dict_size = 2000000 + 1
        self.max_seq_len = 1024
        self.emb_dim = 128
        self.cnn_hid_dim = 128
        self.cnn_win_size = 3
        self.cnn_win_size2 = 5
        self.hid_dim1 = 96
        self.class_dim = 30
        self.is_sparse = True

    def input_data(self, is_infer=False, **kwargs):

        text = fluid.data(
            name="text", shape=[None, self.max_seq_len, 1], dtype='int64')
        label = fluid.data(name="category", shape=[None, 1], dtype='int64')
        seq_len = fluid.data(name="seq_len", shape=[None], dtype='int64')
        return [text, label, seq_len]

    def net(self, inputs, is_infer=False):
        """ network definition """
        #text label
        self.data = inputs[0]
        self.label = inputs[1]
        self.seq_len = inputs[2]
        emb = embedding(self.data, self.dict_size, self.emb_dim,
                        self.is_sparse)
        concat = multi_convs(emb, self.seq_len, self.cnn_hid_dim,
                             self.cnn_win_size, self.cnn_win_size2)
        self.fc_1 = full_connect(concat, self.hid_dim1)
        self.metrics(is_infer)

    def metrics(self, is_infer=False):
        """ classification and metrics """
        # softmax layer
        prediction = fluid.layers.fc(input=[self.fc_1],
                                     size=self.class_dim,
                                     act="softmax",
                                     name="pretrain_fc_1")
        cost = fluid.layers.cross_entropy(input=prediction, label=self.label)
        avg_cost = fluid.layers.mean(x=cost)
        acc = fluid.layers.accuracy(input=prediction, label=self.label)
        #acc = RecallK(input=prediction, label=label, k=1)

        self._cost = avg_cost
        if is_infer:
            self._infer_results["acc"] = acc
        else:
            self._metrics["acc"] = acc


def embedding(inputs, dict_size, emb_dim, is_sparse):
    """ embeding definition """
    emb = fluid.layers.embedding(
        input=inputs,
        size=[dict_size, emb_dim],
        is_sparse=is_sparse,
        param_attr=fluid.ParamAttr(
            name='pretrain_word_embedding',
            initializer=fluid.initializer.Xavier()))
    return emb


def multi_convs(input_layer, seq_len, cnn_hid_dim, cnn_win_size,
                cnn_win_size2):
    """conv and concat"""
    emb = fluid.layers.sequence_unpad(
        input_layer, length=seq_len, name="pretrain_unpad")
    conv = fluid.nets.sequence_conv_pool(
        param_attr=fluid.ParamAttr(name="pretrain_conv0_w"),
        bias_attr=fluid.ParamAttr(name="pretrain_conv0_b"),
        input=emb,
        num_filters=cnn_hid_dim,
        filter_size=cnn_win_size,
        act="tanh",
        pool_type="max")
    conv2 = fluid.nets.sequence_conv_pool(
        param_attr=fluid.ParamAttr(name="pretrain_conv1_w"),
        bias_attr=fluid.ParamAttr(name="pretrain_conv1_b"),
        input=emb,
        num_filters=cnn_hid_dim,
        filter_size=cnn_win_size2,
        act="tanh",
        pool_type="max")
    concat = fluid.layers.concat(
        input=[conv, conv2], axis=1, name="pretrain_concat")
    return concat


def full_connect(input_layer, hid_dim1):
    """full connect layer"""
    fc_1 = fluid.layers.fc(name="pretrain_fc_0",
                           input=input_layer,
                           size=hid_dim1,
                           act="tanh")
    return fc_1