model.py 5.1 KB
Newer Older
Z
zhangwenhui03 已提交
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 numpy as np
Z
zhangwenhui03 已提交
16 17
import paddle.fluid as fluid

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


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

T
for mat  
tangwei 已提交
26 27
    def fc(self, tag, data, out_dim, active='prelu'):

Z
zhangwenhui03 已提交
28
        init_stddev = 1.0
T
for mat  
tangwei 已提交
29 30
        scales = 1.0 / np.sqrt(data.shape[1])

T
tangwei 已提交
31 32 33 34
        p_attr = fluid.param_attr.ParamAttr(
            name='%s_weight' % tag,
            initializer=fluid.initializer.NormalInitializer(
                loc=0.0, scale=init_stddev * scales))
T
for mat  
tangwei 已提交
35

T
tangwei 已提交
36 37
        b_attr = fluid.ParamAttr(
            name='%s_bias' % tag, initializer=fluid.initializer.Constant(0.1))
T
for mat  
tangwei 已提交
38

Z
zhangwenhui03 已提交
39
        out = fluid.layers.fc(input=data,
T
for mat  
tangwei 已提交
40 41 42 43 44
                              size=out_dim,
                              act=active,
                              param_attr=p_attr,
                              bias_attr=b_attr,
                              name=tag)
Z
zhangwenhui03 已提交
45
        return out
T
for mat  
tangwei 已提交
46

Z
zhangwenhui03 已提交
47 48
    def input_data(self):
        sparse_input_ids = [
T
tangwei 已提交
49 50 51 52 53
            fluid.data(
                name="field_" + str(i),
                shape=[-1, 1],
                dtype="int64",
                lod_level=1) for i in range(0, 23)
Z
zhangwenhui03 已提交
54 55 56 57 58
        ]
        label_ctr = fluid.data(name="ctr", shape=[-1, 1], dtype="int64")
        label_cvr = fluid.data(name="cvr", shape=[-1, 1], dtype="int64")
        inputs = sparse_input_ids + [label_ctr] + [label_cvr]
        self._data_var.extend(inputs)
T
for mat  
tangwei 已提交
59

Z
zhangwenhui03 已提交
60
        return inputs
T
for mat  
tangwei 已提交
61

Z
zhangwenhui03 已提交
62
    def net(self, inputs, is_infer=False):
T
for mat  
tangwei 已提交
63

T
tangwei 已提交
64 65 66 67
        vocab_size = envs.get_global_env("hyper_parameters.vocab_size", None,
                                         self._namespace)
        embed_size = envs.get_global_env("hyper_parameters.embed_size", None,
                                         self._namespace)
Z
zhangwenhui03 已提交
68 69
        emb = []
        for data in inputs[0:-2]:
T
tangwei 已提交
70 71 72 73 74 75 76 77 78 79 80
            feat_emb = fluid.embedding(
                input=data,
                size=[vocab_size, embed_size],
                param_attr=fluid.ParamAttr(
                    name='dis_emb',
                    learning_rate=5,
                    initializer=fluid.initializer.Xavier(
                        fan_in=embed_size, fan_out=embed_size)),
                is_sparse=True)
            field_emb = fluid.layers.sequence_pool(
                input=feat_emb, pool_type='sum')
Z
zhangwenhui03 已提交
81 82
            emb.append(field_emb)
        concat_emb = fluid.layers.concat(emb, axis=1)
T
for mat  
tangwei 已提交
83

Z
zhangwenhui03 已提交
84 85 86 87 88
        # ctr
        active = 'relu'
        ctr_fc1 = self.fc('ctr_fc1', concat_emb, 200, active)
        ctr_fc2 = self.fc('ctr_fc2', ctr_fc1, 80, active)
        ctr_out = self.fc('ctr_out', ctr_fc2, 2, 'softmax')
T
for mat  
tangwei 已提交
89

Z
zhangwenhui03 已提交
90 91 92
        # cvr
        cvr_fc1 = self.fc('cvr_fc1', concat_emb, 200, active)
        cvr_fc2 = self.fc('cvr_fc2', cvr_fc1, 80, active)
T
for mat  
tangwei 已提交
93 94
        cvr_out = self.fc('cvr_out', cvr_fc2, 2, 'softmax')

Z
zhangwenhui03 已提交
95 96
        ctr_clk = inputs[-2]
        ctcvr_buy = inputs[-1]
T
for mat  
tangwei 已提交
97

T
tangwei 已提交
98 99 100 101
        ctr_prop_one = fluid.layers.slice(
            ctr_out, axes=[1], starts=[1], ends=[2])
        cvr_prop_one = fluid.layers.slice(
            cvr_out, axes=[1], starts=[1], ends=[2])
T
for mat  
tangwei 已提交
102

T
tangwei 已提交
103 104 105 106
        ctcvr_prop_one = fluid.layers.elementwise_mul(ctr_prop_one,
                                                      cvr_prop_one)
        ctcvr_prop = fluid.layers.concat(
            input=[1 - ctcvr_prop_one, ctcvr_prop_one], axis=1)
Z
zhangwenhui03 已提交
107

T
tangwei 已提交
108 109 110 111
        auc_ctr, batch_auc_ctr, auc_states_ctr = fluid.layers.auc(
            input=ctr_out, label=ctr_clk)
        auc_ctcvr, batch_auc_ctcvr, auc_states_ctcvr = fluid.layers.auc(
            input=ctcvr_prop, label=ctcvr_buy)
Z
zhangwenhui03 已提交
112 113 114 115 116 117

        if is_infer:
            self._infer_results["AUC_ctr"] = auc_ctr
            self._infer_results["AUC_ctcvr"] = auc_ctcvr
            return

Z
zhangwenhui03 已提交
118
        loss_ctr = fluid.layers.cross_entropy(input=ctr_out, label=ctr_clk)
T
tangwei 已提交
119 120
        loss_ctcvr = fluid.layers.cross_entropy(
            input=ctcvr_prop, label=ctcvr_buy)
Z
zhangwenhui03 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134
        cost = loss_ctr + loss_ctcvr
        avg_cost = fluid.layers.mean(cost)

        self._cost = avg_cost
        self._metrics["AUC_ctr"] = auc_ctr
        self._metrics["BATCH_AUC_ctr"] = batch_auc_ctr
        self._metrics["AUC_ctcvr"] = auc_ctcvr
        self._metrics["BATCH_AUC_ctcvr"] = batch_auc_ctcvr

    def train_net(self):
        input_data = self.input_data()
        self.net(input_data)

    def infer_net(self):
Z
zhangwenhui03 已提交
135 136
        self._infer_data_var = self.input_data()
        self._infer_data_loader = fluid.io.DataLoader.from_generator(
T
tangwei 已提交
137 138 139 140
            feed_list=self._infer_data_var,
            capacity=64,
            use_double_buffer=False,
            iterable=False)
Z
zhangwenhui03 已提交
141
        self.net(self._infer_data_var, is_infer=True)