deepFm的keras实现

参考

知乎

  1. FM因子分解机的原理、公式推导、Python实现和应用。其中return K.mean(a-b, 1, keepdims=True)*0.5有问题吧?应该是return K.sum(a-b, 1, keepdims=True)*0.5?

  2. CTR预估模型:DeepFM/Deep&Cross/xDeepFM/AutoInt代码实战与讲解。二阶和DNN部分,只用了稀疏特征,缺少连续特征。

  3. 用keras实现deepFM。只用3个特征,统一性和扩展性不强。

  4. Deepfm原理和源码 一篇就好

  5. TF 2.0 Keras 实现 DeepFM

csdn

  • 用Keras实现一个DeepFM
  • tensorflow2实现DeepFM(基于DataFrame格式训练数据)

博文1代码详解

增加了注释便于理解。

import tensorflow as tf
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
K = tf.keras.backend


# 自定义FM的二阶交叉层
class FMLayer(tf.keras.layers.Layer):
    # FM的k取4(演示方便)
    def __init__(self, input_dim, output_dim=4, **kwargs):
        self.input_dim = input_dim
        self.output_dim = output_dim
        super(FMLayer, self).__init__(**kwargs)
   	
   	# 初始化训练权重
    def build(self, input_shape):
        self.kernel = self.add_weight(name='kernel',	# F*K
                                      shape=(self.input_dim, self.output_dim),
                                      initializer='glorot_uniform',
                                      trainable=True)
        super(FMLayer, self).build(input_shape)

    # 自定义FM的二阶交叉项的计算公式
    def call(self, x):  # x维度B*F
        a = K.pow(K.dot(x, self.kernel), 2) # B*K 
        b = K.dot(K.pow(x, 2), K.pow(self.kernel, 2)) # B*K
        return K.sum(a-b, 1, keepdims=True)*0.5 # B*1, 原代码K.mean应该有误。
	
	# 输出的尺寸大小
    def compute_output_shape(self, input_shape):
        return input_shape[0], 1	# 原代码为self.output_dim有问题


# 实现FM算法
def FM(feature_dim):
    inputs = tf.keras.Input((feature_dim,))  # B*F
    # 线性回归
    liner = tf.keras.layers.Dense(units=1,
    	bias_regularizer=tf.keras.regularizers.l2(0.01),
    	kernel_regularizer=tf.keras.regularizers.l1(0.02)
    	)(inputs) # B*1
    # FM的二阶交叉项
    cross = FMLayer(feature_dim)(inputs) # B*1

    # 获得FM模型(线性回归 + FM的二阶交叉项)
    add = tf.keras.layers.Add()([liner, cross]) # B*1
    predictions = tf.keras.layers.Activation('sigmoid')(add) # B*1

    model = tf.keras.Model(inputs=inputs, outputs=predictions)
    model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
                  loss=tf.keras.losses.binary_crossentropy,
                  metrics=[tf.keras.metrics.binary_accuracy])
    return model

# 训练FM模型
def train():
    fm = FM(30)
    data = load_breast_cancer()

    # sklearn 切分数据
    X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2,
                                                        random_state=11, stratify=data.target)
    fm.fit(X_train, y_train, epochs=5, batch_size=20, validation_data=(X_test, y_test))
    return fm


if __name__ == '__main__':
    fm = train()
    fm.summary()
    

你可能感兴趣的:(小熊tensorflow笔记)