Пример #1
0
def cpd2unfold1(T1_approx, factors):
    """
    Converts the factor matrices to the first unfolding of the corresponding tensor.

    Inputs
    ------
    factors: list of 2-D arrays
        The factor matrices.

    Outputs
    ------
    T1_approx: float 2-D array
        First unfolding of T_approx, where T_approx is (factors[0],...,factors[L-1])*I in coordinate format.
    """

    L = len(factors)
    M = factors[1]

    for l in range(2, L):
        N = empty((M.shape[0] * factors[l].shape[0], M.shape[1]))
        M = mlinalg.khatri_rao(factors[l], M, N)

    dot(factors[0], M.T, out=T1_approx)

    return T1_approx
Пример #2
0
def compute_grad(Tl, factors, P1, g, N, gg, dims, sum_dims):
    """
    This function computes the gradient of the error function.
    """

    # Initialize first variables.
    L = len(factors)
    R = factors[0].shape[1]

    # Main computations.
    for l in range(L):
        itr = [l for l in reversed(range(L))]
        itr.remove(l)
        M = factors[itr[0]]

        # Compute Khatri-Rao products W^(L) ⊙ ... ⊙ W^(l+1) ⊙ W^(l-1) ⊙ ... ⊙ W^(1).
        for ll in range(L-2):
            tmp = M
            dim1, dim2 = tmp.shape[0], dims[itr[ll+1]]
            M = empty((dim1 * dim2, R), dtype=float64)
            M = mlinalg.khatri_rao(tmp, factors[itr[ll+1]], M)

        dot(Tl[l], M, out=N[l])
        dot(factors[l], P1[l], out=gg[l])
        g[sum_dims[l]: sum_dims[l+1]] = (gg[l] - N[l]).T.ravel()

    return g
Пример #3
0
def cpd2tens(factors):
    """
    Converts the factor matrices to tensor in coordinate format using a Khatri-Rao product formula.

    Inputs
    ------
    factors: list of 2-D arrays
        The factor matrices.

    Outputs
    ------
    T_approx: float L-D array
        Tensor (factors[0],...,factors[L-1])*I in coordinate format. 
    """

    L = len(factors)
    dims = [factors[l].shape[0] for l in range(L)]
    T_approx = empty(dims)
    M = factors[1]

    for l in range(2, L):
        N = empty((M.shape[0] * factors[l].shape[0], M.shape[1]))
        M = mlinalg.khatri_rao(factors[l], M, N)

    T1_approx = dot(factors[0], M.T)
    T_approx = foldback(T_approx, T1_approx, 1)

    return T_approx
Пример #4
0
def als_iteration(Tl, factors, fix_mode):
    """
    This function the ALS iterations, that is, it computes the pseudoinverse with respect to the modes. This 
    implementation is simple and not intended to be optimal. 
    """

    # Initialize first variables.
    L = len(factors)
    R = factors[0].shape[1]
    dims = [factors[l].shape[0] for l in range(L)]

    # Main computations for the general case.
    if fix_mode == -1:
        for l in range(L):
            itr = [l for l in reversed(range(L))]
            itr.remove(l)
            M = factors[itr[0]]

            # Compute Khatri-Rao products W^(L) ⊙ ... ⊙ W^(l+1) ⊙ W^(l-1) ⊙ ... ⊙ W^(1).
            for ll in range(L - 2):
                tmp = M
                dim1, dim2 = tmp.shape[0], dims[itr[ll + 1]]
                M = empty((dim1 * dim2, R), dtype=float64)
                M = mlinalg.khatri_rao(tmp, factors[itr[ll + 1]], M)

            factors[l] = dot(Tl[l], pinv(M.T))

        return factors

    # If fix_mode != -1, it is assumed that the program is using the bicpd function.
    # This part is only used for third order tensors.
    X, Y, Z = factors
    T1, T2, T3 = Tl

    if fix_mode == 0:
        M = empty((Z.shape[0] * X.shape[0], R))
        M = mlinalg.khatri_rao(Z, X, M)
        Y = dot(T2, pinv(M.T))
        M = empty((Y.shape[0] * X.shape[0], R))
        M = mlinalg.khatri_rao(Y, X, M)
        Z = dot(T3, pinv(M.T))

    elif fix_mode == 1:
        X, Y, Z = factors
        M = empty((Z.shape[0] * Y.shape[0], R))
        M = mlinalg.khatri_rao(Z, Y, M)
        X = dot(T1, pinv(M.T))
        M = empty((Y.shape[0] * X.shape[0], R))
        M = mlinalg.khatri_rao(Y, X, M)
        Z = dot(T3, pinv(M.T))

    elif fix_mode == 2:
        X, Y, Z = factors
        M = empty((Z.shape[0] * Y.shape[0], R))
        M = mlinalg.khatri_rao(Z, Y, M)
        X = dot(T1, pinv(M.T))
        M = empty((Z.shape[0] * X.shape[0], R))
        M = mlinalg.khatri_rao(Z, X, M)
        Y = dot(T2, pinv(M.T))

    return [X, Y, Z]