model.py 3.4 KB
Newer Older
F
frankwhzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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 math
F
frankwhzhang 已提交
16
import numpy as np
F
frankwhzhang 已提交
17 18 19 20 21 22 23 24 25 26
import paddle.fluid as fluid

from paddlerec.core.utils import envs
from paddlerec.core.model import Model as ModelBase


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

F
frankwhzhang 已提交
27 28 29 30 31 32 33 34 35
    def _init_hyper_parameters(self):
        self.watch_vec_size = envs.get_global_env(
            "hyper_parameters.watch_vec_size")
        self.search_vec_size = envs.get_global_env(
            "hyper_parameters.search_vec_size")
        self.other_feat_size = envs.get_global_env(
            "hyper_parameters.other_feat_size")
        self.output_size = envs.get_global_env("hyper_parameters.output_size")
        self.layers = envs.get_global_env("hyper_parameters.layers")
T
tangwei 已提交
36

F
frankwhzhang 已提交
37
    def input_data(self, is_infer=False, **kwargs):
T
tangwei 已提交
38 39

        watch_vec = fluid.data(
F
frankwhzhang 已提交
40 41 42
            name="watch_vec",
            shape=[None, self.watch_vec_size],
            dtype="float32")
T
tangwei 已提交
43
        search_vec = fluid.data(
F
frankwhzhang 已提交
44 45 46
            name="search_vec",
            shape=[None, self.search_vec_size],
            dtype="float32")
T
tangwei 已提交
47
        other_feat = fluid.data(
F
frankwhzhang 已提交
48 49 50
            name="other_feat",
            shape=[None, self.other_feat_size],
            dtype="float32")
F
frankwhzhang 已提交
51 52 53 54 55
        label = fluid.data(name="label", shape=[None, 1], dtype="int64")
        inputs = [watch_vec] + [search_vec] + [other_feat] + [label]

        return inputs

F
frankwhzhang 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    def net(self, inputs, is_infer=False):
        concat_feats = fluid.layers.concat(input=inputs[:-1], axis=-1)

        l1 = self._fc('l1', concat_feats, self.layers[0], 'relu')
        l2 = self._fc('l2', l1, self.layers[1], 'relu')
        l3 = self._fc('l3', l2, self.layers[2], 'relu')
        l4 = self._fc('l4', l3, self.output_size, 'softmax')

        num_seqs = fluid.layers.create_tensor(dtype='int64')
        acc = fluid.layers.accuracy(input=l4, label=inputs[-1], total=num_seqs)

        cost = fluid.layers.cross_entropy(input=l4, label=inputs[-1])
        avg_cost = fluid.layers.mean(cost)

        self._cost = avg_cost
        self._metrics["acc"] = acc

    def _fc(self, tag, data, out_dim, active='relu'):
F
frankwhzhang 已提交
74
        init_stddev = 1.0
T
tangwei 已提交
75 76
        scales = 1.0 / np.sqrt(data.shape[1])

F
frankwhzhang 已提交
77
        if tag == 'l4':
T
tangwei 已提交
78 79 80 81
            p_attr = fluid.param_attr.ParamAttr(
                name='%s_weight' % tag,
                initializer=fluid.initializer.NormalInitializer(
                    loc=0.0, scale=init_stddev * scales))
F
frankwhzhang 已提交
82 83
        else:
            p_attr = None
T
tangwei 已提交
84 85 86

        b_attr = fluid.ParamAttr(
            name='%s_bias' % tag, initializer=fluid.initializer.Constant(0.1))
F
frankwhzhang 已提交
87 88

        out = fluid.layers.fc(input=data,
T
tangwei 已提交
89 90 91 92 93
                              size=out_dim,
                              act=active,
                              param_attr=p_attr,
                              bias_attr=b_attr,
                              name=tag)
F
frankwhzhang 已提交
94
        return out