推荐系统(六):基于DeepFM的推荐算法

一、基本原理

除了模型,也是被广泛应用在点击率预测中的深度学习模型,主要关注如何学习用户行为(user behavior)的组合特征(feature interactions),从而最大化推荐系统CTR。
是一个集成了(factorization machine)和的神经网络框架,分别承担和的部分。DeepFM的和部分共享相同的输入,可以提高训练效率,不需要额外的特征工程,用FM建模低阶的特征组合,用DNN建模高阶的特征组合,因此可以同时从原始特征中学习到高阶和低阶的特征交互。

DeepFM模型结构(左边为FM层,右边为DNN层)

1.1 FM 结构

FM 包括一阶项和二阶项,一阶项主要是讲特征分别乘上对应的系数,二阶项主要是对和两两组合,并且找到其分别对应的特征向量。

FM模型结构

为了更方便实现二阶部分,可以转化为和平方平方和两个部分:
\begin{aligned} & \sum_{i=1}^{n-1} \sum_{j=i+1}^{n}\left\langle\mathbf{v}_{i}, \mathbf{v}_{j}\right\rangle x_{i} x_{j} \\ =& \frac{1}{2} \sum_{i=1}^{n} \sum_{j=i+1}^{n}\left\langle\mathbf{v}_{i}, \mathbf{v}_{j}\right\rangle x_{i} x_{j}-\frac{1}{2} \sum_{i=1}^{n}\left\langle\mathbf{v}_{i}, \mathbf{v}_{i}\right\rangle x_{i} x_{i} \\ =& \frac{1}{2}\left(\sum_{i=1}^{n} \sum_{j=1}^{n} \sum_{f=1}^{k} {v}_{i, f} v_{j, f} x_{i} x_{j}-\sum_{i=1}^{n} \sum_{f=1}^{k} v_{i, f} v_{i, f} x_{i} x_{i}\right) \\ =& \frac{1}{2} \sum_{f=1}^{k}\left(\left(\sum_{i=1}^{n} v_{i, f} x_{i}\right)\left(\sum_{j=1}^{n} v_{j, f} x_{j}\right)-\sum_{i=1}^{n} v_{i, f}^{2} x_{i}^{2}\right) \\ =& \frac{1}{2} \sum_{f=1}^{k}\left(\left(\sum_{i=1}^{n} v_{i, f} x_{i}\right)^{2}-\sum_{i=1}^{n} v_{i, f}^{2} x_{i}^{2}\right) \end{aligned}

1.2 DNN 结构

深度部分是一个前馈神经网络,CTR预估的原始特征输入向量具有高稀疏、超高维度,分类、连续数据混合,特征按照field分组等特征,因此在第一层隐含层之前,引入一个嵌入层来完成将输入向量压缩到低维稠密向量。

DNN 结构

嵌入层(embedding layer)的结构如下图所示。当前网络结构有两个有趣的特性,1)尽管不同field的输入长度不同,但是embedding之后向量的长度均为K。2)在FM里得到的隐变量现在作为了嵌入层网络的权重。

嵌入层结构

二、算法实践

仍然采用Porto Seguro’s Safe Driver Prediction 的数据,数据的介绍可见上期:推荐系统(五):基于深度学习的推荐模型。数据的加载与处理如上期相同,即:

TRAIN_FILE = "Driver_Prediction_Data/train.csv"
TEST_FILE = "Driver_Prediction_Data/test.csv"

NUMERIC_COLS = [
    "ps_reg_01", "ps_reg_02", "ps_reg_03",
    "ps_car_12", "ps_car_13", "ps_car_14", "ps_car_15"
]

IGNORE_COLS = [
    "id", "target",
    "ps_calc_01", "ps_calc_02", "ps_calc_03", "ps_calc_04",
    "ps_calc_05", "ps_calc_06", "ps_calc_07", "ps_calc_08",
    "ps_calc_09", "ps_calc_10", "ps_calc_11", "ps_calc_12",
    "ps_calc_13", "ps_calc_14",
    "ps_calc_15_bin", "ps_calc_16_bin", "ps_calc_17_bin",
    "ps_calc_18_bin", "ps_calc_19_bin", "ps_calc_20_bin"
]

train_data = pd.read_csv(TRAIN_FILE)
test_data = pd.read_csv(TEST_FILE)
data = pd.concat([train_data,test_data])

feature_dict = {}
total_feature = 0
for col in data.columns:
    if col in IGNORE_COLS:
        continue
    elif col in NUMERIC_COLS:
        feature_dict[col] = total_feature
        total_feature += 1
    else:
        unique_val = data[col].unique()
        feature_dict[col] = dict(zip(unique_val,range(total_feature,len(unique_val)+total_feature)))
        total_feature += len(unique_val)
        
