场感知因子分解机器的原理与代码
本篇深入分析郭大ffm的代码
TensorFlow计算图
传入数据的计算图
self.label = tf.placeholder(shape=(None), dtype=tf.float32)
self.features=tf.placeholder(shape=(None,hparams.feature_nums), dtype=tf.int32)
计算图的参数
self.emb_v1=tf.get_variable(shape=[hparams.hash_ids,1],
initializer=initializer,name='emb_v1')
self.emb_v2=tf.get_variable(shape=[hparams.hash_ids,hparams.feature_nums,hparams.k],
initializer=initializer,name='emb_v2') # fm中是二维的,这里是三维的
计算图的构建
# models/ffm.py build_graph()
#lr
emb_inp_v1=tf.gather(self.emb_v1, self.features) # shape = [batch_size, attr_nums, 1]
w1=tf.reduce_sum(emb_inp_v1,[-1,-2])
# 交叉项
# self.features shape = [batch_size, attr_nums]
emb_inp_v2=tf.gather(self.emb_v2, self.features) # shape = [batch_size, attr_nums, attr_nums, k] # 每个attr从属于一个场
emb_inp_v2=tf.reduce_sum(emb_inp_v2*tf.transpose(emb_inp_v2,[0,2,1,3]),-1) # shape=[batch_size, attr_nums, attr_nums]
temp=[]
for i in range(hparams.feature_nums):
if i!=0:
temp.append(emb_inp_v2[:,i,:i]) # 取下三角
w2=tf.reduce_sum(tf.concat(temp,-1),-1) # 所有下三角的元素concat成一个列表
posted on 2019-08-06 09:55 Frank_Allen 阅读(312) 评论(0) 编辑 收藏 举报