推荐算法—FM算法实现

Model:

import tensorflow as tf


class FMModel:
    def __init__(self,learning_rate,lambda_w,lambda_v,regular,k,p):
        """初始化成员变量"""
        self.learning_rate = learning_rate #学习率
        self.lambda_w = lambda_w #正则化参数
        self.lambda_v = lambda_v #正则化参数
        self.regular = regular # 正则项
        self.k = k # v 的行数
        self.p = p # 训练数据的列数
        self.w0 = tf.Variable(tf.zeros([1]),name="w0") #FM 模型的常数项
        self.w = tf.Variable(tf.random_normal([self.p]), name="w") #FM模型的线性部分的权重参数
        self.v = tf.Variable(tf.random_normal([self.k,self.p],mean=0,stddev=0.01),name="v") # 交叉项的权重参数
    
    def _create_placeholder(self):
        """ 定义容易存储数据"""
        with tf.name_scope("data"):
            self.x = tf.placeholder(tf.float32, [None,self.p], name="x")
            self.y = tf.placeholder(tf.float32, [None,1], name="y")
                        
    def _predict(self):
        """计算预测值"""
        with tf.device('/cpu:0'):      
            with tf.name_scope("predict"):
                
                self.y_hat = tf.add(tf.add(self.w0,tf.matmul(self.x,self.w)),
                                    0.5*tf.reduce_sum(tf.subtract(tf.pow(tf.matmul(self.x,
                                            tf.transpose(self.v)),2),tf.matmul(tf.pow(self.x,2),
                                                tf.transpose(tf.pow(self.v,2))))),name="y_hat")
   
    def _regular(self):
        """计算正则项"""
        with tf.name_scope("regular"):
            
            if self.regular == "l1":
                l1_norm = tf.reduce_sum(
                    tf.add(
                        tf.multiply(self.lambda_w, tf.abs(self.w)),
                        tf.multiply(self.lambda_v, tf.abs(self.v))
                    ))
                return l1_norm
            else:
                l2_norm = tf.reduce_sum(
                    tf.add(
                        tf.multiply(self.lambda_w,tf.pow(self.w,2)),
                        tf.multiply(self.lambda_v, tf.pow(self.v,2))
                        )
                    )
                return l2_norm 
            
    def _loss(self):
        """计算损失值"""
        with tf.device('/cpu:0'):
            with tf.name_scope("loss"):
                norm = self._regular()
                self.loss = tf.add(norm, tf.reduce_mean(tf.square(self.y-self.y_hat)))
    
    def _optimizer(self):
        """ 设定optimizer """
        with tf.device('/cpu:0'):
            with tf.name_scope("optimizer"):
                self.opt = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
    
    def _summaries(self):
        """ 设定summary,以便在Tensorboard里进行可视化 """
        with tf.name_scope("summaries"):
            tf.summary.scalar("loss",self.loss)
            tf.summary.histogram("histogram loss",self.loss)
            self.summary_op = tf.summary.merge_all()
            
    def build_graph(self):
        """ 构建整个图的Graph """
        self._create_placeholder()
        self._predict()
        self._loss()
        self._optimizer()  
        self._summaries()

模型训练后续更新···············

你可能感兴趣的:(推荐算法—FM算法实现)