# convert train dataset
train_y = train_data[['target']].values.tolist()
train_data.drop(['target','id'],axis=1, inplace=True)
train_feature_index = train_data.copy()
train_feature_value = train_data.copy()

for col in train_feature_index.columns:
    if col in IGNORE_COLS:
        train_feature_index.drop(col,axis=1, inplace=True)
        train_feature_value.drop(col,axis=1, inplace=True)
        continue
    elif col in NUMERIC_COLS:
        train_feature_index[col] = feature_dict[col]
    else:
        train_feature_index[col] = train_feature_index[col].map(feature_dict[col])
        train_feature_value[col] = 1
        
field_size = train_feature_value.shape[1] 

包的调用与上期完全一致,类的定义也基本一致,略有差别的是增加了self.use_fm,self.use_deep两个变量。

class DeepFM(BaseEstimator,TransformerMixin):
    def __init__(self, feature_size=100, embedding_size=8, deep_layers=[32,32],batch_size=256,
                 learning_rate=0.001,optimizer='adam',random_seed=2020,used_fm=True,
                 use_deep=True,loss_type='logloss',l2_reg=0.0, field_size=39):
        self.feature_size = feature_size
        self.field_size = field_size
        self.embedding_size = embedding_size
        
        self.deep_layers = deep_layers
        self.deep_layers_activation = tf.nn.relu
        self.use_fm = used_fm
        self.use_deep = use_deep
        self.l2_reg = l2_reg
        
        self.batch_size = batch_size
        self.learning_rate = learning_rate
        self.optimizer_type = optimizer
        
        self.random_seed = random_seed
        self.loss_type = loss_type
        self.train_result, self.valid_result = [], []
        self.max_iteration = 200
        
        self._init_graph()

初始化权值,包括嵌入层、深度层、连接层。

    def _initialize_weights(self):
        weights = dict()
        
        # embeddings layer
        weights['feature_embeddings'] = tf.Variable(tf.random_normal([self.feature_size,self.embedding_size],0.0,0.01), name='feature_embeddings')
        weights['feature_bias'] = tf.Variable(tf.random_normal([self.feature_size,1],0.0,1.0),name='feature_bias')
        
        # deep layers
        input_size = self.field_size * self.embedding_size
        glorot = np.sqrt(2.0/(input_size + self.deep_layers[0]))
        weights['layer_0'] = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(input_size, self.deep_layers[0])),dtype=np.float32)
        weights['bias_0'] = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(1,self.deep_layers[0])),dtype=np.float32)
        
        glorot = np.sqrt(2.0 / (self.deep_layers[0]+self.deep_layers[1]))
        weights['layer_1'] = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(self.deep_layers[0], self.deep_layers[1])), dtype=np.float32)
        weights['bias_1'] = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(1, self.deep_layers[1])),dtype=np.float32)
        
        # final concat projection layer
        if self.use_fm and self.use_deep:
            input_size = self.field_size + self.embedding_size + self.deep_layers[1]
        elif self.use_fm:
            input_size = self.field_size + self.embedding_size
        elif self.use_deep:
            input_size = self.deep_layers[1]
        
        glorot = np.sqrt(2.0 / (input_size+1))
        weights['concat_projection'] = tf.Variable(np.random.normal(loc=0, scale=glorot, size=(input_size, 1)),dtype=np.float32)
        weights['concat_bias'] = tf.Variable(tf.constant(0.01), dtype=np.float32)
        
        return weights

初始化图,其结构如理论部分所示,其中FM部分结构分为一阶和二阶部分,并分别计算。

    def _init_graph(self):
        self.graph = tf.Graph()
        with self.graph.as_default():
            tf.set_random_seed(self.random_seed)
            
            self.feat_index = tf.placeholder(tf.int32, shape=[None,None], name='feat_index')
            self.feat_value = tf.placeholder(tf.float32, shape=[None,None], name='feat_value')
            
            self.label = tf.placeholder(tf.float32, shape=[None,1], name='label')
            self.dropout_keep_deep = tf.placeholder(tf.float32, shape=[None], name='dropout_keep_deep')
            self.train_phase = tf.placeholder(tf.bool, name='train_phase')
            
            self.weights = self._initialize_weights()
            
            # model 
            self.embeddings = tf.nn.embedding_lookup(self.weights['feature_embeddings'], self.feat_index)  
            feat_value = tf.reshape(self.feat_value, shape=[-1, self.field_size, 1])
            self.embeddings = tf.multiply(self.embeddings, feat_value)
            
            # first order term
            self.y_first_order = tf.nn.embedding_lookup(self.weights['feature_bias'], self.feat_index)
            self.y_first_order = tf.reduce_sum(tf.multiply(self.y_first_order, feat_value), 2)
            
            # second order term   (sum-square-part)
            self.summed_features_emb = tf.reduce_sum(self.embeddings,1)  # None * K
            self.summed_features_emb_square = tf.square(self.summed_features_emb)  # None * K
            
            # squre-sum-part
            self.squared_features_emb = tf.square(self.embeddings)
            self.squared_sum_features_emb = tf.reduce_sum(self.squared_features_emb, 1) # None * K
            
            # second order
            self.y_second_order = 0.5 * tf.subtract(self.summed_features_emb_square, self.squared_sum_features_emb)
            
            # deep component
            self.y_deep = tf.reshape(self.embeddings, shape=[-1, self.field_size * self.embedding_size])
            self.y_deep = tf.add(tf.matmul(self.y_deep, self.weights['layer_0']), self.weights['bias_0'])
            self.y_deep = self.deep_layers_activation(self.y_deep)
            
            self.y_deep = tf.add(tf.matmul(self.y_deep, self.weights['layer_1']),self.weights['bias_1'])
            self.y_deep = self.deep_layers_activation(self.y_deep)
                 
            # -----DeepFM-----
            self.concat_input = tf.concat([self.y_first_order, self.y_second_order, self.y_deep], axis=1)
            self.out = tf.add(tf.matmul(self.concat_input, self.weights['concat_projection']), self.weights['concat_bias'])

