Ejemplo n.º 1
0
def FiBiNet(feature_metas,
            interaction_mode='all',
            interaction_mode_se='all',
            embedding_initializer='glorot_uniform',
            embedding_regularizer=tf.keras.regularizers.l2(1e-5),
            fixed_embedding_dim=None,
            dnn_hidden_units=(128, 64, 1),
            dnn_activations=('relu', 'relu', None),
            dnn_use_bias=True,
            dnn_use_bn=False,
            dnn_dropout=0,
            dnn_kernel_initializers='glorot_uniform',
            dnn_bias_initializers='zeros',
            dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
            dnn_bias_regularizers=None,
            name='FiBiNet'):

    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)

        embedded_dict = features.get_embedded_dict(
            group_name='embedding',
            fixed_embedding_dim=fixed_embedding_dim,
            embedding_initializer=embedding_initializer,
            embedding_regularizer=embedding_regularizer,
            slots_filter=None)
        inputs = list(embedded_dict.values())
        interactions = BiInteraction(mode=interaction_mode)(inputs)

        inputs_se = SENet(axis=-1)(tf.stack(inputs, axis=1))
        interactions_se = BiInteraction(mode=interaction_mode_se)(split_tensor(
            inputs_se, axis=1))

        dnn_inputs = tf.concat([interactions, interactions_se], axis=1)

        dnn_output = DNN(units=dnn_hidden_units,
                         use_bias=dnn_use_bias,
                         activations=dnn_activations,
                         use_bn=dnn_use_bn,
                         dropout=dnn_dropout,
                         kernel_initializers=dnn_kernel_initializers,
                         bias_initializers=dnn_bias_initializers,
                         kernel_regularizers=dnn_kernel_regularizers,
                         bias_regularizers=dnn_bias_regularizers)(dnn_inputs)

        # Output
        output = tf.keras.activations.sigmoid(dnn_output)

        model = tf.keras.Model(inputs=features.get_inputs_list(),
                               outputs=output)

        return model
Ejemplo n.º 2
0
def DCN(
        feature_metas,
        cross_kernel_initializer='glorot_uniform',
        cross_kernel_regularizer=tf.keras.regularizers.l2(1e-5),
        cross_bias_initializer='zeros',
        cross_bias_regularizer=None,
        cross_layers_num=3,
        embedding_initializer='glorot_uniform',
        embedding_regularizer=tf.keras.regularizers.l2(1e-5),
        fixed_embedding_dim=None,
        dnn_hidden_units=(128, 64, 1),
        dnn_activations=('relu', 'relu', None),
        dnn_use_bias=True,
        dnn_use_bn=False,
        dnn_dropout=0,
        dnn_kernel_initializers='glorot_uniform',
        dnn_bias_initializers='zeros',
        dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
        dnn_bias_regularizers=None,
        name='Deep&CrossNetwork'):

    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)

        embedded_dict = features.get_embedded_dict(
            group_name='embedding',
            fixed_embedding_dim=fixed_embedding_dim,
            embedding_initializer=embedding_initializer,
            embedding_regularizer=embedding_regularizer,
            slots_filter=None
        )

        # Deep Part
        deep_inputs = features.gen_concated_feature(
            embedding_group='embedding',
            fixed_embedding_dim=fixed_embedding_dim,
            slots_filter=None
        )
        deep_output = DNN(
            units=dnn_hidden_units,
            use_bias=dnn_use_bias,
            activations=dnn_activations,
            use_bn=dnn_use_bn,
            dropout=dnn_dropout,
            kernel_initializers=dnn_kernel_initializers,
            bias_initializers=dnn_bias_initializers,
            kernel_regularizers=dnn_kernel_regularizers,
            bias_regularizers=dnn_bias_regularizers
        )(deep_inputs)

        # Cross Part
        cross_inputs = list(embedded_dict.values())
        cross_output = CrossNetwork(
            kernel_initializer=cross_kernel_initializer,
            kernel_regularizer=cross_kernel_regularizer,
            bias_initializer=cross_bias_initializer,
            bias_regularizer=cross_bias_regularizer
        )(cross_inputs, layers_num=cross_layers_num, require_logit=True)

        output = tf.keras.activations.sigmoid(deep_output + cross_output)

        model = tf.keras.Model(inputs=features.get_inputs_list(), outputs=output)

        return model
