dist_ctr.py 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#   Copyright (c) 2018 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
import paddle.fluid as fluid
17
import os
18 19 20 21 22

import dist_ctr_reader
from test_dist_base import TestDistRunnerBase, runtime_main

IS_SPARSE = True
23
os.environ['PADDLE_ENABLE_REMOTE_PREFETCH'] = "1"
24 25 26 27 28 29 30 31

# Fix seed for test
fluid.default_startup_program().random_seed = 1
fluid.default_main_program().random_seed = 1


class TestDistCTR2x2(TestDistRunnerBase):
    def get_model(self, batch_size=2):
32

33 34
        dnn_input_dim, lr_input_dim = dist_ctr_reader.load_data_meta()
        """ network definition """
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        dnn_data = fluid.layers.data(
            name="dnn_data",
            shape=[-1, 1],
            dtype="int64",
            lod_level=1,
            append_batch_size=False,
        )
        lr_data = fluid.layers.data(
            name="lr_data",
            shape=[-1, 1],
            dtype="int64",
            lod_level=1,
            append_batch_size=False,
        )
        label = fluid.layers.data(
            name="click",
            shape=[-1, 1],
            dtype="int64",
            lod_level=0,
            append_batch_size=False,
        )
56 57 58 59 60 61 62 63 64

        # build dnn model
        dnn_layer_dims = [128, 64, 32, 1]
        dnn_embedding = fluid.layers.embedding(
            is_distributed=False,
            input=dnn_data,
            size=[dnn_input_dim, dnn_layer_dims[0]],
            param_attr=fluid.ParamAttr(
                name="deep_embedding",
65 66 67 68 69 70 71
                initializer=fluid.initializer.Constant(value=0.01),
            ),
            is_sparse=IS_SPARSE,
        )
        dnn_pool = fluid.layers.sequence_pool(
            input=dnn_embedding, pool_type="sum"
        )
72 73 74 75 76 77 78
        dnn_out = dnn_pool
        for i, dim in enumerate(dnn_layer_dims[1:]):
            fc = fluid.layers.fc(
                input=dnn_out,
                size=dim,
                act="relu",
                param_attr=fluid.ParamAttr(
79 80 81 82
                    initializer=fluid.initializer.Constant(value=0.01)
                ),
                name='dnn-fc-%d' % i,
            )
83 84 85 86 87 88 89 90 91
            dnn_out = fc

        # build lr model
        lr_embbding = fluid.layers.embedding(
            is_distributed=False,
            input=lr_data,
            size=[lr_input_dim, 1],
            param_attr=fluid.ParamAttr(
                name="wide_embedding",
92 93 94 95
                initializer=fluid.initializer.Constant(value=0.01),
            ),
            is_sparse=IS_SPARSE,
        )
96 97 98 99 100 101
        lr_pool = fluid.layers.sequence_pool(input=lr_embbding, pool_type="sum")

        merge_layer = fluid.layers.concat(input=[dnn_out, lr_pool], axis=1)

        predict = fluid.layers.fc(input=merge_layer, size=2, act='softmax')
        acc = fluid.layers.accuracy(input=predict, label=label)
102 103 104
        auc_var, batch_auc_var, auc_states = fluid.layers.auc(
            input=predict, label=label
        )
105
        cost = fluid.layers.cross_entropy(input=predict, label=label)
106
        avg_cost = paddle.mean(x=cost)
107 108 109

        inference_program = paddle.fluid.default_main_program().clone()

110
        regularization = None
Q
Qiao Longfei 已提交
111
        use_l2_decay = bool(os.getenv('USE_L2_DECAY', 0))
Q
Qiao Longfei 已提交
112
        if use_l2_decay:
113
            regularization = fluid.regularizer.L2DecayRegularizer(
114 115
                regularization_coeff=1e-1
            )
116 117 118
        use_lr_decay = bool(os.getenv('LR_DECAY', 0))
        lr = 0.0001
        if use_lr_decay:
119 120 121 122 123 124 125 126 127 128
            lr = fluid.layers.exponential_decay(
                learning_rate=0.0001,
                decay_steps=10000,
                decay_rate=0.999,
                staircase=True,
            )

        sgd_optimizer = fluid.optimizer.SGD(
            learning_rate=lr, regularization=regularization
        )
129 130 131 132 133 134
        sgd_optimizer.minimize(avg_cost)

        dataset = dist_ctr_reader.Dataset()
        train_reader = paddle.batch(dataset.train(), batch_size=batch_size)
        test_reader = paddle.batch(dataset.test(), batch_size=batch_size)

135 136 137 138 139 140 141 142
        return (
            inference_program,
            avg_cost,
            train_reader,
            test_reader,
            None,
            predict,
        )
143 144 145 146


if __name__ == "__main__":
    runtime_main(TestDistCTR2x2)