近期在看CF的相关论文,《Collaborative Filtering for Implicit Feedback Datasets》思想非常好,非常easy理解。可是从目标函数



是怎样推导出Xu和Yi的更新公式的推导过程却没有非常好的描写叙述。所以以下写一下

推导:

首先对Xu求导:



当中Y是item矩阵,n*f维,每一行是一个item_vec,C^u是n*n维的对角矩阵。

对角线上的每个元素是c_ui,P(u)是n*1的列向量,它的第i个元素为p_ui。

然后令导数=0,可得:



因为x_u和y_i在目标函数中是对称的。所以非常easy得到:



当中X是user矩阵,m*f维度,每一行是一个user_vec,C^i是m*m的对角矩阵。对角线上的每个元素是c_ui。P(i)是m*1的列向量。它的第u和元素是p_ui

然后令导数=0,可得:



以下是论文算法思想的Python实现:

import numpy as np
import scipy.sparse as sparse
from scipy.sparse.linalg import spsolve
import time def load_matrix(filename, num_users, num_items):
t0 = time.time()
counts = np.zeros((num_users, num_items))
total = 0.0
num_zeros = num_users * num_items
'''假设要对一个列表或者数组既要遍历索引又要遍历元素时。能够用enumerate,当传入參数为文件时,索引为
行号,元素相应的一行内容'''
for i, line in enumerate(open(filename, 'r')):
#strip()去除最前面和最后面的空格
user, item, count = line.strip().split('\t')
user = int(user)
item = int(item)
count = float(count)
if user >= num_users:
continue
if item >= num_items:
continue
if count != 0:
counts[user, item] = count
total += count
num_zeros -= 1
if i % 100000 == 0:
print 'loaded %i counts...' % i
#数据导入完成后计算稀疏矩阵中零元素个数和非零元素个数的比例,记为alpha
alpha = num_zeros / total
print 'alpha %.2f' % alpha
counts *= alpha
#用CompressedSparse Row Format将稀疏矩阵压缩
counts = sparse.csr_matrix(counts)
t1 = time.time()
print 'Finished loading matrix in %f seconds' % (t1 - t0)
return counts class ImplicitMF(): def __init__(self, counts, num_factors=40, num_iterations=30,
reg_param=0.8):
self.counts = counts
self.num_users = counts.shape[0]
self.num_items = counts.shape[1]
self.num_factors = num_factors
self.num_iterations = num_iterations
self.reg_param = reg_param def train_model(self):
#创建user_vectors和item_vectors,他们的元素~N(0,1)的正态分布
self.user_vectors = np.random.normal(size=(self.num_users,
self.num_factors))
self.item_vectors = np.random.normal(size=(self.num_items,
self.num_factors))
'''要生成非常大的数字序列的时候,用xrange会比range性能优非常多,
因为不须要一上来就开辟一块非常大的内存空间,这两个基本上都是在循环的时候用'''
for i in xrange(self.num_iterations):
t0 = time.time()
print 'Solving for user vectors...'
self.user_vectors = self.iteration(True, sparse.csr_matrix(self.item_vectors))
print 'Solving for item vectors...'
self.item_vectors = self.iteration(False, sparse.csr_matrix(self.user_vectors))
t1 = time.time()
print 'iteration %i finished in %f seconds' % (i + 1, t1 - t0) def iteration(self, user, fixed_vecs):
#相当于C的三木运算符。if user=True num_solve = num_users,反之为num_items
num_solve = self.num_users if user else self.num_items
num_fixed = fixed_vecs.shape[0]
YTY = fixed_vecs.T.dot(fixed_vecs)
eye = sparse.eye(num_fixed)
lambda_eye = self.reg_param * sparse.eye(self.num_factors)
solve_vecs = np.zeros((num_solve, self.num_factors))
t = time.time()
for i in xrange(num_solve):
if user:
counts_i = self.counts[i].toarray()
else:
#假设要求item_vec,counts_i为counts中的第i列的转置
counts_i = self.counts[:, i].T.toarray()
''' 原论文中c_ui=1+alpha*r_ui,可是在计算Y’CuY时为了减少时间复杂度,利用了
Y'CuY=Y'Y+Y'(Cu-I)Y,因为Cu是对角矩阵,其元素为c_ui,即1+alpha*r_ui。
所以Cu-I也就是对角元素为alpha*r_ui的对角矩阵'''
CuI = sparse.diags(counts_i, [0])
pu = counts_i.copy()
#np.where(pu != 0)返回pu中元素不为0的索引,然后将这些元素赋值为1,不知道这里为什么要赋值为1?
pu[np.where(pu != 0)] = 1.0
YTCuIY = fixed_vecs.T.dot(CuI).dot(fixed_vecs)
YTCupu = fixed_vecs.T.dot(CuI + eye).dot(sparse.csr_matrix(pu).T)
xu = spsolve(YTY + YTCuIY + lambda_eye, YTCupu)
solve_vecs[i] = xu
if i % 1000 == 0:
print 'Solved %i vecs in %d seconds' % (i, time.time() - t)
t = time.time()
return solve_vecs

