def updateParameters(self, articlePicked, click, userID): self.counter +=1 self.Wlong = vectorize(self.W) featureDimension = len(articlePicked.featureVector) T_X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.A += np.outer(T_X, T_X) self.b += click*T_X self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.featureVector)) Xi_Matirx = np.zeros(shape = (featureDimension, self.userNum)) Xi_Matirx.T[userID] = articlePicked.featureVector W_X = vectorize( np.dot(np.transpose(self.UserTheta), Xi_Matirx)) self.batchGradient +=evaluateGradient(W_X, click, self.Wlong, self.lambda_, self.regu ) if self.counter%self.windowSize ==0: self.Wlong -= 1/(float(self.counter/self.windowSize)+1)*self.batchGradient self.W = matrixize(self.Wlong, self.userNum) self.W = normalize(self.W, axis=0, norm='l1') #print 'SVD', self.W self.batchGradient = np.zeros(self.userNum*self.userNum) # Use Ridge regression to fit W ''' plt.pcolor(self.W_b) plt.colorbar plt.show() ''' if self.W.T[userID].any() <0 or self.W.T[userID].any()>1: print self.W.T[userID] self.CoTheta = np.dot(self.UserTheta, self.W) self.BigW = np.kron(np.transpose(self.W), np.identity(n=len(articlePicked.featureVector))) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW)) self.BigTheta = np.kron(np.identity(n=self.userNum) , self.UserTheta)
def updateParameters(self, articlePicked, click, userID): featureDimension = len(articlePicked.featureVector) T_X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.T_A += np.outer(T_X, T_X) self.T_b += click*T_X Xi_Matirx = np.zeros(shape = (featureDimension, self.userNum)) Xi_Matirx.T[userID] = articlePicked.featureVector W_X = vectorize( np.dot(np.transpose(self.UserTheta), Xi_Matirx)) #print np.shape(W_X) self.W_A += np.outer(W_X, W_X) self.W_b += click * W_X self.UserTheta = matrixize(np.dot(np.linalg.inv(self.T_A), self.T_b), len(articlePicked.featureVector)) self.W = matrixize(np.dot(np.linalg.inv(self.W_A), self.W_b), self.userNum) #self.W = normalize(self.W, axis=0, norm='l1') #print 'A', self.W_A #print 'b', self.W_b ''' plt.pcolor(self.W_b) plt.colorbar plt.show() ''' self.CoTheta = np.dot(self.UserTheta, self.W) self.BigW = np.kron(np.transpose(self.W), np.identity(n=len(articlePicked.featureVector))) self.CCA = np.dot(np.dot(self.BigW , np.linalg.inv(self.T_A)), np.transpose(self.BigW)) self.BigTheta = np.kron(np.identity(n=self.userNum) , self.UserTheta) self.W_CCA = np.dot(np.dot(self.BigTheta , np.linalg.inv(self.W_A)), np.transpose(self.BigTheta))
def updateParameters(self, articlePicked, click, userID): X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) temp1 = np.outer(articlePicked.featureVector, articlePicked.featureVector) temp2 = np.outer(self.W.T[userID], self.W.T[userID]) self.A += np.kron(temp2, temp1) self.b += click * X self.sA += temp1 self.outerW += temp2 if self.RankoneInverse: temp = np.dot(self.AInv, X) self.AInv = self.AInv - (np.outer( temp, temp)) / (1.0 + np.dot(np.transpose(X), temp)) else: #self.AInv = np.linalg.inv(self.A) self.sAInv = np.linalg.inv(self.sA) temI = 1.0 / self.lambda_ * np.identity(n=self.featureDimension * self.userNum) self.AInv = np.kron(self.WPrimeInv, self.sAInv) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.featureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW, self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID): X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.A += np.outer(X, X) self.b += click*X self.UserTheta = matrixize(np.dot(np.linalg.inv(self.A), self.b), len(articlePicked.featureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , np.linalg.inv(self.A)), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID): self.counter +=1 self.Wlong = vectorize(self.W) featureDimension = len(articlePicked.featureVector) T_X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.A += np.outer(T_X, T_X) self.b += click*T_X self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.featureVector)) Xi_Matirx = np.zeros(shape = (featureDimension, self.userNum)) Xi_Matirx.T[userID] = articlePicked.featureVector W_X = vectorize( np.dot(np.transpose(self.UserTheta), Xi_Matirx)) W_X_current = np.dot(np.transpose(self.UserTheta), articlePicked.featureVector) self.W_X_arr[userID].append(W_X_current) self.W_y_arr[userID].append(click) def fun(w): w = np.asarray(w) res = np.sum((np.dot(self.W_X_arr[userID], w) - self.W_y_arr[userID])**2, axis = 0) + self.lambda_*np.linalg.norm(w) return res def fun(w,X,Y): w = np.asarray(w) res = np.sum((np.dot(X, w) - Y)**2, axis = 0) + self.lambda_*np.linalg.norm(w) return res ''' def fprime(w): w = np.asarray(w) res = self.W_X_arr[userID]*(np.dot(np.transpose(self.W_X_arr[userID]),w) - self.W_y_arr[userID]) + self.lambda_*w return res ''' ''' if self.counter%self.windowSize ==0: current = self.W.T[userID] res = minimize(fun, current, constraints = getcons(len(self.W)), method ='SLSQP', bounds=getbounds(len(self.W)), options={'disp': False}) if res.x.any()>1 or res.x.any <0: print 'error' print res.x self.W.T[userID] = res.x ''' if self.counter%self.windowSize ==0: for i in range(len(self.W)): if len(self.W[i]) !=0: def fun(w): w = np.asarray(w) res = np.sum((np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis = 0) + self.lambda_*np.linalg.norm(w) return res current = self.W.T[i] res = minimize(fun, current, constraints = getcons(len(self.W)), method ='SLSQP', bounds=getbounds(len(self.W)), options={'disp': False}) self.W.T[i] = res.x self.windowSize = self.windowSize*2 self.CoTheta = np.dot(self.UserTheta, self.W) self.BigW = np.kron(np.transpose(self.W), np.identity(n=len(articlePicked.featureVector))) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW)) self.BigTheta = np.kron(np.identity(n=self.userNum) , self.UserTheta)
def updateParameters(self, articlePicked, click, userID): X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.A += np.outer(X, X) self.b += click*X self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.featureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked_FeatureVector, click, userID): X = vectorize(np.outer(articlePicked_FeatureVector, self.W.T[userID])) outer = np.outer(X, X) self.A += outer self.b += click*X self.AInv = self.AInv - float(np.dot(self.AInv, np.dot(outer, self.AInv)))/(1.0+np.dot(np.transpose(X), np.dot(self.AInv, X) )) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked_FeatureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW, self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID): Xi_Matirx = np.zeros(shape=(self.featureDimension, self.userNum)) Xi_Matirx.T[userID] = articlePicked.featureVector X = vectorize(np.dot(np.transpose(self.theta), Xi_Matirx)) #self.W = matrixize(np.dot(np.linalg.inv(self.A), self.b), self.userNum) #fun = lambda x = np.identity(self.userNum): self.f1 + (1/2.0)*(np.dot( np.dot(articlePicked.featureVector, self.theta) ,x.T[userID]) - click)**2 + (self.eta_/2.0)*np.matrix.trace(np.dot(np.transpose(x), x)) def fun(x): x = np.asarray(x) x = x.reshape(self.userNum, self.userNum) return self.f1 + (1 / 2.0) * (np.dot( np.dot(articlePicked.featureVector, self.theta), x.T[userID]) - click)**2 + (self.eta_ / 2.0) * np.matrix.trace( np.dot(np.transpose(x), x)) def fprime(x): x = np.asarray(x) return np.dot(self.A + np.outer(X, X), x) - (self.b + click * X) cons = ( { 'type': 'ineq', 'fun': lambda x: x }, { 'type': 'ineq', 'fun': lambda x: 1 - x }, { 'type': 'eq', 'fun': lambda x: np.sum( np.square(np.sum(x, axis=1) - 1) ) # I'm not sure whether to sum in row or column, but this should do the work. }) ''' res = minimize(fun, self.W, constraints = cons, method ='SLSQP', jac = fprime, options={'disp': False}) self.W = res.x.reshape(self.userNum, self.userNum) #self.W = np.transpose(self.W) ''' #print self.W[0] #Normalization #self.W = normalize(self.W, axis=0, norm='l1') self.A += np.outer(X, X) self.b += click * X self.W = matrixize(np.dot(np.linalg.inv(self.A), self.b), self.userNum) self.f1 += (1 / 2.0) * (np.dot( np.dot(articlePicked.featureVector, self.theta), self.W.T[userID]))**2 self.CoTheta = np.dot(self.theta, self.W) self.CCA = np.dot(np.dot(self.BigTheta, np.linalg.inv(self.A)), np.transpose(self.BigTheta))
def getCoTheta(self, userID): thetaMatrix = matrixize(self.USERS.theta, self.dimension) return thetaMatrix.T[userID] #inherite from CoLinUCB_SelectUserAlgorithm # class GOBLin_SelectUserAlgorithm(CoLinUCB_SelectUserAlgorithm): # def __init__(self, dimension, alpha, lambda_, n, W): # CoLinUCB_SelectUserAlgorithm.__init__(self, dimension, alpha, lambda_, n, W) # self.USERS = GOBLinSharedStruct(dimension, lambda_, n, W) # def getLearntParameters(self, userID): # thetaMatrix = matrixize(self.USERS.theta, self.dimension) # return thetaMatrix.T[userID]
def updateParameters(self, articlePicked, click, userID): X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.A += np.outer(X, X) self.b += click*X if self.RankoneInverse: temp = np.dot(self.AInv, X) self.AInv = self.AInv - (np.outer(temp,temp))/(1.0+np.dot(np.transpose(X),temp)) else: self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.featureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID): featureDimension = len(articlePicked.featureVector) T_X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) self.T_A += np.outer(T_X, T_X) self.T_b += click * T_X Xi_Matirx = np.zeros(shape=(featureDimension, self.userNum)) Xi_Matirx.T[userID] = articlePicked.featureVector W_X = vectorize(np.dot(np.transpose(self.UserTheta), Xi_Matirx)) #print np.shape(W_X) self.W_A += np.outer(W_X, W_X) self.W_b += click * W_X self.UserTheta = matrixize(np.dot(np.linalg.inv(self.T_A), self.T_b), len(articlePicked.featureVector)) self.W = matrixize(np.dot(np.linalg.inv(self.W_A), self.W_b), self.userNum) #self.W = normalize(self.W, axis=0, norm='l1') #print 'A', self.W_A #print 'b', self.W_b ''' plt.pcolor(self.W_b) plt.colorbar plt.show() ''' self.CoTheta = np.dot(self.UserTheta, self.W) self.BigW = np.kron(np.transpose(self.W), np.identity(n=len(articlePicked.featureVector))) self.CCA = np.dot(np.dot(self.BigW, np.linalg.inv(self.T_A)), np.transpose(self.BigW)) self.BigTheta = np.kron(np.identity(n=self.userNum), self.UserTheta) self.W_CCA = np.dot(np.dot(self.BigTheta, np.linalg.inv(self.W_A)), np.transpose(self.BigTheta))
def updateParameters(self, articlePicked, click, userID, update='Inv'): X = vectorize(np.outer(articlePicked.contextFeatureVector, self.W.T[userID])) self.A += np.outer(X, X) self.b += click*X if update == 'Inv': self.AInv = np.linalg.inv(self.A) else: self.AInv = self.AInv - float(np.dot(self.AInv, np.dot(outer, self.AInv)))/(1.0+np.dot(np.transpose(X), np.dot(self.AInv, X) )) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.contextFeatureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID): X = vectorize(np.outer(articlePicked, self.W.T[userID])) self.A += np.outer(X, X) self.b += click * X if self.RankoneInverse: temp = np.dot(self.AInv, X) self.AInv = self.AInv - (np.outer(temp, temp)) / (1.0 + np.dot(np.transpose(X), temp)) else: self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW, self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked_FeatureVector, click, userID): X = vectorize(np.outer(articlePicked_FeatureVector, self.W.T[userID])) # self.A += np.outer(X, X) self.b += click * X self.AInv = self.AInv - np.dot( self.AInv, np.dot(np.outer(X, X), self.AInv)) / float( 1.0 + np.dot(np.dot(np.transpose(X), self.AInv), X)) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked_FeatureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW, self.AInv), np.transpose(self.BigW))
def LateUpdate(self): featureDimension = self.featureVectorMatrix.shape[0] current_A = np.zeros(shape = (featureDimension* self.userNum, featureDimension*self.userNum)) current_b = np.zeros(featureDimension*self.userNum) for i in range(self.userNum): X = vectorize(np.outer(self.featureVectorMatrix.T[i], self.W.T[i])) XS = np.outer(X, X) current_A += XS current_b += self.reward[i] * X self.A += current_A self.b += current_b self.UserTheta = matrixize(np.dot(np.linalg.inv(self.A), self.b), featureDimension) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , np.linalg.inv(self.A)), np.transpose(self.BigW))
def updateParameters(self, articles, clicks, userID): self.time += len(articles) for article, click in zip(articles, clicks): if article.id in self.count[userID]: self.count[userID][article.id] += 1 else: self.count[userID][article.id] = 1 X = vectorize(np.outer(article.V, self.W.T[userID])) self.A += np.outer(X, X) self.b += click*X self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articles[0].V)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def LateUpdate(self): featureDimension = self.contextFeatureVectorMatrix.shape[0] current_A = np.zeros(shape = (featureDimension* self.userNum, featureDimension*self.userNum)) current_b = np.zeros(featureDimension*self.userNum) for i in range(self.userNum): X = vectorize(np.outer(self.contextFeatureVectorMatrix.T[i], self.W.T[i])) XS = np.outer(X, X) current_A += XS current_b += self.reward[i] * X self.A += current_A self.b += current_b self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), featureDimension) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID, update='Inv'): #self.currentW = np.dot(self.currentW, self.W) #X = vectorize(np.outer(articlePicked.contextFeatureVector, self.currentW.T[userID])) X = vectorize(np.outer(articlePicked.contextFeatureVector, self.W.T[userID])) self.A += np.outer(X, X) self.b += click*X if update == 'Inv': self.AInv = np.linalg.inv(self.A) else: self.AInv = self.AInv - float(np.dot(self.AInv, np.dot(outer, self.AInv)))/(1.0+np.dot(np.transpose(X), np.dot(self.AInv, X) )) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.contextFeatureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def updateParameters(self, articlePicked, click, userID): Xi_Matirx = np.zeros(shape = (self.featureDimension, self.userNum)) Xi_Matirx.T[userID] = articlePicked.featureVector X = vectorize( np.dot(np.transpose(self.theta), Xi_Matirx)) #self.W = matrixize(np.dot(np.linalg.inv(self.A), self.b), self.userNum) #fun = lambda x = np.identity(self.userNum): self.f1 + (1/2.0)*(np.dot( np.dot(articlePicked.featureVector, self.theta) ,x.T[userID]) - click)**2 + (self.eta_/2.0)*np.matrix.trace(np.dot(np.transpose(x), x)) def fun(x): x = np.asarray(x) x = x.reshape(self.userNum, self.userNum) return self.f1 + (1/2.0)*(np.dot( np.dot(articlePicked.featureVector, self.theta) ,x.T[userID]) - click)**2 + (self.eta_/2.0)*np.matrix.trace(np.dot(np.transpose(x), x)) def fprime(x): x = np.asarray(x) return np.dot(self.A+ np.outer(X, X), x) -(self.b + click*X) cons = ({'type' : 'ineq', 'fun' : lambda x: x }, {'type' : 'ineq', 'fun' : lambda x: 1-x}, {'type': 'eq', 'fun': lambda x : np.sum(np.square(np.sum(x, axis = 1)-1)) # I'm not sure whether to sum in row or column, but this should do the work. } ) ''' res = minimize(fun, self.W, constraints = cons, method ='SLSQP', jac = fprime, options={'disp': False}) self.W = res.x.reshape(self.userNum, self.userNum) #self.W = np.transpose(self.W) ''' #print self.W[0] #Normalization #self.W = normalize(self.W, axis=0, norm='l1') self.A += np.outer(X, X) self.b += click * X self.W = matrixize(np.dot(np.linalg.inv(self.A), self.b), self.userNum) self.f1 +=(1/2.0)*(np.dot( np.dot(articlePicked.featureVector, self.theta) ,self.W.T[userID]))**2 self.CoTheta = np.dot(self.theta, self.W) self.CCA = np.dot(np.dot(self.BigTheta, np.linalg.inv(self.A)), np.transpose(self.BigTheta))
def updateParameters(self, articlePicked, click, userID): X = vectorize(np.outer(articlePicked.featureVector, self.W.T[userID])) temp1 = np.outer(articlePicked.featureVector, articlePicked.featureVector) temp2 = np.outer(self.W.T[userID], self.W.T[userID]) self.A += np.kron(temp2, temp1) self.b += click*X self.sA += temp1 self.outerW += temp2 if self.RankoneInverse: temp = np.dot(self.AInv, X) self.AInv = self.AInv - (np.outer(temp,temp))/(1.0+np.dot(np.transpose(X),temp)) else: #self.AInv = np.linalg.inv(self.A) self.sAInv = np.linalg.inv(self.sA) temI = 1.0/self.lambda_*np.identity(n = self.featureDimension*self.userNum) self.AInv = np.kron(self.WPrimeInv, self.sAInv) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(articlePicked.featureVector)) self.CoTheta = np.dot(self.UserTheta, self.W) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW))
def updateParameters(self, featureVector, click, userID): self.counter += 1 self.Wlong = vectorize(self.W) featureDimension = len(featureVector) T_X = vectorize(np.outer(featureVector, self.W.T[userID])) self.A += np.outer(T_X, T_X) self.b += click * T_X if self.RankoneInverse: temp = np.dot(self.AInv, T_X) self.AInv = self.AInv - (np.outer( temp, temp)) / (1.0 + np.dot(np.transpose(T_X), temp)) else: self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(featureVector)) Xi_Matirx = np.zeros(shape=(featureDimension, self.userNum)) Xi_Matirx.T[userID] = featureVector W_X = vectorize(np.dot(np.transpose(self.UserTheta), Xi_Matirx)) W_X_current = np.dot(np.transpose(self.UserTheta), featureVector) self.W_X_arr[userID].append(W_X_current) self.W_y_arr[userID].append(click) # print self.windowSize if self.counter % self.windowSize == 0: for i in range(len(self.W)): if len(self.W_X_arr[i]) != 0: def fun(w): w = np.asarray(w) return np.sum( (np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis=0) + self.lambda_ * np.linalg.norm(w)**2 def fun_l1(w): w = np.asarray(w) return np.sum( (np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis=0) + self.lambda_ * np.linalg.norm(w, 1)**2 def evaluateGradient(w): w = np.asarray(w) X = np.asarray(self.W_X_arr[i]) y = np.asarray(self.W_y_arr[i]) grad = np.dot(np.transpose(X), (np.dot(X, w) - y)) + self.lambda_ * w return 2 * grad def fun_WRegu(w): w = np.asarray(w) return np.sum( (np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis=0 ) + self.lambda_ * np.linalg.norm(w - self.W.T[i])**2 def evaluateGradient_WRegu(w): w = np.asarray(w) X = np.asarray(self.W_X_arr[i]) y = np.asarray(self.W_y_arr[i]) grad = np.dot(np.transpose(X), (np.dot(X, w) - y)) + self.lambda_ * (w - self.W.T[i]) return 2 * grad current = self.W.T[i] if self.L1Regu: res = minimize(fun_l1, current, constraints=getcons(len(self.W)), method='SLSQP', bounds=getbounds(len(self.W)), options={'disp': False}) elif self.WRegu: res = minimize(fun_WRegu, current, constraints=getcons(len(self.W)), method='SLSQP', jac=evaluateGradient_WRegu, bounds=getbounds(len(self.W)), options={'disp': False}) else: res = minimize(fun, current, constraints=getcons(len(self.W)), method='SLSQP', jac=evaluateGradient, bounds=getbounds(len(self.W)), options={'disp': False}) self.W.T[i] = res.x if self.windowSize < 2000: self.windowSize = self.windowSize * 2 self.CoTheta = np.dot(self.UserTheta, self.W) self.BigW = np.kron(np.transpose(self.W), np.identity(n=len(featureVector))) self.CCA = np.dot(np.dot(self.BigW, self.AInv), np.transpose(self.BigW)) self.BigTheta = np.kron(np.identity(n=self.userNum), self.UserTheta)
def updateParameters(self, featureVector, click, userID): self.counter +=1 self.Wlong = vectorize(self.W) featureDimension = len(featureVector) T_X = vectorize(np.outer(featureVector, self.W.T[userID])) self.A += np.outer(T_X, T_X) self.b += click*T_X if self.RankoneInverse: temp = np.dot(self.AInv, T_X) self.AInv = self.AInv - (np.outer(temp,temp))/(1.0+np.dot(np.transpose(T_X),temp)) else: self.AInv = np.linalg.inv(self.A) self.UserTheta = matrixize(np.dot(self.AInv, self.b), len(featureVector)) Xi_Matirx = np.zeros(shape = (featureDimension, self.userNum)) Xi_Matirx.T[userID] = featureVector W_X = vectorize( np.dot(np.transpose(self.UserTheta), Xi_Matirx)) W_X_current = np.dot(np.transpose(self.UserTheta), featureVector) self.W_X_arr[userID].append(W_X_current) self.W_y_arr[userID].append(click) #print self.windowSize if self.counter%self.windowSize ==0: for i in range(len(self.W)): if len(self.W_X_arr[i]) !=0: def fun(w): w = np.asarray(w) return np.sum((np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis = 0) + self.lambda_*np.linalg.norm(w)**2 def fun_l1(w): w = np.asarray(w) return np.sum((np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis = 0) + self.lambda_*np.linalg.norm(w,1)**2 def evaluateGradient(w): w = np.asarray(w) X = np.asarray(self.W_X_arr[i]) y = np.asarray(self.W_y_arr[i]) grad = np.dot(np.transpose(X) , ( np.dot(X,w)- y)) + self.lambda_ * w return 2*grad def fun_WRegu(w): w = np.asarray(w) return np.sum((np.dot(self.W_X_arr[i], w) - self.W_y_arr[i])**2, axis = 0) + self.lambda_*np.linalg.norm(w - self.W.T[i])**2 def evaluateGradient_WRegu(w): w = np.asarray(w) X = np.asarray(self.W_X_arr[i]) y = np.asarray(self.W_y_arr[i]) grad = np.dot(np.transpose(X) , ( np.dot(X,w)- y)) + self.lambda_ * (w - self.W.T[i]) return 2*grad current = self.W.T[i] if self.L1Regu: res = minimize(fun_l1, current, constraints = getcons(len(self.W)), method ='SLSQP', bounds=getbounds(len(self.W)), options={'disp': False}) elif self.WRegu: res = minimize(fun_WRegu, current, constraints = getcons(len(self.W)), method ='SLSQP', jac = evaluateGradient_WRegu, bounds=getbounds(len(self.W)), options={'disp': False}) else: res = minimize(fun, current, constraints = getcons(len(self.W)), method ='SLSQP', jac = evaluateGradient, bounds=getbounds(len(self.W)), options={'disp': False}) self.W.T[i] = res.x if self.windowSize<2000: self.windowSize = self.windowSize*2 self.CoTheta = np.dot(self.UserTheta, self.W) self.BigW = np.kron(np.transpose(self.W), np.identity(n=len(featureVector))) self.CCA = np.dot(np.dot(self.BigW , self.AInv), np.transpose(self.BigW)) self.BigTheta = np.kron(np.identity(n=self.userNum) , self.UserTheta)
def getCoTheta(self, userID): thetaMatrix = matrixize(self.USERS.theta, self.dimension) return thetaMatrix.T[userID]
def getLearntParameters(self, userID): thetaMatrix = matrixize(self.USERS.theta, self.dimension) return thetaMatrix.T[userID]