特徵交叉
特徵組合的意義就不多多贅述了。在這裡主要介紹2種流行的特徵組合手段:FM和DCN
FM
推導:
舉例說明:
設 a=(2,3) , b=(1,2), 則 ab=(2,6)用上面的公式推導:
tesorlfow很容易實現:
def cross(self,x,share_EmbedWeights):
"""
先x的每個特徵進行embedding得到vi,然後 xi*vi ,然後再求內積
目標:[batch, n] * [n,k] =[batch,n,k]
轉化為: [n*[batch, 1]] * [n*[batch,k]] =[batch,n,k], x的每一行要轉化成一個對角矩陣
"""
n=x.shape[1].value #feature nums
k=self.k
#
embeds = []
for i in range(n):
xi = x[:,i] # shape=[batch]
vi = tf.nn.embedding_lookup(share_EmbedWeights, i) #shape=[k]
# 直接 xi*vi 報錯
# xi*v_i=[batch,1] * [1,k]=[batch,k]
embed_i = tf.expand_dims(xi,1) * tf.expand_dims(vi ,0)
embeds.append(embed_i)
embed = tf.reshape(tf.concat(embeds, 1),[-1,n,k]) # shape=[-1,n,k]
#
# sum_square 表示先sum後square. sum是axis=1,而不是axis=2
sum_square = tf.square(tf.reduce_sum(embed, axis=1))
square_sum = tf.reduce_sum(tf.square(embed), axis=1)
y_v = 0.5 * tf.reduce_sum(sum_square - square_sum, axis=1)
return y_v
DCN
核心部分是cross layer