self.loss = 0
self.trusterModel()
self.trusteeModel()
iteration += 1
self.isConverged(iteration)
def trusterModel(self):
for entry in self.dao.trainingData:
u, i, r = entry
mbu = len(self.sao.getFollowees(u))
uid = self.dao.getUserId(u)
iid = self.dao.getItemId(i)
error = self.truserPredict(u, i) - r
nbu = len(self.dao.userRated(u)[0])
nvi = len(self.dao.itemRated(i)[0])
self.loss += error**2 + self.regB * ((mbu + nbu) * self.Br[uid].dot(self.Br[uid]) + nvi * self.Vr[iid].dot(self.Vr[iid]))
self.Vr[iid] = self.Vr[iid] - self.lRate * (error * self.Br[uid] + self.regB * nvi * self.Vr[iid])
relations = self.sao.getFollowees(u)
if len(relations)!=0:
for followee in relations.iterkeys():
weight = relations[followee]
uf = self.dao.getUserId(followee)
if uf != -1 and self.dao.containsUser(followee): # followee is in rating set
error1 = self.Br[uid].dot(self.Wr[uf]) - weight
mwk = len(self.sao.getFollowers(followee))
self.loss += self.regT * error1**2 + self.regB * mwk * self.Wr[uf].dot(self.Wr[uf])
self.Br[uid] = self.Br[uid] - self.lRate * (error * self.Vr[iid] + self.regB * (mbu + nbu) * self.Br[uid] + self.regT * (self.Br[uid].dot(self.Wr[uf]) - weight) * self.Wr[uf])
self.Wr[uf] = self.Wr[uf] - self.lRate * (self.regT * error1 * self.Br[uid] + self.regB * mwk * self.Wr[uf])
def trusteeModel(self):
for entry in self.dao.trainingData:
u, i, r = entry
mwu = len(self.sao.getFollowers(u))
uid = self.dao.getUserId(u)
iid = self.dao.getItemId(i)
error = self.truseePredict(u, i) - r
nwu = len(self.dao.userRated(u)[0])
nvi = len(self.dao.itemRated(i)[0])
self.loss += error**2 + self.regB * ((mwu + nwu) * self.We[uid].dot(self.We[uid]) + nvi * self.Ve[iid].dot(self.Ve[iid]))
self.Ve[iid] = self.Ve[iid] - self.lRate * (error * self.We[uid] + self.regB * nvi * self.Ve[iid])
relations = self.sao.getFollowers(u)
if len(relations) != 0:
for follower in relations.iterkeys():
weight = relations[follower]
uf = self.dao.getUserId(follower)
if uf != -1 and self.dao.containsUser(follower): # follower is in rating set
error1 = self.Be[uf].dot(self.We[uid]) - weight
mbk = len(self.sao.getFollowees(follower))
self.loss += self.regT * error1**2 + self.regB * mbk * self.Be[uf].dot(self.Be[uf])
self.We[uid] = self.We[uid] - self.lRate * (error * self.Vr[iid] + self.regB * (mwu + nwu) * self.We[uid] + self.regT * error1 * self.Be[uf])
self.Be[uf] = self.Be[uf] - self.lRate * (self.regT * error1 * self.We[uid] + self.regB * mbk * self.Be[uf])
def truserPredict(self, u, i):
if self.dao.containsUser(u) and self.dao.containsItem(i):
u = self.dao.getUserId(u)
i = self.dao.getItemId(i)
return self.Br[u].dot(self.Vr[i])
else:
return self.dao.globalMean
def truseePredict(self, u, i):
if self.dao.containsUser(u) and self.dao.containsItem(i):
u = self.dao.getUserId(u)
i = self.dao.getItemId(i)
return self.We[u].dot(self.Ve[i])
else:
return self.dao.globalMean
def predict(self, u, i):
if self.dao.containsUser(u) and self.dao.containsItem(i):
u = self.dao.getUserId(u)
i = self.dao.getItemId(i)
return (self.Br[u] + self.We[u]).dot(self.Vr[i] + self.Ve[i]) * 0.25
else:
return self.dao.globalMean
我就写了个trustMF方法,用到梯度下降法,就给我报一大堆runtimeerror,我真的不知道到底哪里出错了
报错如下:
C:\Users\123\Documents\GitHub\RecQ\algorithm\rating\TrustMF.py:65: RuntimeWarning: overflow encountered in multiply
self.Br[uid] = self.Br[uid] - self.lRate * (error * self.Vr[iid] + self.regB * (mbu + nbu) * self.Br[uid] + self.regT * (self.Br[uid].dot(self.Wr[uf]) - weight) * self.Wr[uf])
C:\Users\123\Documents\GitHub\RecQ\algorithm\rating\TrustMF.py:65: RuntimeWarning: invalid value encountered in add
self.Br[uid] = self.Br[uid] - self.lRate * (error * self.Vr[iid] + self.regB * (mbu + nbu) * self.Br[uid] + self.regT * (self.Br[uid].dot(self.Wr[uf]) - weight) * self.Wr[uf])
C:\Users\123\Documents\GitHub\RecQ\algorithm\rating\TrustMF.py:64: RuntimeWarning: invalid value encountered in double_scalars
self.loss += self.regT * error1**2 + self.regB * mwk * self.Wr[uf].dot(self.Wr[uf])
C:\Users\123\Documents\GitHub\RecQ\algorithm\rating\TrustMF.py:54: RuntimeWarning: invalid value encountered in double_scalars
self.loss += error**2 + self.regB * ((mbu + nbu) * self.Br[uid].dot(self.Br[uid]) + nvi * self.Vr[iid].dot(self.Vr[iid]))
求解,在线等