TensorFlow2实现协同过滤算法中的矩阵分解(首家基于TS2版本)
目標:
用TensorFlow2,實現協同過濾算法中的矩陣分解。網上找的絕大部分是基于一個模板復制出來的,且基于TensorFlow1,因此本人親自動手,用TensorFlow2實現。
- 好奇為什么TensorFlow2不幫我們實現了,在Spark中,直接調用spark.mllib.recommendation.ALS() 就好了。
內容:
在推薦系統中,協同過濾算法是很常用的推薦算法。中心思想:物以類聚,人以群分。也就是口味相同的人,把他喜歡的物品或者電影歌曲推薦給你;或者是將你喜歡的物品,類似的物品推薦給你。
整體流程:
1、 獲取用戶對商品的評分、購買記錄等
2、 構造協同矩陣M
3、 基于矩陣進行分解M=U*V
4、 利用要推薦的物品或者用戶,和U或者V計算相似度
代碼:
TensorFlow2可以自動幫你求導更新參數,太方便了,你要做的就是構造損失函數loss而已。
loss函數可以理解為,我們分解得到U*V得到預測的M_pre,用M和M_pre求歐式距離:即歐幾里得距離(Euclidean Distance)
公式具體為:
大致意思就是分解一個大矩陣為兩個小矩陣相乘。
具體代碼為:
'''================================================= @Function -> 用TensorFlow2實現協同過濾矩陣的分解 @Author :luoji @Date :2021-10-19 =================================================='''import numpy as np import tensorflow as tfdef matrixDecomposition(alike_matix,rank=10,num_epoch= 5000,learning_rate=0.001,reg=0.5):row,column = len(alike_matix),len(alike_matix[0])y_true = tf.constant(alike_matix, dtype=tf.float32) # 構建y_trueU = tf.Variable(shape=(row, rank), initial_value=np.random.random(size=(row, rank)),dtype=tf.float32) # 構建一個變量U,代表user權重矩陣V = tf.Variable(shape=(rank, column), initial_value=np.random.random(size=(rank, column)),dtype=tf.float32) # 構建一個變量,代表權重矩陣,初始化為0variables = [U,V]optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)for batch_index in range(num_epoch):with tf.GradientTape() as tape:y_pre = tf.matmul(U, V)loss = tf.reduce_sum(tf.norm(y_true-y_pre, ord='euclidean')+ reg*(tf.norm(U,ord='euclidean')+tf.norm(V,ord='euclidean'))) #正則化項print("batch %d : loss %f" %(batch_index,loss.numpy()))grads = tape.gradient(loss,variables)optimizer.apply_gradients(grads_and_vars=zip(grads,variables))return U,V,tf.matmul(U, V)if __name__ == "__main__":# 把矩陣分解為 M=U*V ,U和V由用戶指定秩rankalike_matrix = [[1.0, 2.0, 3.0],[4.5, 5.0, 3.1],[1.0, 2.0, 3.0],[4.5, 5.0, 5.1],[1.0, 2.0, 3.0]]U,V,preMatrix = matrixDecomposition(alike_matrix,rank=2,reg=0.5,num_epoch=2000) # reg 減小則num_epoch需增大print(U)print(V)print(alike_matrix)print(preMatrix)print("this difference between alike_matrix and preMatrix is :")print(alike_matrix-preMatrix)print('loss is :',sum(sum(abs(alike_matrix - preMatrix))))待分解的矩陣:
[[1.0, 2.0, 3.0],
[4.5, 5.0, 3.1],
[1.0, 2.0, 3.0],
[4.5, 5.0, 5.1],
[1.0, 2.0, 3.0]]
分解后,相乘的到的矩陣:
[[1.0647349 1.929376 2.9957888]
[4.6015587 4.7999315 3.1697667]
[1.0643657 1.9290545 2.9957101]
[4.287443 5.211667 4.996485 ]
[1.0647217 1.9293401 2.9957187]],
可以看出兩者還是很相似的,證明我們用TensorFlow2進行的矩陣分解是正確的。
注意,正則化項reg需要和num_epoch配套,reg越大,收斂越快,但效果不一定最好。
產出:
TensorFlow2,實現協同過濾算法中的矩陣分解,而且該模塊可以直接復用。
1、加深了對TensorFlow2的理解,太神奇了,只要找到損失函數loss,模型就可以訓練。Amazing!
2、CSDN 技術博客1 篇,全網找不到第二個基于TensorFlow2實現的。好奇為什么TensorFlow2不幫我們實現了,在Spark中,直接調用spark.mllib.recommendation.ALS() 就好了
總結
以上是生活随笔為你收集整理的TensorFlow2实现协同过滤算法中的矩阵分解(首家基于TS2版本)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SparkException: Pyth
- 下一篇: 基于协同过滤算法的在线鲜花店推荐系统详解