Ejemplo n.º 3
0
def NFFM(
        feature_metas,
        biinteraction_mode='all',
        embedding_initializer='glorot_uniform',
        embedding_regularizer=tf.keras.regularizers.l2(1e-5),
        fm_fixed_embedding_dim=None,
        linear_use_bias=True,
        linear_kernel_initializer=tf.keras.initializers.RandomNormal(stddev=1e-4, seed=1024),
        linear_kernel_regularizer=tf.keras.regularizers.l2(1e-5),
        dnn_hidden_units=(128, 64, 1),
        dnn_activations=('relu', 'relu', None),
        dnn_use_bias=True,
        dnn_use_bn=False,
        dnn_dropout=0,
        dnn_kernel_initializers='glorot_uniform',
        dnn_bias_initializers='zeros',
        dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
        dnn_bias_regularizers=None,
        name='NFFM'):

    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)

        # Linear Part
        with tf.name_scope('Linear'):
            linear_output = features.get_linear_logit(use_bias=linear_use_bias,
                                                      kernel_initializer=linear_kernel_initializer,
                                                      kernel_regularizer=linear_kernel_regularizer,
                                                      embedding_group='dot_embedding',
                                                      slots_filter=None)

        # Interaction Part
        with tf.name_scope('Interaction'):
            fm_embedded_dict = features.get_embedded_dict(
                group_name='embedding',
                fixed_embedding_dim=fm_fixed_embedding_dim,
                embedding_initializer=embedding_initializer,
                embedding_regularizer=embedding_regularizer,
                slots_filter=None
            )
            fm_dim_groups = group_embedded_by_dim(fm_embedded_dict)
            interactions = list()
            for fm_group in fm_dim_groups.values():
                group_interaction = BiInteraction(mode=biinteraction_mode)(fm_group)
                interactions.append(group_interaction)

            interactions = tf.concat(interactions, axis=1)

            dnn_output = DNN(
                units=dnn_hidden_units,
                use_bias=dnn_use_bias,
                activations=dnn_activations,
                use_bn=dnn_use_bn,
                dropout=dnn_dropout,
                kernel_initializers=dnn_kernel_initializers,
                bias_initializers=dnn_bias_initializers,
                kernel_regularizers=dnn_kernel_regularizers,
                bias_regularizers=dnn_bias_regularizers
            )(interactions)

        # Output
        output = tf.add_n([linear_output, dnn_output])
        output = tf.keras.activations.sigmoid(output)

        model = tf.keras.Model(inputs=features.get_inputs_list(), outputs=output)

        return model
Ejemplo n.º 4
0
def AFM(feature_metas,
        linear_slots,
        fm_slots,
        embedding_initializer='glorot_uniform',
        embedding_regularizer=tf.keras.regularizers.l2(1e-5),
        fm_fixed_embedding_dim=None,
        linear_use_bias=True,
        linear_kernel_initializer=tf.keras.initializers.RandomNormal(
            stddev=1e-4, seed=1024),
        linear_kernel_regularizer=tf.keras.regularizers.l2(1e-5),
        dnn_hidden_units=(128, 64, 1),
        dnn_activations=('relu', 'relu', None),
        dnn_use_bias=True,
        dnn_use_bn=False,
        dnn_dropout=0,
        dnn_kernel_initializers='glorot_uniform',
        dnn_bias_initializers='zeros',
        dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
        dnn_bias_regularizers=None,
        name='AFM'):

    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)

        # Linear Part
        with tf.name_scope('Linear'):
            linear_output = features.get_linear_logit(
                use_bias=linear_use_bias,
                kernel_initializer=linear_kernel_initializer,
                kernel_regularizer=linear_kernel_regularizer,
                embedding_group='dot_embedding',
                slots_filter=linear_slots)

        # Interaction
        with tf.name_scope('Interaction'):
            fm_embedded_dict = features.get_embedded_dict(
                group_name='embedding',
                fixed_embedding_dim=fm_fixed_embedding_dim,
                embedding_initializer=embedding_initializer,
                embedding_regularizer=embedding_regularizer,
                slots_filter=fm_slots)
            #  print(fm_embedded_dict)
            fm_dim_groups = group_embedded_by_dim(fm_embedded_dict)

            fms = [
                AttentionBasedPoolingLayer()(group)
                for group in fm_dim_groups.values() if len(group) > 1
            ]
            print("fms", fms)
            dnn_inputs = tf.concat(fms, axis=1)
            print("dnn_inputs", dnn_inputs)
            dnn_output = DNN(
                units=dnn_hidden_units,
                use_bias=dnn_use_bias,
                activations=dnn_activations,
                use_bn=dnn_use_bn,
                dropout=dnn_dropout,
                kernel_initializers=dnn_kernel_initializers,
                bias_initializers=dnn_bias_initializers,
                kernel_regularizers=dnn_kernel_regularizers,
                bias_regularizers=dnn_bias_regularizers)(dnn_inputs)

        # Output
        output = tf.add_n([linear_output, dnn_output])
        output = tf.keras.activations.sigmoid(output)

        model = tf.keras.Model(inputs=features.get_inputs_list(),
                               outputs=output)

        return model
