model.py 2.5 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
X
xujiaqi01 已提交
16

17
from paddlerec.core.model import Model as ModelBase
X
xujiaqi01 已提交
18 19 20 21 22


class Model(ModelBase):
    def __init__(self, config):
        ModelBase.__init__(self, config)
T
tangwei 已提交
23 24 25 26 27 28 29
        self.dict_dim = 100
        self.max_len = 10
        self.cnn_dim = 32
        self.cnn_filter_size = 128
        self.emb_dim = 8
        self.hid_dim = 128
        self.class_dim = 2
X
xujiaqi01 已提交
30 31 32

    def train_net(self):
        """ network definition """
T
for mat  
tangwei 已提交
33

T
tangwei 已提交
34 35
        data = fluid.data(
            name="input", shape=[None, self.max_len], dtype='int64')
X
xujiaqi01 已提交
36 37
        label = fluid.data(name="label", shape=[None, 1], dtype='int64')
        seq_len = fluid.data(name="seq_len", shape=[None], dtype='int64')
X
fix  
xujiaqi01 已提交
38 39 40

        self._data_var = [data, label, seq_len]

X
xujiaqi01 已提交
41
        # embedding layer
T
tangwei 已提交
42
        emb = fluid.embedding(input=data, size=[self.dict_dim, self.emb_dim])
X
fix  
xujiaqi01 已提交
43
        emb = fluid.layers.sequence_unpad(emb, length=seq_len)
X
xujiaqi01 已提交
44 45 46
        # convolution layer
        conv = fluid.nets.sequence_conv_pool(
            input=emb,
T
tangwei 已提交
47 48
            num_filters=self.cnn_dim,
            filter_size=self.cnn_filter_size,
X
xujiaqi01 已提交
49 50 51 52
            act="tanh",
            pool_type="max")

        # full connect layer
X
fix  
xujiaqi01 已提交
53
        fc_1 = fluid.layers.fc(input=[conv], size=self.hid_dim)
X
xujiaqi01 已提交
54
        # softmax layer
T
tangwei 已提交
55 56 57
        prediction = fluid.layers.fc(input=[fc_1],
                                     size=self.class_dim,
                                     act="softmax")
X
xujiaqi01 已提交
58 59
        cost = fluid.layers.cross_entropy(input=prediction, label=label)
        avg_cost = fluid.layers.mean(x=cost)
T
for mat  
tangwei 已提交
60
        acc = fluid.layers.accuracy(input=prediction, label=label)
X
xujiaqi01 已提交
61 62

        self.cost = avg_cost
X
fix  
xujiaqi01 已提交
63
        self._metrics["acc"] = acc
X
xujiaqi01 已提交
64

T
tangwei 已提交
65
    def get_avg_cost(self):
X
xujiaqi01 已提交
66 67 68
        return self.cost

    def get_metrics(self):
X
fix  
xujiaqi01 已提交
69
        return self._metrics
X
xujiaqi01 已提交
70 71

    def optimizer(self):
T
tangwei 已提交
72
        learning_rate = 0.01
X
xujiaqi01 已提交
73 74 75
        sgd_optimizer = fluid.optimizer.Adagrad(learning_rate=learning_rate)
        return sgd_optimizer

X
fix  
xujiaqi01 已提交
76
    def infer_net(self):
X
xujiaqi01 已提交
77
        self.train_net()