def solveForModeB(X, M, n, maxInner, epsilon, tol): """ Solve for the subproblem B = argmin (B >= 0) f(M) Parameters ---------- X : the original tensor M : the current CP factorization n : the mode that we are trying to solve the subproblem for epsilon : parameter to avoid dividing by zero tol : the convergence tolerance (to Returns ------- M : the updated CP factorization Phi : the last Phi(n) value iter : the number of iterations before convergence / maximum kktModeViolation : the maximum value of min(B(n), E - Phi(n)) """ # Pi(n) = [A(N) kr A(N-1) kr ... A(n+1) kr A(n-1) kr .. A(1)]^T Pi = tensorTools.calculatePi(X, M, n) #print(M.U[n]) for iter in range(maxInner): # Phi = (X(n) elem-div (B Pi)) Pi^T Phi = tensorTools.calculatePhi(X, M.U[n], Pi, n, epsilon=epsilon) #print(Phi) # check for convergence that min(B(n), E - Phi(n)) = 0 [or close] kktModeViolation = np.max(np.abs( np.minimum(M.U[n], 1 - Phi).flatten())) if (kktModeViolation < tol): break # Do the multiplicative update M.U[n] = np.multiply(M.U[n], Phi) #print(" Mode={0}, Inner Iter={1}, KKT violation={2}".format(n, iter, kktModeViolation)) return M, Phi, iter, kktModeViolation
def solveForModeB(X, M, n, maxInner, epsilon, tol): """ Solve for the subproblem B = argmin (B >= 0) f(M) Parameters ---------- X : the original tensor M : the current CP factorization n : the mode that we are trying to solve the subproblem for epsilon : parameter to avoid dividing by zero tol : the convergence tolerance (to Returns ------- M : the updated CP factorization Phi : the last Phi(n) value iter : the number of iterations before convergence / maximum kktModeViolation : the maximum value of min(B(n), E - Phi(n)) """ # Pi(n) = [A(N) kr A(N-1) kr ... A(n+1) kr A(n-1) kr .. A(1)]^T Pi = tensorTools.calculatePi(X, M, n) for iter in range(maxInner): # Phi = (X(n) elem-div (B Pi)) Pi^T Phi = tensorTools.calculatePhi(X, M.U[n], Pi, n, epsilon=epsilon) # check for convergence that min(B(n), E - Phi(n)) = 0 [or close] kktModeViolation = np.max(np.abs(np.minimum(M.U[n], 1-Phi).flatten())) if (kktModeViolation < tol): break # Do the multiplicative update M.U[n] = np.multiply(M.U[n],Phi) #print(" Mode={0}, Inner Iter={1}, KKT violation={2}".format(n, iter, kktModeViolation)) return M, Phi, iter, kktModeViolation
def solveForModeB1(X, M, n, maxInner, epsilon, tol, sita, Y1, lambta2): """ Solve for the subproblem B = argmin (B >= 0) f(M) Parameters ---------- X : the original tensor M : the current CP factorization n : the mode that we are trying to solve the subproblem for epsilon : parameter to avoid dividing by zero tol : the convergence tolerance (to Returns ------- M : the updated CP factorization Phi : the last Phi(n) value iter : the number of iterations before convergence / maximum kktModeViolation : the maximum value of min(B(n), E - Phi(n)) DemoU : the updated demographic U matrix """ # Pi(n) = [A(N) kr A(N-1) kr ... A(n+1) kr A(n-1) kr .. A(1)]^T Pi = tensorTools.calculatePi(X, M, n) #print 'Pi size', Pi.shape #print 'pi='+str(Pi) #print(M.U[n]) for iter in range(maxInner): # Phi = (X(n) elem-div (B Pi)) Pi^T #print X.vals.shape,X.shape #print X.vals.flatten().shape Phi = tensorTools.calculatePhi(X, M.U[n], Pi, n, epsilon=epsilon) #print('phi'+str(Phi)) #print(Phi) # check for convergence that min(B(n), E - Phi(n)) = 0 [or close] kktModeViolation = np.max(np.abs( np.minimum(M.U[n], 1 - Phi).flatten())) if (kktModeViolation < tol): break B = M.U[n] #print B.shape colNorm = np.apply_along_axis(np.linalg.norm, 0, B, 1) zeroNorm = np.where(colNorm == 0)[0] colNorm[zeroNorm] = 1 B = B / colNorm[np.newaxis, :] tm = np.hstack((np.ones((B.shape[0], 1)), B)) Y1 = Y1.reshape((Y1.shape[0], 1)) derive = -1.0 * lambta2 / B.shape[0] * np.dot( (Y1 - np.dot(tm, sita)), sita.T) #print derive.shape #print np.multiply(M.U[n],derive[:,1:]).shape #print np.multiply(M.U[n],Phi).shape M.U[n] = np.array(np.multiply(M.U[n], Phi)) - np.array( (np.multiply(M.U[n], derive[:, 1:]))) #print 'after' #print M.U[n][0] #print(" Mode={0}, Inner Iter={1}, KKT violation={2}".format(n, iter, kktModeViolation)) return M, Phi, iter, kktModeViolation
def solveForModeB1(X, M, n, maxInner, epsilon, tol, Demog, DemoU, lambta1, lambta4): """ Solve for the subproblem B = argmin (B >= 0) f(M) Parameters ---------- X : the original tensor M : the current CP factorization n : the mode that we are trying to solve the subproblem for epsilon : parameter to avoid dividing by zero tol : the convergence tolerance (to Demog : the demographic matrix shape : number of persons * fertures DemoU : the estimated matrix decomposited from Demog matrix ,another matrix is X's factoe matrix U[0] lambta1 is the parameter of docomposition of demographic information lambta4 is the patameter of penalty item of demoU Returns ------- M : the updated CP factorization Phi : the last Phi(n) value iter : the number of iterations before convergence / maximum kktModeViolation : the maximum value of min(B(n), E - Phi(n)) DemoU : the updated demographic U matrix """ # Pi(n) = [A(N) kr A(N-1) kr ... A(n+1) kr A(n-1) kr .. A(1)]^T Pi = tensorTools.calculatePi(X, M, n) #print 'pi='+str(Pi) #print(M.U[n]) for iter in range(maxInner): # Phi = (X(n) elem-div (B Pi)) Pi^T Phi = tensorTools.calculatePhi(X, M.U[n], Pi, n, epsilon=epsilon) #print('phi'+str(Phi)) #print(Phi) # check for convergence that min(B(n), E - Phi(n)) = 0 [or close] kktModeViolation = np.max(np.abs( np.minimum(M.U[n], 1 - Phi).flatten())) if (kktModeViolation < tol): break llambta, tempB = __normalize_mode(M, n, 1) #DemoU=__solveDemoU(M.U[n],100,epsilon,Demog,DemoU,lambta1) #print 'demoU' #print DemoU[0] #M.U[n] = np.multiply(M.U[n],Phi)-(lambta1*np.multiply(M.U[n]/(np.sum(Pi,axis=0)),np.dot(np.dot(M.U[n],DemoU)-Demog,DemoU.T))) # #M.U[n] = np.multiply(M.U[n],Phi) M.U[n] = np.multiply(M.U[n], Phi) - (lambta1 * np.multiply( M.U[n], np.dot(np.dot(tempB, DemoU) - Demog, DemoU.T))) #print 'after' #print M.U[n][0] #print(" Mode={0}, Inner Iter={1}, KKT violation={2}".format(n, iter, kktModeViolation)) return M, Phi, iter, kktModeViolation
def solveForModeB1(X, M, n, maxInner, epsilon, tol,sita,Y1, lambta2): """ Solve for the subproblem B = argmin (B >= 0) f(M) Parameters ---------- X : the original tensor M : the current CP factorization n : the mode that we are trying to solve the subproblem for epsilon : parameter to avoid dividing by zero tol : the convergence tolerance (to Returns ------- M : the updated CP factorization Phi : the last Phi(n) value iter : the number of iterations before convergence / maximum kktModeViolation : the maximum value of min(B(n), E - Phi(n)) DemoU : the updated demographic U matrix """ # Pi(n) = [A(N) kr A(N-1) kr ... A(n+1) kr A(n-1) kr .. A(1)]^T Pi = tensorTools.calculatePi(X, M, n) #print 'pi='+str(Pi) #print(M.U[n]) for iter in range(maxInner): # Phi = (X(n) elem-div (B Pi)) Pi^T Phi = tensorTools.calculatePhi(X, M.U[n], Pi, n, epsilon=epsilon) #print('phi'+str(Phi)) #print(Phi) # check for convergence that min(B(n), E - Phi(n)) = 0 [or close] kktModeViolation = np.max(np.abs(np.minimum(M.U[n], 1-Phi).flatten())) if (kktModeViolation < tol): break #DemoU=__solveDemoU(M.U[n],100,epsilon,Demog,DemoU,lambta1) #print 'demoU' #print DemoU[0] #M.U[n] = np.multiply(M.U[n],Phi)-(lambta1*np.multiply(M.U[n]/(np.sum(Pi,axis=0)),np.dot(np.dot(M.U[n],DemoU)-Demog,DemoU.T))) # #M.U[n] = np.multiply(M.U[n],Phi) B=M.U[n] colNorm = np.apply_along_axis(np.linalg.norm, 0, B, 1) zeroNorm = np.where(colNorm == 0)[0] colNorm[zeroNorm] = 1 B = B / colNorm[np.newaxis, :] tm=np.hstack((np.ones((B.shape[0],1)),B)) Y1=Y1.reshape((Y1.shape[0],1)) derive=-lambta2*np.dot((Y1/(np.exp(np.multiply(np.dot(tm,sita),Y1))+np.ones((Y1.shape[0],1)))),sita.T)/tm.shape[0] M.U[n] = np.multiply(M.U[n],Phi)-(np.multiply(M.U[n],derive[:,1:])) return M, Phi, iter, kktModeViolation
def solveSubproblem(self, C, aug, n): """ Solve the subproblem for mode n Parameters ------------ C : the "other" tensor, either bias or signal of the tensor decomposition aug : the location to solve (either augmented or not) n : the mode to solve """ ## shift the weight from lambda to mode n self.M[aug].redistribute(n) Pi = tensorTools.calculatePi(self.X, self.M[aug], n) B, inI, kktModeViolation = self.__solveMode(Pi, self.M[aug].U[n], C, n) self.M[aug].U[n] = B ## shift the weight from mode to lambda self.M[aug].normalize_mode(n, 1) return B, Pi, inI, kktModeViolation
def __solveSignalTensor(self, xsubs, BHat, n): ## calculate Psi Psi = tensorTools.calculatePi(self.X, self.M[AUG_LOCATION], n) C = np.multiply(BHat[xsubs,:], Psi) return self.solveSubproblem(C, REG_LOCATION, n)