Ejemplo n.º 5
0
def DeepFM(
        feature_metas,
        linear_slots,
        fm_slots,
        dnn_slots,
        embedding_initializer='glorot_uniform',
        embedding_regularizer=tf.keras.regularizers.l2(1e-5),
        fm_fixed_embedding_dim=None,
        linear_use_bias=True,
        #按照正态分布生成随机张量的初始化器。
        linear_kernel_initializer=tf.keras.initializers.RandomNormal(
            stddev=1e-4, seed=1024),
        linear_kernel_regularizer=tf.keras.regularizers.l2(1e-5),
        dnn_hidden_units=(128, 64, 1),
        dnn_activations=('relu', 'relu', None),
        dnn_use_bias=True,
        dnn_use_bn=False,
        dnn_dropout=0,
        dnn_kernel_initializers='glorot_uniform',
        dnn_bias_initializers='zeros',
        dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
        dnn_bias_regularizers=None,
        name='DeepFM'):

    # 检测是否是feature__metas类型是否是FeatrueMetas;
    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)
        #print(features.shape)

        # Linear Part
        with tf.name_scope('Linear'):
            #
            linear_output = features.get_linear_logit(
                use_bias=linear_use_bias,
                kernel_initializer=linear_kernel_initializer,
                kernel_regularizer=linear_kernel_regularizer,
                embedding_group='dot_embedding',
                slots_filter=linear_slots)

        # FM Part
        with tf.name_scope('FM'):
            fm_embedded_dict = features.get_embedded_dict(
                group_name='embedding',
                fixed_embedding_dim=fm_fixed_embedding_dim,
                embedding_initializer=embedding_initializer,
                embedding_regularizer=embedding_regularizer,
                slots_filter=fm_slots)
            fm_dim_groups = group_embedded_by_dim(fm_embedded_dict)
            fms = [
                FM()(group) for group in fm_dim_groups.values()
                if len(group) > 1
            ]
            fm_output = tf.add_n(fms)

        # DNN Part
        with tf.name_scope('DNN'):
            dnn_inputs = features.gen_concated_feature(
                embedding_group='embedding',
                fixed_embedding_dim=fm_fixed_embedding_dim,
                embedding_initializer=embedding_initializer,
                embedding_regularizer=embedding_regularizer,
                slots_filter=dnn_slots)
            dnn_output = DNN(
                units=dnn_hidden_units,
                use_bias=dnn_use_bias,
                activations=dnn_activations,
                use_bn=dnn_use_bn,
                dropout=dnn_dropout,
                kernel_initializers=dnn_kernel_initializers,
                bias_initializers=dnn_bias_initializers,
                kernel_regularizers=dnn_kernel_regularizers,
                bias_regularizers=dnn_bias_regularizers)(dnn_inputs)

        # Output 将计算矩阵进行拼接

        output = tf.add_n([linear_output, fm_output, dnn_output])

        output = tf.keras.activations.sigmoid(output)

        model = tf.keras.Model(inputs=features.get_inputs_list(),
                               outputs=output)

        return model
