model.py 3.2 KB
Newer Older
T
tangwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   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.

T
tangwei 已提交
15
import paddle.fluid as fluid
C
Chengmo 已提交
16 17
from paddlerec.core.utils import envs
from paddlerec.core.model import ModelBase
Y
yinhaofeng 已提交
18
from basemodel import embedding
X
xujiaqi01 已提交
19 20 21 22 23


class Model(ModelBase):
    def __init__(self, config):
        ModelBase.__init__(self, config)
Y
yinhaofeng 已提交
24 25 26 27 28 29 30 31 32 33
        self.dict_size = 2000001
        self.max_len = 100
        self.cnn_dim = 128
        self.cnn_filter_size1 = 1
        self.cnn_filter_size2 = 2
        self.cnn_filter_size3 = 3
        self.emb_dim = 128
        self.hid_dim = 96
        self.class_dim = 2
        self.is_sparse = True
X
xujiaqi01 已提交
34

X
xjqbest 已提交
35
    def input_data(self, is_infer=False, **kwargs):
T
tangwei 已提交
36
        data = fluid.data(
Y
yinhaofeng 已提交
37
            name="input", shape=[None, self.max_len, 1], dtype='int64')
X
xujiaqi01 已提交
38
        seq_len = fluid.data(name="seq_len", shape=[None], dtype='int64')
39 40
        label = fluid.data(name="label", shape=[None, 1], dtype='int64')
        return [data, seq_len, label]
X
fix  
xujiaqi01 已提交
41

X
xjqbest 已提交
42 43
    def net(self, input, is_infer=False):
        """ network definition """
Y
yinhaofeng 已提交
44 45 46
        self.data = input[0]
        self.seq_len = input[1]
        self.label = input[2]
X
fix  
xujiaqi01 已提交
47

X
xujiaqi01 已提交
48
        # embedding layer
Y
yinhaofeng 已提交
49 50 51
        emb = embedding(self.data, self.dict_size, self.emb_dim,
                        self.is_sparse)
        emb = fluid.layers.sequence_unpad(emb, length=self.seq_len)
X
xujiaqi01 已提交
52
        # convolution layer
53 54 55 56 57 58 59 60
        conv1 = fluid.nets.sequence_conv_pool(
            input=emb,
            num_filters=self.cnn_dim,
            filter_size=self.cnn_filter_size1,
            act="tanh",
            pool_type="max")

        conv2 = fluid.nets.sequence_conv_pool(
X
xujiaqi01 已提交
61
            input=emb,
T
tangwei 已提交
62
            num_filters=self.cnn_dim,
63
            filter_size=self.cnn_filter_size2,
X
xujiaqi01 已提交
64 65 66
            act="tanh",
            pool_type="max")

67 68 69 70 71 72 73 74 75
        conv3 = fluid.nets.sequence_conv_pool(
            input=emb,
            num_filters=self.cnn_dim,
            filter_size=self.cnn_filter_size3,
            act="tanh",
            pool_type="max")

        convs_out = fluid.layers.concat(input=[conv1, conv2, conv3], axis=1)

X
xujiaqi01 已提交
76
        # full connect layer
77
        fc_1 = fluid.layers.fc(input=convs_out, size=self.hid_dim, act="tanh")
X
xujiaqi01 已提交
78
        # softmax layer
T
tangwei 已提交
79 80 81
        prediction = fluid.layers.fc(input=[fc_1],
                                     size=self.class_dim,
                                     act="softmax")
Y
yinhaofeng 已提交
82
        cost = fluid.layers.cross_entropy(input=prediction, label=self.label)
X
xujiaqi01 已提交
83
        avg_cost = fluid.layers.mean(x=cost)
Y
yinhaofeng 已提交
84
        acc = fluid.layers.accuracy(input=prediction, label=self.label)
X
xujiaqi01 已提交
85

X
xjqbest 已提交
86 87 88
        self._cost = avg_cost
        if is_infer:
            self._infer_results["acc"] = acc
89
            self._infer_results["loss"] = avg_cost
X
xjqbest 已提交
90 91
        else:
            self._metrics["acc"] = acc
92
            self._metrics["loss"] = avg_cost