本文提供了多种损失函数和优化方法可供选择,同时也对模型进行了保存和记录。

            # loss
            if self.loss_type == 'logloss':
                self.out = tf.nn.sigmoid(self.out)
                self.loss = tf.losses.log_loss(self.label, self.out)
            elif self.loss_type == 'mse':
                self.loss = tf.nn.l2_loss(tf.subtract(self.label, self.out))
                
            # l2 regularization on weights
            if self.l2_reg > 0:
                self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights['concat_projection'])
                if self.use_deep:
                    self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights['layer_0'])
                    self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(self.weights['layer_1'])
                    
            # optimize
            if self.optimizer_type == 'adam':
                self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-8).minimize(self.loss)
            elif self.optimizer_type == 'adagrad':
                self.optimizer = tf.train.AdagradDAOptimizer(learning_rate=self.learning_rate, initial_accumulator_value=1e-8).minimize(self.loss)
            elif self.optimizer_type == 'gd':
                self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate).minimize(self.loss)
            elif self.optimizer_type == 'momentum':
                self.optimizer = tf.train.MomentumOptimizer(learning_rate=self.learning_rate, momentum=0.95).minimize(self.loss)
        
            # init
            self.saver = tf.train.Saver()
            save_path = "deepfm/model.ckpt"
            
            init = tf.global_variables_initializer()
            self.sess = tf.Session()
            self.sess.run(init)
            
            save_path = self.saver.save(self.sess, save_path, global_step=1)
            
            writer = tf.summary.FileWriter("D:/logs/deepfm/", tf.get_default_graph())
            writer.close()        

根据保存的网络结构,可通过tensorboard查看网络结构,如下所示:


定义训练函数,及实例化类并完成训练。

    def train(self, train_feature_index, train_feature_value, train_y):
        with tf.Session(graph=self.graph) as sess:
            sess.run(tf.global_variables_initializer())
            for i in range(self.max_iteration):
                epoch_loss, _ = sess.run([self.loss, self.optimizer],feed_dict={self.feat_index:train_feature_index,
                                                                                self.feat_value:train_feature_value,
                                                                                self.label:train_y})
                print('epoch %s, loss is %s' % (str(i), str(epoch_loss)))

deepfm = DeepFM(feature_size=total_feature, field_size=field_size, embedding_size=8)  
deepfm.train(train_feature_index, train_feature_value, train_y) 

结果如下所示:

epoch 0, loss is 1.2481447
epoch 1, loss is 1.2091292
epoch 2, loss is 1.1717732
epoch 3, loss is 1.1359197
epoch 4, loss is 1.1009481
epoch 5, loss is 1.0666977
···
epoch 195, loss is 0.15519407
epoch 196, loss is 0.15500054
epoch 197, loss is 0.15480281
epoch 198, loss is 0.15460847
epoch 199, loss is 0.15441668

参考资料

[1]. Guo, Huifeng, Tang, Ruiming, Ye, Yunming, Li, Zhenguo, & He, Xiuqiang. . Deepfm: a factorization-machine based neural network for ctr prediction.
[2]. 推荐系统与深度学习. 黄昕等. 清华大学出版社. 2019.
[3]. https://github.com/wangby511/Recommendation_System

春天,遂想起江南,
唐诗里的江南,九岁时
采桑叶于其中,捉蜻蜓于其中
——余光中《春天,遂想起》

你可能感兴趣的:(推荐系统(六):基于DeepFM的推荐算法)