NFFM的原理与代码

本篇深入分析郭大nffm的代码

TensorFlow计算图

计算图的构建

ones = tf.ones_like(emb_inp_v2)
mask_a = tf.matrix_band_part(ones, 0, -1) # Upper triangular matrix of 0s and 1s
mask_b = tf.matrix_band_part(ones, 0, 0)  # Diagonal matrix of 0s and 1s
mask = tf.cast(mask_a - mask_b, dtype=tf.bool) # Make a bool mask
'''
True - True = 0 and 0 = False
'''

#DNN
dnn_input = tf.boolean_mask(emb_inp_v2, mask) # 把上三角减去对角线的部分取出来
dnn_input = tf.reshape(dnn_input,[tf.shape(emb_inp_v2)[0],hparams.feature_nums*(hparams.feature_nums-1)//2])

input_size=int(dnn_input.shape[-1])
for idx in range(len(hparams.hidden_size)):
    glorot = np.sqrt(2.0 / (input_size + hparams.hidden_size[idx]))
    W = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(input_size, hparams.hidden_size[idx])), dtype=np.float32)
    dnn_input=tf.tensordot(dnn_input,W,[[-1],[0]])
    if hparams.norm is True:
        dnn_input=self.batch_norm_layer(dnn_input,\
                                   self.use_norm,'norm_'+str(idx))
    dnn_input=tf.nn.relu(dnn_input)
    input_size=hparams.hidden_size[idx]

glorot = np.sqrt(2.0 / (hparams.hidden_size[-1] + 1))
W = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(hparams.hidden_size[-1], 1)), dtype=np.float32)
b = tf.Variable(tf.constant(-3.5), dtype=np.float32)        
w3=tf.tensordot(dnn_input,W,[[-1],[0]])+b 

为什么没有把单项特征算进去?

posted on 2019-08-06 10:16  Frank_Allen  阅读(979)  评论(0编辑  收藏  举报

导航