model.py 3.9 KB
Newer Older
Z
zhangwenhui03 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 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

17
from paddlerec.core.utils import envs
C
Chengmo 已提交
18
from paddlerec.core.model import ModelBase
M
update  
malin10 已提交
19
from paddlerec.core.metrics import RecallK
Z
zhangwenhui03 已提交
20 21 22 23 24 25


class Model(ModelBase):
    def __init__(self, config):
        ModelBase.__init__(self, config)

F
frankwhzhang 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39
    def _init_hyper_parameters(self):
        self.recall_k = envs.get_global_env("hyper_parameters.recall_k")
        self.vocab_size = envs.get_global_env("hyper_parameters.vocab_size")
        self.hid_size = envs.get_global_env("hyper_parameters.hid_size")
        self.init_low_bound = envs.get_global_env(
            "hyper_parameters.init_low_bound")
        self.init_high_bound = envs.get_global_env(
            "hyper_parameters.init_high_bound")
        self.emb_lr_x = envs.get_global_env("hyper_parameters.emb_lr_x")
        self.gru_lr_x = envs.get_global_env("hyper_parameters.gru_lr_x")
        self.fc_lr_x = envs.get_global_env("hyper_parameters.fc_lr_x")

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

Z
zhangwenhui03 已提交
40 41 42 43 44 45
        # Input data
        src_wordseq = fluid.data(
            name="src_wordseq", shape=[None, 1], dtype="int64", lod_level=1)
        dst_wordseq = fluid.data(
            name="dst_wordseq", shape=[None, 1], dtype="int64", lod_level=1)

F
frankwhzhang 已提交
46 47 48 49 50
        return [src_wordseq, dst_wordseq]

    def net(self, inputs, is_infer=False):
        src_wordseq = inputs[0]
        dst_wordseq = inputs[1]
Z
zhangwenhui03 已提交
51

Z
zhangwenhui03 已提交
52 53
        emb = fluid.embedding(
            input=src_wordseq,
F
frankwhzhang 已提交
54
            size=[self.vocab_size, self.hid_size],
Z
zhangwenhui03 已提交
55
            param_attr=fluid.ParamAttr(
Z
zhangwenhui03 已提交
56
                name="emb",
Z
zhangwenhui03 已提交
57
                initializer=fluid.initializer.Uniform(
F
frankwhzhang 已提交
58 59
                    low=self.init_low_bound, high=self.init_high_bound),
                learning_rate=self.emb_lr_x),
Z
zhangwenhui03 已提交
60 61
            is_sparse=True)
        fc0 = fluid.layers.fc(input=emb,
F
frankwhzhang 已提交
62
                              size=self.hid_size * 3,
Z
zhangwenhui03 已提交
63 64
                              param_attr=fluid.ParamAttr(
                                  initializer=fluid.initializer.Uniform(
F
frankwhzhang 已提交
65 66 67
                                      low=self.init_low_bound,
                                      high=self.init_high_bound),
                                  learning_rate=self.gru_lr_x))
Z
zhangwenhui03 已提交
68 69
        gru_h0 = fluid.layers.dynamic_gru(
            input=fc0,
F
frankwhzhang 已提交
70
            size=self.hid_size,
Z
zhangwenhui03 已提交
71 72
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.Uniform(
F
frankwhzhang 已提交
73 74
                    low=self.init_low_bound, high=self.init_high_bound),
                learning_rate=self.gru_lr_x))
Z
zhangwenhui03 已提交
75 76

        fc = fluid.layers.fc(input=gru_h0,
F
frankwhzhang 已提交
77
                             size=self.vocab_size,
Z
zhangwenhui03 已提交
78 79 80
                             act='softmax',
                             param_attr=fluid.ParamAttr(
                                 initializer=fluid.initializer.Uniform(
F
frankwhzhang 已提交
81 82 83
                                     low=self.init_low_bound,
                                     high=self.init_high_bound),
                                 learning_rate=self.fc_lr_x))
Z
zhangwenhui03 已提交
84
        cost = fluid.layers.cross_entropy(input=fc, label=dst_wordseq)
M
update  
malin10 已提交
85
        acc = RecallK(input=fc, label=dst_wordseq, k=self.recall_k)
M
malin10 已提交
86

Z
zhangwenhui03 已提交
87
        if is_infer:
M
update  
malin10 已提交
88
            self._infer_results['Recall@20'] = acc
Z
zhangwenhui03 已提交
89
            return
Z
zhangwenhui03 已提交
90 91 92 93
        avg_cost = fluid.layers.mean(x=cost)

        self._cost = avg_cost
        self._metrics["cost"] = avg_cost
M
update  
malin10 已提交
94
        self._metrics["Recall@20"] = acc