model.py 4.6 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 18
from paddlerec.core.utils import envs
from paddlerec.core.model import Model as ModelBase
Z
zhangwenhui03 已提交
19 20 21 22 23 24


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

Z
zhangwenhui03 已提交
25
    def model(self, is_infer=False):
Z
zhangwenhui03 已提交
26

T
tangwei 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
        feature_size = envs.get_global_env("hyper_parameters.feature_size",
                                           None, self._namespace)
        bottom_size = envs.get_global_env("hyper_parameters.bottom_size", None,
                                          self._namespace)
        tower_size = envs.get_global_env("hyper_parameters.tower_size", None,
                                         self._namespace)
        tower_nums = envs.get_global_env("hyper_parameters.tower_nums", None,
                                         self._namespace)

        input_data = fluid.data(
            name="input", shape=[-1, feature_size], dtype="float32")
        label_income = fluid.data(
            name="label_income", shape=[-1, 2], dtype="float32", lod_level=0)
        label_marital = fluid.data(
            name="label_marital", shape=[-1, 2], dtype="float32", lod_level=0)
T
for mat  
tangwei 已提交
42

Z
zhangwenhui03 已提交
43 44 45
        if is_infer:
            self._infer_data_var = [input_data, label_income, label_marital]
            self._infer_data_loader = fluid.io.DataLoader.from_generator(
T
tangwei 已提交
46 47 48 49
                feed_list=self._infer_data_var,
                capacity=64,
                use_double_buffer=False,
                iterable=False)
Z
zhangwenhui03 已提交
50

Z
zhangwenhui03 已提交
51 52
        self._data_var.extend([input_data, label_income, label_marital])

T
tangwei 已提交
53 54 55 56 57 58
        bottom_output = fluid.layers.fc(
            input=input_data,
            size=bottom_size,
            act='relu',
            bias_attr=fluid.ParamAttr(learning_rate=1.0),
            name='bottom_output')
T
for mat  
tangwei 已提交
59

Z
zhangwenhui03 已提交
60 61
        # Build tower layer from bottom layer
        output_layers = []
T
for mat  
tangwei 已提交
62
        for index in range(tower_nums):
Z
zhangwenhui03 已提交
63
            tower_layer = fluid.layers.fc(input=bottom_output,
T
for mat  
tangwei 已提交
64 65 66
                                          size=tower_size,
                                          act='relu',
                                          name='task_layer_' + str(index))
Z
zhangwenhui03 已提交
67
            output_layer = fluid.layers.fc(input=tower_layer,
T
for mat  
tangwei 已提交
68 69 70
                                           size=2,
                                           act='softmax',
                                           name='output_layer_' + str(index))
Z
zhangwenhui03 已提交
71 72
            output_layers.append(output_layer)

T
tangwei 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        pred_income = fluid.layers.clip(
            output_layers[0], min=1e-15, max=1.0 - 1e-15)
        pred_marital = fluid.layers.clip(
            output_layers[1], min=1e-15, max=1.0 - 1e-15)

        label_income_1 = fluid.layers.slice(
            label_income, axes=[1], starts=[1], ends=[2])
        label_marital_1 = fluid.layers.slice(
            label_marital, axes=[1], starts=[1], ends=[2])

        auc_income, batch_auc_1, auc_states_1 = fluid.layers.auc(
            input=pred_income,
            label=fluid.layers.cast(
                x=label_income_1, dtype='int64'))
        auc_marital, batch_auc_2, auc_states_2 = fluid.layers.auc(
            input=pred_marital,
            label=fluid.layers.cast(
                x=label_marital_1, dtype='int64'))
Z
zhangwenhui03 已提交
91

Z
zhangwenhui03 已提交
92 93 94 95 96
        if is_infer:
            self._infer_results["AUC_income"] = auc_income
            self._infer_results["AUC_marital"] = auc_marital
            return

T
tangwei 已提交
97 98 99 100
        cost_income = fluid.layers.cross_entropy(
            input=pred_income, label=label_income, soft_label=True)
        cost_marital = fluid.layers.cross_entropy(
            input=pred_marital, label=label_marital, soft_label=True)
Z
zhangwenhui03 已提交
101
        cost = fluid.layers.elementwise_add(cost_income, cost_marital, axis=1)
T
for mat  
tangwei 已提交
102 103 104

        avg_cost = fluid.layers.mean(x=cost)

Z
zhangwenhui03 已提交
105 106 107 108 109 110 111
        self._cost = avg_cost
        self._metrics["AUC_income"] = auc_income
        self._metrics["BATCH_AUC_income"] = batch_auc_1
        self._metrics["AUC_marital"] = auc_marital
        self._metrics["BATCH_AUC_marital"] = batch_auc_2

    def train_net(self):
Z
zhangwenhui03 已提交
112
        self.model()
Z
zhangwenhui03 已提交
113 114

    def infer_net(self):
Z
zhangwenhui03 已提交
115
        self.model(is_infer=True)