network_conf.py 4.4 KB
Newer Older
C
CandyCaneLane 已提交
1 2 3 4
import paddle.fluid as fluid
import math


5 6 7 8 9 10 11
def ctr_deepfm_model(embedding_size,
                     num_field,
                     num_feat,
                     layer_sizes,
                     act,
                     reg,
                     is_sparse=False):
C
CandyCaneLane 已提交
12 13
    init_value_ = 0.1

14 15 16 17 18 19
    raw_feat_idx = fluid.data(
        name='feat_idx', shape=[None, num_field], dtype='int64')
    raw_feat_value = fluid.data(
        name='feat_value', shape=[None, num_field], dtype='float32')
    label = fluid.data(
        name='label', shape=[None, 1], dtype='float32')  # None * 1
C
CandyCaneLane 已提交
20 21

    feat_idx = fluid.layers.reshape(raw_feat_idx,
22
                                    [-1, 1])  # (None * num_field) * 1
C
CandyCaneLane 已提交
23 24 25 26 27
    feat_value = fluid.layers.reshape(
        raw_feat_value, [-1, num_field, 1])  # None * num_field * 1

    # -------------------- first order term  --------------------

28
    first_weights_re = fluid.embedding(
C
CandyCaneLane 已提交
29
        input=feat_idx,
30
        is_sparse=is_sparse,
C
CandyCaneLane 已提交
31 32 33 34 35 36
        dtype='float32',
        size=[num_feat + 1, 1],
        padding_idx=0,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.TruncatedNormalInitializer(
                loc=0.0, scale=init_value_),
37 38 39
            regularizer=fluid.regularizer.L1DecayRegularizer(reg)))
    first_weights = fluid.layers.reshape(
        first_weights_re, shape=[-1, num_field, 1])  # None * num_field * 1
C
CandyCaneLane 已提交
40 41 42 43
    y_first_order = fluid.layers.reduce_sum((first_weights * feat_value), 1)

    # -------------------- second order term  --------------------

44
    feat_embeddings_re = fluid.embedding(
C
CandyCaneLane 已提交
45
        input=feat_idx,
46
        is_sparse=is_sparse,
C
CandyCaneLane 已提交
47 48 49 50 51
        dtype='float32',
        size=[num_feat + 1, embedding_size],
        padding_idx=0,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.TruncatedNormalInitializer(
52 53 54 55 56
                loc=0.0, scale=init_value_ / math.sqrt(float(embedding_size)))))
    feat_embeddings = fluid.layers.reshape(
        feat_embeddings_re,
        shape=[-1, num_field,
               embedding_size])  # None * num_field * embedding_size
C
CandyCaneLane 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    feat_embeddings = feat_embeddings * feat_value  # None * num_field * embedding_size

    # sum_square part
    summed_features_emb = fluid.layers.reduce_sum(feat_embeddings,
                                                  1)  # None * embedding_size
    summed_features_emb_square = fluid.layers.square(
        summed_features_emb)  # None * embedding_size

    # square_sum part
    squared_features_emb = fluid.layers.square(
        feat_embeddings)  # None * num_field * embedding_size
    squared_sum_features_emb = fluid.layers.reduce_sum(
        squared_features_emb, 1)  # None * embedding_size

    y_second_order = 0.5 * fluid.layers.reduce_sum(
        summed_features_emb_square - squared_sum_features_emb, 1,
        keep_dim=True)  # None * 1

    # -------------------- DNN --------------------

    y_dnn = fluid.layers.reshape(feat_embeddings,
                                 [-1, num_field * embedding_size])
    for s in layer_sizes:
        y_dnn = fluid.layers.fc(
            input=y_dnn,
            size=s,
            act=act,
            param_attr=fluid.ParamAttr(
                initializer=fluid.initializer.TruncatedNormalInitializer(
                    loc=0.0, scale=init_value_ / math.sqrt(float(10)))),
            bias_attr=fluid.ParamAttr(
                initializer=fluid.initializer.TruncatedNormalInitializer(
                    loc=0.0, scale=init_value_)))
    y_dnn = fluid.layers.fc(
        input=y_dnn,
        size=1,
        act=None,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.TruncatedNormalInitializer(
                loc=0.0, scale=init_value_)),
        bias_attr=fluid.ParamAttr(
            initializer=fluid.initializer.TruncatedNormalInitializer(
                loc=0.0, scale=init_value_)))

    # ------------------- DeepFM ------------------

    predict = fluid.layers.sigmoid(y_first_order + y_second_order + y_dnn)
    cost = fluid.layers.log_loss(input=predict, label=label)
    batch_cost = fluid.layers.reduce_sum(cost)

    # for auc
    predict_2d = fluid.layers.concat([1 - predict, predict], 1)
    label_int = fluid.layers.cast(label, 'int64')
    auc_var, batch_auc_var, auc_states = fluid.layers.auc(input=predict_2d,
                                                          label=label_int,
                                                          slide_steps=0)

    return batch_cost, auc_var, [raw_feat_idx, raw_feat_value, label]