Example #1
0
    def _ttm_compute(self, V, mode, transp):
        Z = self.unfold(mode, transp=True).tocsr()
        if transp:
            V = V.T
        Z = Z.dot(V.T)
        shape = copy(self.shape)
        shape[mode] = V.shape[0]
        if issparse_mat(Z):
            newT = unfolded_sptensor((Z.data, (Z.row, Z.col)), [mode], None, shape=shape).fold()
        else:
            newT = unfolded_dtensor(Z.T, mode, shape).fold()

        return newT
Example #2
0
def nvecs(X, n, rank, do_flipsign=True, use_eigsh=False, dtype=np.float):
    """
    Eigendecomposition of mode-n unfolding of a tensor

    Parameters
    ----------
    X : sptensor or dtensor
    n : int
    rank : int
           number of eigenvectors to return
    do_flipsign: bool, optional
              Flip sign of factor matrices such that largest magnitude
              element will be positive
    use_eigsh : bool, optional
            if True, use the Implicitly Restarted Lanczos Method (scipy.sparse.linalg.eigsh)
            to find eigenvectors of matrix unfolding.
            eigsh is effective for large eigenvalues of sparse matrices,
            but works poorly for dense matrices or when many eigenvalues are needed.
    dtype: data-type, optional
    """
    Xn = X.unfold(n)
    if issparse_mat(Xn):
        Xn = csr_matrix(Xn, dtype=dtype)
    Y = Xn.dot(Xn.T)
    if not use_eigsh:
        if issparse_mat(Y):
            Y = Y.todense()
        N = Y.shape[0]
        _, U = eigh(Y, eigvals=(N - rank, N - 1))
        #_, U = eigsh(Y, rank, which='LM')
    else:
        _, U = eigsh(Y, rank, which='LM')
    # reverse order of eigenvectors such that eigenvalues are decreasing
    U = array(U[:, ::-1])
    # flip sign
    if do_flipsign:
        U = flipsign(U)
    return U
Example #3
0
    def _ttm_compute(self, V, mode, transp):
        Z = self.unfold(mode, transp=True).tocsr()
        if transp:
            V = V.T
        Z = Z.dot(V.T)
        shape = np.copy(self.shape)
        shape[mode] = V.shape[0]
        if issparse_mat(Z):
            newT = unfolded_sptensor((Z.data, (Z.row, Z.col)), [mode],
                                     None,
                                     shape=shape).fold()
        else:
            newT = unfolded_dtensor(Z.T, mode, shape).fold()

        return newT
Example #4
0
def nvecs(X, n, rank, do_flipsign=True):
    """
    Eigendecomposition of mode-n unfolding of a tensor
    """
    Xn = X.unfold(n)
    Y = Xn.dot(Xn.T)
    if issparse_mat(Y):
        _, U = eigsh(Y, rank, which='LM')
    else:
        N = Y.shape[0]
        _, U = eigh(Y, eigvals=(N - rank, N - 1))
        #_, U = eigsh(Y, rank, which='LM')
    # reverse order of eigenvectors such that eigenvalues are decreasing
    U = array(U[:, ::-1])
    # flip sign
    if do_flipsign:
        U = flipsign(U)
    return U
Example #5
0
def nvecs(X, n, rank, do_flipsign=True, dtype=np.float):
    """
    Eigendecomposition of mode-n unfolding of a tensor
    """
    Xn = X.unfold(n)
    if issparse_mat(Xn):
        Xn = csr_matrix(Xn, dtype=dtype)
        Y = Xn.dot(Xn.T)
        _, U = eigsh(Y, rank, which='LM')
    else:
        Y = Xn.dot(Xn.T)
        N = Y.shape[0]
        _, U = eigh(Y, eigvals=(N - rank, N - 1))
    # reverse order of eigenvectors such that eigenvalues are decreasing
    U = np.array(U[:, ::-1])
    # flip sign
    if do_flipsign:
        U = flipsign(U)
    return U