Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有个小bug #136

Closed
flyxu opened this issue Feb 2, 2018 · 4 comments
Closed

有个小bug #136

flyxu opened this issue Feb 2, 2018 · 4 comments

Comments

@flyxu
Copy link

flyxu commented Feb 2, 2018

config目录下的SoReg.conf第6行testSet.txt应该写成testset.txt。
另外,SoReg.py里的迭代公式只考虑了Followee� ,但没有考虑Followeer,是不是缺少一项,我看原论文中迭代公式考虑了两种情况。
还有由SoReg衍生的LOCABAL算法,看到你们有一个commit实现了,但后来又删除了,为什么不保留啊?

@Coder-Yu
Copy link
Owner

Coder-Yu commented Feb 3, 2018

你好,因为算法库由多人维护,所以难免出现纰漏,我们会尽快修复bug. LOCABAL当时那一版写的有问题,后来便直接删除了。有时间会不断添加新算法的。感兴趣也可以关注另一个库Yue,里面也包含了不少Item Ranking的方法。

@flyxu
Copy link
Author

flyxu commented Feb 5, 2018

你好,SoReg.py 里的代码除了我说的少了一项外,还有一个问题是第60行for user in self.dao.user:应该删掉。不需要在这儿写一个循环。
此外,我看到您在ratings文件夹下的所有算法的loss计算方式都是一边读数据一边累加loss,然后最后再加上正则项的loss.我认为这样是有问题的,因为你累加的loss是在P,Q更新之间的loss,但最后加的正则项的loss是P,Q更新后的loss,这两个loss能否直接相加?我认为应该在P,Q更新后再累加loss,然后最后加上更新后的正则项loss.我觉的是这样,不知道对不对。

@flyxu
Copy link
Author

flyxu commented Feb 5, 2018

这是重写的SoReg.py。您看看对不对吧

# encoding:utf-8
from baseclass.SocialRecommender import SocialRecommender
from tool import config
from tool import qmath


class SoReg_D(SocialRecommender):
    def __init__(self,conf,trainingSet=None,testSet=None,relation=list(),fold='[1]'):
        super(SoReg_D, self).__init__(conf,trainingSet,testSet,relation,fold)

    def readConfiguration(self):
        super(SoReg_D, self).readConfiguration()
        alpha = config.LineConfig(self.config['SoReg'])
        self.alpha = float(alpha['-alpha'])

    def printAlgorConfig(self):
        super(SoReg_D, self).printAlgorConfig()
        print 'Specified Arguments of',self.config['recommender']+':'
        print 'alpha: %.3f' %self.alpha
        print '='*80

    def initModel(self):
        super(SoReg_D, self).initModel()
        # compute similarity
        from collections import defaultdict
        self.Sim = defaultdict(dict)
        print 'constructing similarity matrix...'

        for user in self.dao.user:
            for f in self.sao.getFollowees(user):
                if self.Sim.has_key(user) and self.Sim[user].has_key(f):
                    pass
                else:
                    self.Sim[user][f]=self.sim(user,f)
                    self.Sim[f][user]=self.Sim[user][f]


    def sim(self,u,v):
        return (qmath.pearson_sp(self.dao.sRow(u), self.dao.sRow(v))+self.sao.weight(u,v))/2.0

    def buildModel(self):
        iteration = 0
        while iteration < self.maxIter:
            self.loss = 0
            for entry in self.dao.trainingData:
                user, item, rating = entry
                uid = self.dao.user[user]
                vid = self.dao.item[item]

                # add the followees' influence

                error = rating - self.P[uid].dot(self.Q[vid])
                p = self.P[uid]
                q = self.Q[vid]

                #self.loss += error**2

                #update latent vectors
                self.P[uid] += self.lRate*(error*q - self.regU * p)
                self.Q[vid] += self.lRate*(error*p - self.regI * q)


                simSumf1 = 0
                #simloss = 0
                uid = self.dao.user[user]
                for f in self.sao.getFollowees(user):
                    if self.dao.containsUser(f):
                        fid = self.dao.user[f]
                        simSumf1 += self.Sim[user][f] * (self.P[uid] - self.P[fid])
                        #simloss += self.Sim[user][f] * ((self.P[uid] - self.P[fid]).dot(self.P[uid] - self.P[fid]))
                        #self.loss += simloss

                #缺失的第二项
                simSumf2 = 0
                for g in self.sao.getFollowers(user):
                    if self.dao.containsUser(g):
                        gid=self.dao.user[g]
                        simSumf2 += self.Sim[g][user] * (self.P[uid]-self.P[gid])

                self.P[uid] += self.lRate * (- self.alpha * (simSumf1+simSumf2))

                #P,Q更新后再计算loss
                e=rating - self.P[uid].dot(self.Q[vid])
                self.loss += e**2
                simloss=0
                uid = self.dao.user[user]
                for f in self.sao.getFollowees(user):
                    if self.dao.containsUser(f):
                        fid = self.dao.user[f]
                        simloss += self.Sim[user][f] * ((self.P[uid] - self.P[fid]).dot(self.P[uid] - self.P[fid]))
                        self.loss += simloss

            self.loss += self.regU*(self.P*self.P).sum() + self.regI*(self.Q*self.Q).sum()
            iteration += 1
            if self.isConverged(iteration):
                break

@Coder-Yu
Copy link
Owner

Coder-Yu commented Feb 5, 2018

非常感谢您的细心,loss的那个地方我觉得都是可行的。至于SoReg, 是一个不太会写代码的同学贡献的,后来我也没有认真检查这个算法,只是大概看了下改了改。谢谢您的更新,如果方便的话,您可以直接在源文件上修改,然后pull request,我会把您加到contributor里面的。这个库整体的代码还有一些问题,后面有时间会整体重构一下的。后来在这个基础上改的音乐推荐的库Yue的代码质量有所提高,有兴趣可以关注。再次感谢

@Coder-Yu Coder-Yu closed this as completed Feb 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants