Beispiel #1
0
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
Beispiel #2
0
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
Beispiel #3
0
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
Beispiel #4
0
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
Beispiel #5
0
 def __solveMode(self, Pi, B, C, n):
     """
     Performs the inner iterations and checks for convergence.
     """
     for innerI in range(self.maxInnerIters):
         # Phi = (X(n) elem-div (B Pi)) Pi^T
         Phi = tensorTools.calculatePhi(self.X, B, Pi, n, C=C)
         # check for convergence that min(B(n), E - Phi(n)) = 0 [or close]
         kktModeViolation = np.max(np.abs(np.minimum(B, 1-Phi).flatten()))
         if (kktModeViolation < self.convTol):
             break
         # Do the multiplicative update
         B = np.multiply(B, Phi)
     return B, (innerI+1), kktModeViolation
Beispiel #6
0
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