FM是2010年提出的模型,凭借其在数据量比较大并且特征稀疏的情况下,仍然能够得到优秀的性能和效果的特性,FM是为了解决稀疏数据的特征组合问题(多项式模型容易出现的问题)
背景
在推荐领域,点击率CTR(click-through rate)和转化率CVR(conversion rate)是两个关键指标。预估CTR/CVR,业界常用的方法有人工特征工程 + LR(Logistic Regression)、GBDT(Gradient Boosting Decision Tree) + LR[1][2][3]、FM(Factorization Machine)[2][7]和FFM(Field-aware Factorization Machine)[9]模型。
分析
当使用多项式模型时,特征经过onehot编码以后,数据变得稀疏,而且存在很多特征值为0的情况。因为上述公式在求导的时候非0,权重才有意义。为解决这个问题,提出FM算法
FM定义
公式改进
FM求导
#-*- coding:utf-8 -*-
# @Time:2020/11/5 17:38
# @Auther :lizhe
# @File:FM.py
# @Email:bylz0213@gmail.com
import tensorflow.compat.v1 as tf1
class FM:
def __init__(self,feat_num,hidden_num):
self.feat_num = feat_num
self.hidden_num = hidden_num
self.x = tf1.placeholder(dtype=tf1.float32, shape=[None, feat_num], name='input_x')
self.y = tf1.placeholder(dtype=tf1.float32, shape=[None, 1], name='input_y')
w_bias= tf1.get_variable(name="weight_bias", shape=[1], dtype=tf1.float32)
w_linear = tf1.get_variable(name='linear_weight', shape=[feat_num], dtype=tf1.float32)
w_h = tf1.get_variable(name="interaction_w", shape=(self.feat_num, self.hidden_num), dtype=tf1.float32)
linear = w_bias + tf1.reduce_sum(tf1.multiply(self.x, w_linear), axis=-1, keep_dims=True)
interaction_part = 0.5 * tf1.reduce_sum(
tf1.square(tf1.matmul(self.x, w_h)) - tf1.matmul(tf1.square(self.x), tf1.square(w_h)),
axis=-1,keep_dims=True)
y_hat = linear + interaction_part
self.loss = tf1.reduce_mean(tf1.square(self.y - y_hat))