model.py 4.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.

Y
yaoxuefeng 已提交
15 16
import math

T
tangwei 已提交
17 18
import paddle.fluid as fluid

19 20
from paddlerec.core.utils import envs
from paddlerec.core.model import Model as ModelBase
Y
yaoxuefeng 已提交
21 22 23 24 25


class Model(ModelBase):
    def __init__(self, config):
        ModelBase.__init__(self, config)
T
for mat  
tangwei 已提交
26

Y
yaoxuefeng 已提交
27
    def wide_part(self, data):
T
tangwei 已提交
28 29 30 31 32 33 34 35 36 37
        out = fluid.layers.fc(
            input=data,
            size=1,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=1.0 / math.sqrt(data.shape[1])),
                regularizer=fluid.regularizer.L2DecayRegularizer(
                    regularization_coeff=1e-4)),
            act=None,
            name='wide')
Y
yaoxuefeng 已提交
38
        return out
T
for mat  
tangwei 已提交
39

Y
yaoxuefeng 已提交
40
    def fc(self, data, hidden_units, active, tag):
T
tangwei 已提交
41 42 43 44 45 46 47 48
        output = fluid.layers.fc(
            input=data,
            size=hidden_units,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=1.0 / math.sqrt(data.shape[1]))),
            act=active,
            name=tag)
T
for mat  
tangwei 已提交
49

Y
yaoxuefeng 已提交
50
        return output
T
for mat  
tangwei 已提交
51

Y
yaoxuefeng 已提交
52 53 54 55
    def deep_part(self, data, hidden1_units, hidden2_units, hidden3_units):
        l1 = self.fc(data, hidden1_units, 'relu', 'l1')
        l2 = self.fc(l1, hidden2_units, 'relu', 'l2')
        l3 = self.fc(l2, hidden3_units, 'relu', 'l3')
T
for mat  
tangwei 已提交
56

Y
yaoxuefeng 已提交
57
        return l3
T
for mat  
tangwei 已提交
58

Y
yaoxuefeng 已提交
59
    def train_net(self):
X
xjqbest 已提交
60
        self._init_slots()
X
xujiaqi01 已提交
61 62 63
        wide_input = self._dense_data_var[0]
        deep_input = self._dense_data_var[1]
        label = self._sparse_data_var[0]
Y
yaoxuefeng 已提交
64

T
tangwei 已提交
65 66 67 68 69 70
        hidden1_units = envs.get_global_env("hyper_parameters.hidden1_units",
                                            75, self._namespace)
        hidden2_units = envs.get_global_env("hyper_parameters.hidden2_units",
                                            50, self._namespace)
        hidden3_units = envs.get_global_env("hyper_parameters.hidden3_units",
                                            25, self._namespace)
Y
yaoxuefeng 已提交
71
        wide_output = self.wide_part(wide_input)
T
tangwei 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        deep_output = self.deep_part(deep_input, hidden1_units, hidden2_units,
                                     hidden3_units)

        wide_model = fluid.layers.fc(
            input=wide_output,
            size=1,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=1.0)),
            act=None,
            name='w_wide')

        deep_model = fluid.layers.fc(
            input=deep_output,
            size=1,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.TruncatedNormal(
                    loc=0.0, scale=1.0)),
            act=None,
            name='w_deep')
T
for mat  
tangwei 已提交
92

Y
yaoxuefeng 已提交
93
        prediction = fluid.layers.elementwise_add(wide_model, deep_model)
T
tangwei 已提交
94 95 96 97
        pred = fluid.layers.sigmoid(
            fluid.layers.clip(
                prediction, min=-15.0, max=15.0),
            name="prediction")
Y
yaoxuefeng 已提交
98 99

        num_seqs = fluid.layers.create_tensor(dtype='int64')
T
tangwei 已提交
100 101 102 103 104 105 106 107
        acc = fluid.layers.accuracy(
            input=pred,
            label=fluid.layers.cast(
                x=label, dtype='int64'),
            total=num_seqs)
        auc_var, batch_auc, auc_states = fluid.layers.auc(
            input=pred, label=fluid.layers.cast(
                x=label, dtype='int64'))
T
for mat  
tangwei 已提交
108

Y
yaoxuefeng 已提交
109 110 111 112
        self._metrics["AUC"] = auc_var
        self._metrics["BATCH_AUC"] = batch_auc
        self._metrics["ACC"] = acc

T
tangwei 已提交
113 114 115
        cost = fluid.layers.sigmoid_cross_entropy_with_logits(
            x=prediction, label=fluid.layers.cast(
                label, dtype='float32'))
Y
yaoxuefeng 已提交
116 117 118 119
        avg_cost = fluid.layers.mean(cost)
        self._cost = avg_cost

    def optimizer(self):
T
tangwei 已提交
120 121
        learning_rate = envs.get_global_env("hyper_parameters.learning_rate",
                                            None, self._namespace)
Y
yaoxuefeng 已提交
122 123 124
        optimizer = fluid.optimizer.Adam(learning_rate, lazy_mode=True)
        return optimizer

X
xjqbest 已提交
125 126
    def infer_net(self):
        self.train_net()