コード例 #1
0
def calculatePhi(X, B, Pi, n, epsilon=1e-4, C=None):
    """
    Calculate the matrix for multiplicative update

    Parameters
    ----------
    X       : the observed tensor
    B       : the factor matrix associated with mode n
    Pi      : the product of all matrices but the n-th from above
    n       : the mode that we are trying to solve the subproblem for
    epsilon : the 
    C       : the augmented / non-augmented tensor (\alpha u \Psi or B \Phi) in sparse form
    """
    Phi = None
    if X.__class__ == sptensor.sptensor:
        Phi = -np.ones((X.shape[n], B.shape[1]))
        xsubs = X.subs[:,n]
        if C !=  None:
            v = np.sum(np.multiply(B[xsubs,:], Pi) + C, axis=1)
        else:
            v = np.sum(np.multiply(B[xsubs,:], Pi), axis=1)
        wvals = X.vals.flatten() / v
        for r in range(B.shape[1]):
            Phi[:,r] = accumarray.accum_np(xsubs, np.multiply(wvals, Pi[:,r]), size=X.shape[n])
    else:
        Xn = tenmat.tenmat(X,[n])
        V = np.inner(B,Pi)
        W = Xn.data / np.maximum(V, epsilon)
        Phi = np.inner(W, Pi.transpose())  
    return Phi
コード例 #2
0
def calculatePhi(X, B, Pi, n, epsilon=1e-4, C=None):
    """
    Calculate the matrix for multiplicative update

    Parameters
    ----------
    X       : the observed tensor
    B       : the factor matrix associated with mode n
    Pi      : the product of all matrices but the n-th from above
    n       : the mode that we are trying to solve the subproblem for
    epsilon : the 
    C       : the augmented / non-augmented tensor (\alpha u \Psi or B \Phi) in sparse form
    """
    Phi = None
    if X.__class__ == sptensor.sptensor:
        Phi = -np.ones((X.shape[n], B.shape[1]))
        xsubs = X.subs[:, n]
        if C != None:
            v = np.sum(np.multiply(B[xsubs, :], Pi) + C, axis=1)
        else:
            v = np.sum(np.multiply(B[xsubs, :], Pi), axis=1)
        wvals = X.vals.flatten() / v
        for r in range(B.shape[1]):
            Phi[:, r] = accumarray.accum_np(xsubs,
                                            np.multiply(wvals, Pi[:, r]),
                                            size=X.shape[n])
    else:
        Xn = tenmat.tenmat(X, [n])
        V = np.inner(B, Pi)
        W = Xn.data / np.maximum(V, epsilon)
        Phi = np.inner(W, Pi.transpose())
    return Phi
コード例 #3
0
ファイル: tensorTools.py プロジェクト: ykpku/TaGiTed
def calculatePhi(X, B, Pi, n, epsilon=1e-4, C=None):
    """
    Calculate the matrix for multiplicative update

    Parameters
    ----------
    X       : the observed tensor
    B       : the factor matrix associated with mode n
    Pi      : the product of all matrices but the n-th from above
    n       : the mode that we are trying to solve the subproblem for
    epsilon : the 
    C       : the augmented / non-augmented tensor (\alpha u \Psi or B \Phi) in sparse form
    """
    Phi = None
    if X.__class__ == sptensor.sptensor:
        Phi = -np.ones((X.shape[n], B.shape[1]))
        #print 'n:',n

        xsubs = X.subs[:, n]
        #print 'xsub:',xsubs.shape
        if C != None:
            v = np.sum(np.multiply(B[xsubs, :], Pi) + C, axis=1)
            #print 'v1'
        else:
            #print 'B[xsubs,:]',B[xsubs,:].shape
            #print 'Pi',Pi.shape
            #print 'np.multiply(B[xsubs,:], Pi)',np.multiply(B[xsubs,:], Pi).shape
            #print np.multiply(B[xsubs,:], Pi)
            v = np.sum(np.array(np.multiply(B[xsubs, :], Pi)), axis=1)
            #print 'v2'
            #print 'v size:',v.shape
            #print v
        #print '___v size',v.shape
        for i in range(len(v)):
            if v[i] == 0:
                v[i] = 1.0

        wvals = X.vals.flatten() / v
        #print('wvals',wvals.shape)
        for r in range(B.shape[1]):
            #print 'r',r
            #print 'np.array(xsubs)',np.array(xsubs).shape
            Pi = np.array(Pi)
            #print 'Pi[:,r]',Pi[:,r].shape
            #print 'np.array(np.multiply(wvals, Pi[:,r]))',np.array(np.multiply(Pi[:,r],np.array(wvals) )).shape
            Phi[:,
                r] = accumarray.accum_np(np.array(xsubs),
                                         np.array(np.multiply(wvals, Pi[:,
                                                                        r])),
                                         size=X.shape[n])
            #print 'Phi[:,r]',Phi[:,r].shape
    else:
        Xn = tenmat.tenmat(X, [n])
        V = np.inner(B, Pi)
        W = Xn.data / np.maximum(V, epsilon)
        Phi = np.inner(W, Pi.transpose())
    return Phi
コード例 #4
0
def __calculatePhi(X, M, R, n, Pi, epsilon):
    """
    Calculate the matrix for multiplicative update
    """
    Phi = None
    if X.__class__ == sptensor.sptensor:
        Phi = -np.ones((X.shape[n], R))
        xsubs = X.subs[:,n]
        v = np.sum(np.multiply(M.U[n][xsubs,:], Pi), axis=1)
        wvals = X.vals.flatten() / np.maximum(v, epsilon)
        for r in range(R):
            #Phi[:,r] = tools.accum_np(xsubs, np.multiply(wvals, Pi[:,r]), X.shape[n])
            Phi[:,r] = accumarray.accum_np(xsubs, np.multiply(wvals, Pi[:,r]), size=X.shape[n])
    else:
        Xn = tenmat.tenmat(X,[n])
        V = np.inner(M.U[n],Pi)
        W = Xn.data / np.maximum(V, epsilon)
        Phi = np.inner(W, Pi.transpose())
        
    return Phi