network_conf.py 2.6 KB
Newer Older
1 2 3 4 5 6 7
import paddle.v2 as paddle

dense_feature_dim = 13
sparse_feature_dim = 117568


def fm_layer(input, factor_size, fm_param_attr):
8 9 10
    first_order = paddle.layer.fc(input=input,
                                  size=1,
                                  act=paddle.activation.Linear())
11 12 13 14 15 16 17
    second_order = paddle.layer.factorization_machine(
        input=input,
        factor_size=factor_size,
        act=paddle.activation.Linear(),
        param_attr=fm_param_attr)
    out = paddle.layer.addto(
        input=[first_order, second_order],
W
wangmeng28 已提交
18
        act=paddle.activation.Linear(),
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        bias_attr=False)
    return out


def DeepFM(factor_size, infer=False):
    dense_input = paddle.layer.data(
        name="dense_input",
        type=paddle.data_type.dense_vector(dense_feature_dim))
    sparse_input = paddle.layer.data(
        name="sparse_input",
        type=paddle.data_type.sparse_binary_vector(sparse_feature_dim))
    sparse_input_ids = [
        paddle.layer.data(
            name="C" + str(i),
            type=paddle.data_type.integer_value(sparse_feature_dim))
        for i in range(1, 27)
    ]

    dense_fm = fm_layer(
        dense_input,
        factor_size,
        fm_param_attr=paddle.attr.Param(name="DenseFeatFactors"))
    sparse_fm = fm_layer(
        sparse_input,
        factor_size,
        fm_param_attr=paddle.attr.Param(name="SparseFeatFactors"))

    def embedding_layer(input):
        return paddle.layer.embedding(
            input=input,
            size=factor_size,
            param_attr=paddle.attr.Param(name="SparseFeatFactors"))

    sparse_embed_seq = map(embedding_layer, sparse_input_ids)
    sparse_embed = paddle.layer.concat(sparse_embed_seq)

55 56 57
    fc1 = paddle.layer.fc(input=[sparse_embed, dense_input],
                          size=400,
                          act=paddle.activation.Relu())
58 59 60
    fc2 = paddle.layer.fc(input=fc1, size=400, act=paddle.activation.Relu())
    fc3 = paddle.layer.fc(input=fc2, size=400, act=paddle.activation.Relu())

61 62 63
    predict = paddle.layer.fc(input=[dense_fm, sparse_fm, fc3],
                              size=1,
                              act=paddle.activation.Sigmoid())
64 65 66 67 68 69

    if not infer:
        label = paddle.layer.data(
            name="label", type=paddle.data_type.dense_vector(1))
        cost = paddle.layer.multi_binary_label_cross_entropy_cost(
            input=predict, label=label)
W
wangmeng28 已提交
70 71 72
        paddle.evaluator.classification_error(
            name="classification_error", input=predict, label=label)
        paddle.evaluator.auc(name="auc", input=predict, label=label)
73 74 75
        return cost
    else:
        return predict