Ejemplo n.º 6
0
def AutoInt(feature_metas,
            seed=2333,
            interaction_layer_num=3,
            attention_embedding_size=8,
            attention_heads=2,
            interaction_use_res=True,
            embedding_initializer='glorot_uniform',
            embedding_regularizer=tf.keras.regularizers.l2(1e-5),
            fixed_embedding_dim=None,
            dnn_hidden_units=(128, 64, 1),
            dnn_activations=('relu', 'relu', None),
            dnn_use_bias=True,
            dnn_use_bn=False,
            dnn_dropout=0,
            dnn_kernel_initializers='glorot_uniform',
            dnn_bias_initializers='zeros',
            dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
            dnn_bias_regularizers=None,
            name='AutoInt'):

    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)

        embedded_dict = features.get_embedded_dict(
            slots_filter=None,
            fixed_embedding_dim=fixed_embedding_dim,
            embedding_initializer=embedding_initializer,
            embedding_regularizer=embedding_regularizer,
            group_name='embedding')
        grouped_embedded = group_embedded_by_dim(embedded_dict)
        grouped_inputs = [
            tf.stack(group, axis=1) for group in grouped_embedded.values()
        ]
        for _ in range(interaction_layer_num):
            for i in range(len(grouped_inputs)):
                grouped_inputs[i] = AutoIntInteraction(
                    att_embedding_size=attention_embedding_size,
                    heads=attention_heads,
                    use_res=interaction_use_res,
                    seed=seed)(grouped_inputs[i])

        dnn_inputs = tf.keras.layers.Flatten()(tf.concat(grouped_inputs,
                                                         axis=2))
        output = DNN(units=dnn_hidden_units,
                     use_bias=dnn_use_bias,
                     activations=dnn_activations,
                     use_bn=dnn_use_bn,
                     dropout=dnn_dropout,
                     kernel_initializers=dnn_kernel_initializers,
                     bias_initializers=dnn_bias_initializers,
                     kernel_regularizers=dnn_kernel_regularizers,
                     bias_regularizers=dnn_bias_regularizers)(dnn_inputs)

        output = tf.keras.activations.sigmoid(output)

        model = tf.keras.Model(inputs=features.get_inputs_list(),
                               outputs=output)

        return model
Ejemplo n.º 7
0
def PNN(feature_metas,
        use_inner_product=True,
        use_outer_product=False,
        outer_kernel_initializer='glorot_uniform',
        outer_kernel_regularizer=tf.keras.regularizers.l2(1e-5),
        embedding_initializer='glorot_uniform',
        embedding_regularizer=tf.keras.regularizers.l2(1e-5),
        fixed_embedding_dim=None,
        dnn_hidden_units=(128, 64, 1),
        dnn_activations=('relu', 'relu', None),
        dnn_use_bias=True,
        dnn_use_bn=False,
        dnn_dropout=0,
        dnn_kernel_initializers='glorot_uniform',
        dnn_bias_initializers='zeros',
        dnn_kernel_regularizers=tf.keras.regularizers.l2(1e-5),
        dnn_bias_regularizers=None,
        name='PNN'):

    assert isinstance(feature_metas, FeatureMetas)

    with tf.name_scope(name):

        features = Features(metas=feature_metas)

        embedded_dict = features.get_embedded_dict(
            group_name='embedding',
            fixed_embedding_dim=fixed_embedding_dim,
            embedding_initializer=embedding_initializer,
            embedding_regularizer=embedding_regularizer,
            slots_filter=None)
        raw_embedded_inputs = features.gen_concated_feature(
            embedding_group='embedding',
            fixed_embedding_dim=fixed_embedding_dim,
            slots_filter=None)
        inputs = [raw_embedded_inputs]

        if use_inner_product:
            inner_product_inputs = InnerProduct()(list(embedded_dict.values()))
            inputs.append(inner_product_inputs)

        if use_outer_product:
            outer_product_inputs = OuterProduct(
                outer_kernel_regularizer=outer_kernel_regularizer,
                outer_kernel_initializer=outer_kernel_initializer)(list(
                    embedded_dict.values()))
            inputs.append(outer_product_inputs)

        inputs = tf.concat(inputs, axis=1)
        output = DNN(units=dnn_hidden_units,
                     use_bias=dnn_use_bias,
                     activations=dnn_activations,
                     use_bn=dnn_use_bn,
                     dropout=dnn_dropout,
                     kernel_initializers=dnn_kernel_initializers,
                     bias_initializers=dnn_bias_initializers,
                     kernel_regularizers=dnn_kernel_regularizers,
                     bias_regularizers=dnn_bias_regularizers)(inputs)

        output = tf.keras.activations.sigmoid(output)

        model = tf.keras.Model(inputs=features.get_inputs_list(),
                               outputs=output)

        return model