Matrix and Tensor Decomposition in Recommender Systems 阅读笔记(翻译)

Matrix and Tensor Decomposition in RecommenderSystems
阅读笔记(翻译)

2.MATRIX DECOMPOSITION

       矩阵分解(matrix factorization)是将一个矩阵分解为多个矩阵相乘结果的过程,它对发掘参与实体(participating entities, 如用户和商品)的数据中的潜在关系有着重要意义。在简化形式中,矩阵分解方法仅用两个矩阵,这两个矩阵各自包含了用户特征因素(user-feature factor)间的相关性(correlation)与商品特征因素(item-featurefactor)间的相关性。

       为预测一个用户对一部电影的评分(rating),我们可以计算电影和用户在图上的坐标[x, y]间的点积(dot product)。

       下面介绍基本矩阵分解方法。第一种方法为特征值分解(Eigenvalue Decomposition)。这种方法将原矩阵分解为矩阵的标准型(canonical form)。第二种方法为非负矩阵分解(Non-Negative MatrixFactorization, NMF)。这种方法将原矩阵分解为两个较小的矩阵,且每个小矩阵的每个元素都非负。第三种方法为概率矩阵分解(Probabilistic MatrixFactorization, PMF)。这种方法适用于大型数据集。PMF方法在用了球形高斯先验(spherical Gaussian priors)的高度稀疏(very sparse)和不均衡(imbalanced)数据集上表现较好。第四种方法为概率潜在语义分析(Probabilistic Latent Semantic Analysis, PLSA)。源于潜在类别模型(latentclass model)的混合分解(mixture decomposition)是该方法的基础。最后一种方法为CUR Decomposition。这种方法confronts the problemof density in the factorized matrices(a problem that is faced on SVD method)。我们还会详细描述SVD和UV分解。我们将用目标函数(objectivefunction)衡量预测值与用户真实评分之间的误差,并将其的最小化。另外,目标函数还被加入了友情的约束来平衡推荐的质量(additional constraint offriendship is added in the objective function to leverage the quality ofrecommendations)。

        最后,我们对比了SVD和UV分解算法,对结合了SVD的CF算法的表现进行了研究。



3.TENSORDECOMPOSITION

        由于在许多情况下,数据为三元关系(ternary relation)(如SocialTagging Systems(STSs), Location-based social network(LBSNs)),许多一开始被设计成针对矩阵的推荐算法无法应用。高阶问题成为新的挑战。比如,STSs的三元关系可以被表示为三阶张量

其中,


        张量分解技术可被应用于发掘张量A中的潜在语义结构。基本的思想是将推荐问题转化成一个三阶张量完整化问题(third-order tensor completion problem)(通过预测A中为被观测到的实体)。

        张量分解的第一种方法是Tucker 分解(TD)。这种方法是HOSVD的潜在张量分解模型(underlyingtensor factorization model)。TD将张量分解为一个矩阵集和一个小的core tensor。第二种方法为PARAFAC方法(PARAllelFACtor analysis),它和TD一样存在约束要求core tensor为对角的(diagonal)。第三种方法是PITF方法(PairwiseInteraction Tensor Factorization),它是TD的特例,在学习和预测上运行时间均为线性。第四种方法为低阶张量分解(Low-order Tensor Decomposition, LOTD)。

        本文主要介绍的分解方法是高阶SVD(High OderSVD, HOSVD),它是SVD的延伸。特别的,我们会用一个小例子一步步来说明HOSVD的实现。然后,我们会说明当一个新的用户在我们的推荐系统注册时该如何更新HOSVD。我们还会讨论HOSVD如何与其他平衡推荐质量的方法相结合。最后,我们会提供在STSs的张量分解在真实数据集上的实验结果。我们还会讨论会用到的metric。我们的目标是说明影响算法效率的主要因素。




原文链接:http://dl.acm.org/citation.cfm?id=2959195