Alternating Least Squares(ASL) for Implicit Feedback Datasets的数学推导以及用Python实现的更多相关文章

  1. 【论文笔记】 Denoising Implicit Feedback for Recommendation

    Denoising Implicit Feedback for Recommendation Authors: 王文杰,冯福利,何向南,聂礼强,蔡达成 WSDM'21 新加坡国立大学,中国科学技术大学 ...

  2. 【RS】Using graded implicit feedback for bayesian personalized ranking - 使用分级隐式反馈来进行贝叶斯个性化排序

    [论文标题]Using graded implicit feedback for bayesian personalized ranking (RecSys '14  recsys.ACM ) [论文 ...

  3. 【RS】BPR:Bayesian Personalized Ranking from Implicit Feedback - BPR:利用隐反馈的贝叶斯个性化排序

    [论文标题]BPR:Bayesian Personalized Ranking from Implicit Feedback (2012,Published by ACM Press) [论文作者]S ...

  4. spark MLlib 概念 6:ALS(Alternating Least Squares) or (ALS-WR)

    Large-scale Parallel Collaborative Filtering for the Netflix Prize http://www.hpl.hp.com/personal/Ro ...

  5. 【论文笔记】Leveraging Post-click Feedback for Content Recommendations

    Leveraging Post-click Feedback for Content Recommendations Authors: Hongyi Wen, Longqi Yang, Deborah ...

  6. 阿基米德项目ALS矩阵分解算法应用案例

    转自:https://github.com/ceys/jdml/wiki/ALS 阿基米德项目ALS矩阵分解算法应用案例 编写人:ceys/youyis 最后更新时间:2014.5.12 一.算法描述 ...

  7. MLlib-协同过滤

    协同过滤 显示vs隐式反馈 参数调整 实例 教程 协同过滤 协同过滤是推荐系统的常用方法.可以填充user-item相关矩阵中的缺失值.MLlib支持基于模型的协同过滤,即使用能够预测缺失值的一个隐藏 ...

  8. 共轭梯度法求解协同过滤中的 ALS

    协同过滤是一类基于用户行为数据的推荐方法,主要是利用已有用户群体过去的行为或意见来预测当前用户的偏好,进而为其产生推荐.能用于协同过滤的算法很多,大致可分为:基于最近邻推荐和基于模型的推荐.其中基于最 ...

  9. 推荐系统-协同过滤在Spark中的实现

    作者:vivo 互联网服务器团队-Tang Shutao 现如今推荐无处不在,例如抖音.淘宝.京东App均能见到推荐系统的身影,其背后涉及许多的技术.本文以经典的协同过滤为切入点,重点介绍了被工业界广 ...

随机推荐

  1. *****git pull总结

    当git clone之后,直接git pull它会自动匹配一个正确的remote url 是因为在config文件中配置了以下内容: 1 [branch "master"] 2 r ...

  2. python os用法精简版

    import os print(os.getcwd()) #返回当前路径,无参数 print(os.listdir('E:\zsfile')) #该路径下所有文件名 os.remove('E:\zsf ...

  3. python 根据文件创建时间排序

    #coding:utf8 import os,time directory = "d:/scrapy tutorial/" t = [] d = {} for filename i ...

  4. Qimage与IplImage的转换

    QImage test2012::ImageCV2Qimg(IplImage* img){ assert(img!=NULL); int h = img->height; int w = img ...

  5. Codeforces 691C. Exponential notation

    题目链接:http://codeforces.com/problemset/problem/691/C 题意: 给你一个浮点数,让你把这个数转化为 aEb 的形式,含义为 a * 10b, 其中 a ...

  6. hdu6071(最短路)

    hdu6071 题意 四个点连接形成一个环,给出相邻两个点的距离,求从点 \(2\) 出发再回到 \(2\) 的路程大于等于 \(K\) 的最小值. 分析 首先我们让 \(w=min(d12, d23 ...

  7. 正则 lazy

  8. Xamarin XAML语言教程基本页面ContentPage占用面积(二)

    Xamarin XAML语言教程基本页面ContentPage占用面积(二) Xamarin XAML语言教程基本页面ContentPage占用面积(二)内容页面的添加 为了方便用户添加Content ...

  9. wildfly8.1部署注意事项

    wildfly8.1部署注意事项 jboss  最近新项目上线,本人部署过程中总结了以下几点比较关键的地方,看是否对大家有用处     服务器改成支持外网访问 在standalone.xml文件中找到 ...

  10. HTTP状态代码集

    所有 HTTP 状态代码及其定义.   代码  指示     2xx  成功     200  正常:请求已完成.     201  正常:紧接 POST 命令.     202  正常:已接受用于处 ...