热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python报错invalid_python运行报错RuntimeWarning:invalidvalueindouble_scalars

我就搞不懂了,为什么运行老是会报类似于RuntimeWarning:overflowencounteredinmultiply的这种错误,这个应该不是代

我就搞不懂了,为什么运行老是会报类似于RuntimeWarning: overflow encountered in multiply的这种错误,这个应该不是代码逻辑的问题吧

部分代码如下:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @File : TrustMF.py

from baseclass.SocialRecommender import SocialRecommender

import numpy as np

from tool import config

class TrustMF(SocialRecommender):

def __init__(self, conf,trainingSet=None,testSet=None,relation=list(),fold='[1]'):

super(TrustMF, self).__init__(conf,trainingSet,testSet,relation,fold)

def initModel(self):

super(TrustMF, self).initModel()

self.Br = np.random.rand(self.dao.trainingSize()[0], self.k) # latent user matrix

self.Wr = np.random.rand(self.dao.trainingSize()[0], self.k) # latent item matrix

self.Vr = np.random.rand(self.dao.trainingSize()[1], self.k) # latent item matrix

self.Be = np.random.rand(self.dao.trainingSize()[0], self.k) # latent user matrix

self.We = np.random.rand(self.dao.trainingSize()[0], self.k) # latent item matrix

self.Ve = np.random.rand(self.dao.trainingSize()[1], self.k) # latent item matrix

def readConfiguration(self):

super(TrustMF, self).readConfiguration()

regular = config.LineConfig(self.config['reg.lambda'])

self.regB = float(regular['-b'])

self.regT = float(regular['-t'])

def printAlgorConfig(self):

super(TrustMF,self).printAlgorConfig()

print 'Regularization parameter: regT %.3f' % self.regT

print '=' * 80

def buildModel(self):

# If necessary, you can fix the parameter in ./config/Trust.conf

iteration = 0

while iteration

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]))

求解,在线等



推荐阅读
author-avatar
zws3504075
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有