### 关于张量分解的实现 #### 使用NumPy进行张量分解 在Python中,可以利用`NumPy`库来执行基本的矩阵操作并扩展至多维数组(即张量)。一种常见的张量分解方法是CANDECOMP/PARAFAC (CP) 分解。虽然`NumPy`本身不直接提供高级别的张量分解函数,但可以通过组合其基础功能手动实现简单的张量分解算法。 以下是基于`NumPy`的一个简单CP分解示例[^4]: ```python import numpy as np from scipy.optimize import minimize def cp_decomposition(tensor, rank): """ Perform CP Decomposition using alternating least squares. Parameters: tensor: Input tensor (multi-dimensional array). rank: Rank of the decomposition. Returns: Factors matrices A, B, C corresponding to each mode. """ dims = tensor.shape factors = [np.random.rand(dims[i], rank) for i in range(len(dims))] def reconstruction_error(factors_flat): nonlocal factors start_idx = 0 for i in range(len(dims)): end_idx = start_idx + dims[i]*rank factors[i] = factors_flat[start_idx:end_idx].reshape((dims[i], rank)) start_idx = end_idx reconstructed_tensor = np.einsum('ar,br,cr->abc', *factors) error = np.linalg.norm(reconstructed_tensor - tensor) return error initial_guess = np.concatenate([factor.flatten() for factor in factors]) result = minimize(reconstruction_error, initial_guess, method='L-BFGS-B') optimal_factors = result.x start_idx = 0 for i in range(len(dims)): end_idx = start_idx + dims[i]*rank factors[i] = optimal_factors[start_idx:end_idx].reshape((dims[i], rank)) start_idx = end_idx return factors # Example usage tensor = np.random.rand(10, 10, 10) rank = 3 A, B, C = cp_decomposition(tensor, rank) ``` 此代码片段展示了如何通过交替最小二乘法(ALS)实现CP分解,并使用`scipy.optimize.minimize`优化重建误差[^4]。 --- #### 使用TensorFlow进行张量分解 对于更复杂的场景,推荐使用专门设计用于高效计算的框架如`TensorFlow`。该框架内置了一些工具支持张量运算以及自动微分等功能,从而简化了复杂模型构建过程中的工作负担。下面是一个基于`TensorFlow`版本的例子[^5]: ```python import tensorflow as tf def tucker_decomposition(tensor, ranks): """ Perform Tucker Decomposition on an input tensor. Parameters: tensor: Input tensor (multi-dimensional array). ranks: List specifying core tensor dimensions after compression. Returns: Core tensor and projection matrices along all modes. """ init_core = tf.keras.initializers.RandomUniform() core_tensor = tf.Variable(init_core(shape=ranks), trainable=True) projections = [] for dim_size, rank in zip(tf.shape(tensor).numpy(), ranks): proj_matrix = tf.Variable( tf.keras.initializers.GlorotNormal()(shape=(dim_size, rank)), trainable=True ) projections.append(proj_matrix) @tf.function def loss(): approximated_tensor = tf.tensordot(core_tensor, projections[0], axes=([0],[0])) for idx in range(1, len(projections)): approximated_tensor = tf.tensordot(approximated_tensor, projections[idx], axes=([idx], [0])) return tf.reduce_mean((approximated_tensor - tensor)**2) optimizer = tf.optimizers.Adam() for _ in range(100): # Number of iterations can be adjusted based on convergence criteria with tf.GradientTape() as tape: current_loss = loss() grads = tape.gradient(current_loss, [core_tensor] + projections) optimizer.apply_gradients(zip(grads, [core_tensor] + projections)) return core_tensor.numpy(), [p.numpy() for p in projections] # Example Usage input_tensor = tf.random.uniform(shape=[8, 8, 8]) ranks = [3, 3, 3] core, projections = tucker_decomposition(input_tensor, ranks) print("Core Tensor Shape:", core.shape) for i, proj in enumerate(projections): print(f"Projection Matrix {i} Shape:", proj.shape) ``` 上述脚本实现了Tucker分解——另一种重要的张量压缩技术,在这里我们定义了一个损失函数并通过梯度下降调整参数直至收敛[^5]。 --- ### 总结 无论是采用通用科学计算包还是深度学习专用软件栈都可以完成不同类型的张量分析任务;前者适合快速原型开发而后者则更适合大规模分布式训练环境下的应用需求。选择合适的解决方案取决于具体应用场景的要求及其规模特性等因素影响。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值