def main():
    
    #ut = UnitTests()
    #ut.runAllTests()

    I = random.randint(6,6)
    J = random.randint(6,6)
    K = random.randint(6,6)
    #I = 30
    #J = 30
    #K = 30
    r = 2 
    sparsity = .1
    regParam = 0.1
        
    ctf.random.seed(42)
    U_SVD = ctf.random.random((I,r))
    V_SVD= ctf.random.random((J,r))
    W_SVD= ctf.random.random((K,r))
    
    # 3rd-order tensor
    T_SVD = ctf.tensor((I,J,K),sp=True)
    T_SVD.fill_sp_random(0,1,sparsity)
 
    omega = updateOmega(T_SVD,I,J,K)

    U_CG = ctf.copy(U_SVD)
    V_CG = ctf.copy(V_SVD)
    W_CG = ctf.copy(W_SVD)
    T_CG = ctf.copy(T_SVD)
        
    #t = time.time()  
    #getALS_SVD(T_SVD,U_SVD,V_SVD,W_SVD,regParam,omega,I,J,K,r)   
    #print("ALS SVD costs time = ",np.round_(time.time()- t,4))    


    t = time.time()  
    getALS_CG(T_CG,U_CG,V_CG,W_CG,regParam,omega,I,J,K,r)   
    print("ALS CG costs time = ",np.round_(time.time()- t,4))    
Esempio n. 2
0
 def test_copy(self):
     a1 = ctf.zeros((2, 3, 4))
     a1 = ctf.copy(a1)
     a1 = a1.copy()
Esempio n. 3
0
 def test_copy(self):
     a1 = ctf.zeros((2,3,4))
     a1 = ctf.copy(a1)
     a1 = a1.copy()
    V = ctf.random.random((J, R))
    W = ctf.random.random((K, R))

    [_, RMSE] = get_objective(T, U, V, W, omega, 0)
    if ctf.comm().rank() == 0:
        print("Initial RMSE is", RMSE)

    if numiter_ALS_imp > 0:
        if ctf.comm().rank() == 0:
            print("Performing up to", numiter_ALS_imp,
                  "iterations or until reaching error threshold", err_thresh,
                  "or reaching time limit of,", time_limit,
                  "seconds of ALS with implicit CG")
            print("ALS with implicit CG block size is", block_size_ALS_imp,
                  "and regularization parameter is", reg_ALS)
        U_copy = ctf.copy(U)
        V_copy = ctf.copy(V)
        W_copy = ctf.copy(W)

        getALS_CG(T, U_copy, V_copy, W_copy, reg_ALS, omega, I, J, K, R,
                  block_size_ALS_imp, numiter_ALS_imp, err_thresh, time_limit,
                  True)

    if numiter_ALS_exp > 0:
        if ctf.comm().rank() == 0:
            print("Performing up to", numiter_ALS_exp,
                  "iterations or until reaching error threshold", err_thresh,
                  "or reaching time limit of", time_limit,
                  "seconds of ALS with explicit CG")
            print("ALS with explicit CG block size is", block_size_ALS_exp,
                  "and regularization parameter is", reg_ALS)
def main():

    #ut = UnitTests()
    #ut.runAllTests()

    #I = random.randint(6,6)
    #J = random.randint(6,6)
    #K = random.randint(6,6)
    I = 1000
    J = 1000
    K = 1000
    r = 2
    sparsity = .000001
    regParam = .1
    block = 100

    # 3rd-order tensor
    T_SVD = ctf.tensor((I, J, K), sp=True)
    T_SVD.fill_sp_random(0, 1, sparsity)
    #T_SVD = function_tensor(I,J,K,sparsity)
    assert (T_SVD.sp == 1)

    #omega = updateOmega(T_SVD,I,J,K)
    omega = getOmega(T_SVD)
    assert (omega.sp == 1)

    ctf.random.seed(42)
    U_SVD = ctf.random.random((I, r), sp=True)
    V_SVD = ctf.random.random((J, r), sp=True)
    W_SVD = ctf.random.random((K, r), sp=True)

    U_CG = ctf.copy(U_SVD)
    V_CG = ctf.copy(V_SVD)
    W_CG = ctf.copy(W_SVD)
    T_CG = ctf.copy(T_SVD)

    U_CG2 = ctf.copy(U_SVD)
    V_CG2 = ctf.copy(V_SVD)
    W_CG2 = ctf.copy(W_SVD)
    T_CG2 = ctf.copy(T_SVD)

    #t = time.time()
    #getALS_SVD(T_SVD,U_SVD,V_SVD,W_SVD,regParam,omega,I,J,K,r)
    #print("ALS SVD costs time = ",np.round_(time.time()- t,4))

    if ctf.comm().rank() == 0:
        print(
            "--------------------------------ALS iterative CG------------------------"
        )
    blockCGit, blockCGtime = getALS_CG(T_CG, U_CG, V_CG, W_CG, regParam, omega,
                                       I, J, K, r, block)
    if ctf.comm().rank() == 0:
        print("Number of iterations: %d" % (blockCGit))
        print("CG block size: %d " % (block))
        print("ALS iterative CG costs time: %f" % (blockCGtime))

    if ctf.comm().rank() == 0:
        print(
            "--------------------------------ALS direct SVD------------------------"
        )
    kressnerit, kressnertime = getALS_Kressner(T_CG2, U_CG2, V_CG2, W_CG2,
                                               regParam, omega, I, J, K, r)
    if ctf.comm().rank() == 0:
        print("Number of iterations: %d" % (kressnerit))
        print("ALS direct CG costs time: %f" % (kressnertime))
Esempio n. 6
0
def main():

    I = 1000
    J = 1000
    K = 1000
    r = 2
    sparsity = .000001
    regParam = .1
    block_size = 1000
    use_func = 0
    num_iter = 1
    err_thresh = .001
    run_implicit = 1
    run_explicit = 1

    if len(sys.argv) >= 4:
        I = int(sys.argv[1])
        J = int(sys.argv[2])
        K = int(sys.argv[3])
    if len(sys.argv) >= 5:
        sparsity = np.float64(sys.argv[4])
    if len(sys.argv) >= 6:
        r = int(sys.argv[5])
    if len(sys.argv) >= 7:
        block_size = int(sys.argv[6])
    if len(sys.argv) >= 8:
        use_func = int(sys.argv[7])
    if len(sys.argv) >= 9:
        num_iter = int(sys.argv[8])
    if len(sys.argv) >= 10:
        err_thresh = np.float64(sys.argv[9])
    if len(sys.argv) >= 11:
        regParam = np.float64(sys.argv[10])
    if len(sys.argv) >= 12:
        run_implicit = int(sys.argv[11])
    if len(sys.argv) >= 13:
        run_explicit = int(sys.argv[12])

    if glob_comm.rank() == 0:
        print("I is", I, "J is", J, "K is", K, "sparisty is", sparsity, "r is",
              r, "block_size is", block_size, "use_func is", use_func,
              "num_iter is", num_iter, "err_thresh is", err_thresh,
              "regParam is", regParam, "run_implicit", run_implicit,
              "run_explicit is", run_explicit)

    # 3rd-order tensor
    if use_func == 1:
        T_SVD = function_tensor(I, J, K, sparsity)
    else:
        T_SVD = ctf.tensor((I, J, K), sp=sparse_format)
        T_SVD.fill_sp_random(0, 1, sparsity)

    #omega = updateOmega(T_SVD,I,J,K)
    t0 = time.time()
    omega = getOmega(T_SVD)
    if glob_comm.rank() == 0:
        print('getOmega takes {}'.format(time.time() - t0))

    ctf.random.seed(42)
    U_SVD = ctf.random.random((I, r))
    V_SVD = ctf.random.random((J, r))
    W_SVD = ctf.random.random((K, r))

    U_CG = ctf.copy(U_SVD)
    V_CG = ctf.copy(V_SVD)
    W_CG = ctf.copy(W_SVD)
    T_CG = ctf.copy(T_SVD)

    if run_implicit == True:
        getALS_CG(T_CG, U_CG, V_CG, W_CG, regParam, omega, I, J, K, r,
                  block_size, num_iter, err_thresh, 600, True)
    if run_explicit == True:
        getALS_CG(T_SVD, U_SVD, V_SVD, W_SVD, regParam, omega, I, J, K, r,
                  block_size, num_iter, err_thresh, 600, False)
Esempio n. 7
0
def main():

    #ut = UnitTests()
    #ut.runAllTests()

    #I = random.randint(6,6)
    #J = random.randint(6,6)
    #K = random.randint(6,6)
    I = 8
    J = 8
    K = 8
    r = 2
    sparsity = .1
    regParam = .1
    block = 4
    ntrails = 5

    # 3rd-order tensor
    #T_SVD = ctf.tensor((I,J,K),sp=True)
    #T_SVD.fill_sp_random(0,1,sparsity)
    T_SVD = function_tensor(I, J, K, sparsity)
    assert (T_SVD.sp == 1)

    omega = updateOmega(T_SVD, I, J, K)
    assert (omega.sp == 1)

    blockCGerrList = []
    blockCGtimeList = []
    KressnererrList = []
    KressnertimeList = []
    for i in range(ntrails):

        ctf.random.seed(42 + i)
        U_SVD = ctf.random.random((I, r), sp=True)
        V_SVD = ctf.random.random((J, r), sp=True)
        W_SVD = ctf.random.random((K, r), sp=True)

        U_CG = ctf.copy(U_SVD)
        V_CG = ctf.copy(V_SVD)
        W_CG = ctf.copy(W_SVD)
        T_CG = ctf.copy(T_SVD)

        U_CG2 = ctf.copy(U_SVD)
        V_CG2 = ctf.copy(V_SVD)
        W_CG2 = ctf.copy(W_SVD)
        T_CG2 = ctf.copy(T_SVD)

        #t = time.time()
        #getALS_SVD(T_SVD,U_SVD,V_SVD,W_SVD,regParam,omega,I,J,K,r)
        #print("ALS SVD costs time = ",np.round_(time.time()- t,4))

        t = time.time()
        blockCGnorm, blockCGit, blockCGtime = getALS_CG(
            T_CG, U_CG, V_CG, W_CG, regParam, omega, I, J, K, r, block)
        blockCGerrList.append(blockCGnorm)
        blockCGtimeList.append(blockCGtime)
        if ctf.comm().rank() == 0:
            print("Number of iterations: %d" % (blockCGit))
            print("CG block size: %d " % (block))
            print("ALS iterative CG costs time: %f" %
                  (np.round_(time.time() - t, 4)))

        t = time.time()
        kressnernorm, kressnerit, kressnertime = getALS_Kressner(
            T_CG2, U_CG2, V_CG2, W_CG2, regParam, omega, I, J, K, r)
        KressnererrList.append(kressnernorm)
        KressnertimeList.append(kressnertime)
        if ctf.comm().rank() == 0:
            print("Number of iterations: %d" % (kressnerit))
            print("ALS direct CG costs time: %f" %
                  (np.round_(time.time() - t, 4)))


#----------------------------------------- plot -------------------------------------------------#

    plt.figure()
    for i in range(ntrails):
        if ctf.comm().rank() == 0:
            plt.plot(blockCGtimeList[i],
                     blockCGerrList[i],
                     label="trail %d" % (i + 1))
            plt.legend()
            plt.title(
                "Function tensor(%d*%d*%d), iterative block CG, block size %d, rank %d, sparsity %f"
                % (I, J, K, block, r, sparsity))
            plt.xlabel("Time[s]")
            plt.ylabel("Training Error Norm")
            plt.savefig('iterative_block_CG.png')

    plt.figure()
    for i in range(ntrails):
        if ctf.comm().rank() == 0:
            plt.plot(KressnertimeList[i],
                     KressnererrList[i],
                     label="trail %d" % (i + 1))
            plt.legend()
            plt.title(
                "Funtion tensor(%d*%d*%d),direct CG (Kressner), rank %d, sparsity %f "
                % (I, J, K, r, sparsity))
            plt.xlabel("Time[s]")
            plt.ylabel("Training Error Norm")
            plt.savefig('direct_CG.png')
Esempio n. 8
0
def run_CCD(T,U,V,W,omega,regParam,num_iter,time_limit,objective_frequency,use_MTTKRP=True):
    U_vec_list = []
    V_vec_list = []
    W_vec_list = []
    r = U.shape[1]
    for f in range(r):
        U_vec_list.append(U[:,f])
        V_vec_list.append(V[:,f])
        W_vec_list.append(W[:,f])


    # print(T)
    # T.write_to_file('tensor_out.txt')
    # assert(T.sp == 1)

    ite = 0
    objectives = []

    t_before_loop = time.time()
    t_obj_calc = 0.

    t_CCD = ctf.timer_epoch("ccd_CCD")
    t_CCD.begin()
    while True:

        t_iR_upd = ctf.timer("ccd_init_R_upd")
        t_iR_upd.start()
        t0 = time.time()
        R = ctf.copy(T)
        t1 = time.time()
        # R -= ctf.einsum('ijk, ir, jr, kr -> ijk', omega, U, V, W)
        R -= ctf.TTTP(omega, [U,V,W])
        t2 = time.time()
        # R += ctf.einsum('ijk, i, j, k -> ijk', omega, U[:,0], V[:,0], W[:,0])
        R += ctf.TTTP(omega, [U[:,0], V[:,0], W[:,0]])
        t3 = time.time()

        t_iR_upd.stop()

        t_b_obj = time.time()
        if ite % objective_frequency == 0:
            duration = time.time() - t_before_loop - t_obj_calc
            [objective, RMSE] = get_objective(T,U,V,W,omega,regParam)
            objectives.append(objective)
            if glob_comm.rank() == 0:
                print('Objective after',duration,'seconds (',ite,'iterations) is: {}'.format(objective))
                print('RMSE after',duration,'seconds (',ite,'iterations) is: {}'.format(RMSE))
        t_obj_calc += time.time() - t_b_obj

        if glob_comm.rank() == 0 and status_prints == True:
            print('ctf.copy() takes {}'.format(t1-t0))
            print('ctf.TTTP() takes {}'.format(t2-t1))
            print('ctf.TTTP() takes {}'.format(t3-t2))


        for f in range(r):

            # update U[:,f]
            if glob_comm.rank() == 0 and status_prints == True:
                print('updating U[:,{}]'.format(f))

            t0 = time.time()
            if use_MTTKRP:
                alphas = ctf.tensor(R.shape[0])
                #ctf.einsum('ijk -> i', ctf.TTTP(R, [None, V_vec_list[f], W_vec_list[f]]),out=alphas)
                ctf.MTTKRP(R, [alphas, V_vec_list[f], W_vec_list[f]], 0)
            else:
                alphas = ctf.einsum('ijk, j, k -> i', R, V_vec_list[f], W_vec_list[f])

            t1 = time.time()

            if use_MTTKRP:
                betas = ctf.tensor(R.shape[0])
                #ctf.einsum('ijk -> i', ctf.TTTP(omega, [None, V_vec_list[f]*V_vec_list[f], W_vec_list[f]*W_vec_list[f]]),out=betas)
                ctf.MTTKRP(omega, [betas, V_vec_list[f]*V_vec_list[f], W_vec_list[f]*W_vec_list[f]], 0)
            else:
                betas = ctf.einsum('ijk, j, j, k, k -> i', omega, V_vec_list[f], V_vec_list[f], W_vec_list[f], W_vec_list[f])

            t2 = time.time()

            U_vec_list[f] = alphas / (regParam + betas)
            U[:,f] = U_vec_list[f]

            if glob_comm.rank() == 0 and status_prints == True:
                print('ctf.einsum() takes {}'.format(t1-t0))
                print('ctf.einsum() takes {}'.format(t2-t1))


            # update V[:,f]
            if glob_comm.rank() == 0 and status_prints == True:
                print('updating V[:,{}]'.format(f))
            if use_MTTKRP:
                alphas = ctf.tensor(R.shape[1])
                #ctf.einsum('ijk -> j', ctf.TTTP(R, [U_vec_list[f], None, W_vec_list[f]]),out=alphas)
                ctf.MTTKRP(R, [U_vec_list[f], alphas, W_vec_list[f]], 1)
            else:
                alphas = ctf.einsum('ijk, i, k -> j', R, U_vec_list[f], W_vec_list[f])

            if use_MTTKRP:
                betas = ctf.tensor(R.shape[1])
                #ctf.einsum('ijk -> j', ctf.TTTP(omega, [U_vec_list[f]*U_vec_list[f], None, W_vec_list[f]*W_vec_list[f]]),out=betas)
                ctf.MTTKRP(omega, [U_vec_list[f]*U_vec_list[f], betas, W_vec_list[f]*W_vec_list[f]], 1)
            else:
                betas = ctf.einsum('ijk, i, i, k, k -> j', omega, U_vec_list[f], U_vec_list[f], W_vec_list[f], W_vec_list[f])

            V_vec_list[f] = alphas / (regParam + betas)
            V[:,f] = V_vec_list[f]


            if glob_comm.rank() == 0 and status_prints == True:
                print('updating W[:,{}]'.format(f))
            if use_MTTKRP:
                alphas = ctf.tensor(R.shape[2])
                #ctf.einsum('ijk -> k', ctf.TTTP(R, [U_vec_list[f], V_vec_list[f], None]),out=alphas)
                ctf.MTTKRP(R, [U_vec_list[f], V_vec_list[f], alphas], 2)
            else:
                alphas = ctf.einsum('ijk, i, j -> k', R, U_vec_list[f], V_vec_list[f])

            if use_MTTKRP:
                betas = ctf.tensor(R.shape[2])
                #ctf.einsum('ijk -> k', ctf.TTTP(omega, [U_vec_list[f]*U_vec_list[f], V_vec_list[f]*V_vec_list[f], None]),out=betas)
                ctf.MTTKRP(omega, [U_vec_list[f]*U_vec_list[f], V_vec_list[f]*V_vec_list[f], betas], 2)
            else:
                betas = ctf.einsum('ijk, i, i, j, j -> k', omega, U_vec_list[f], U_vec_list[f], V_vec_list[f], V_vec_list[f])

            W_vec_list[f] = alphas / (regParam + betas)
            W[:,f] = W_vec_list[f]



            t_tttp = ctf.timer("ccd_TTTP")
            t_tttp.start()
            R -= ctf.TTTP(omega, [U_vec_list[f], V_vec_list[f], W_vec_list[f]])

            if f+1 < r:
                R += ctf.TTTP(omega, [U_vec_list[f+1], V_vec_list[f+1], W_vec_list[f+1]])

            t_tttp.stop()
        t_iR_upd.stop()

        ite += 1

        if ite == num_iter or time.time() - t_before_loop - t_obj_calc > time_limit:
            break

    t_CCD.end()
    duration = time.time() - t_before_loop - t_obj_calc
    [objective, RMSE] = get_objective(T,U,V,W,omega,regParam)

    if glob_comm.rank() == 0:
        print('CCD amortized seconds per sweep: {}'.format(duration/ite))
        print('Time/CCD Iteration: {}'.format(duration/ite))
        print('Objective after',duration,'seconds (',ite,'iterations) is: {}'.format(objective))
        print('RMSE after',duration,'seconds (',ite,'iterations) is: {}'.format(RMSE))
Esempio n. 9
0
def main():

    I = 300
    J = 300
    K = 300

    r = 30

    sparsity = .1
    regParam = .1

    ctf.random.seed(42)

    # # 3rd-order tensor
    # T = ctf.tensor((I,J,K))
    # # T.read_from_file('tensor.txt')
    # T.fill_sp_random(0,1,sparsity)
    T = function_tensor(I, J, K, sparsity)

    t0 = time.time()
    omega = getOmega(T)

    if glob_comm.rank() == 0:
        print('getOmega takes {}'.format(time.time() - t0))

    U = ctf.random.random((I, r))
    V = ctf.random.random((J, r))
    W = ctf.random.random((K, r))

    # print(T)
    # T.write_to_file('tensor_out.txt')
    # assert(T.sp == 1)

    # exit(0)
    # print(U)
    # print(V)
    # print(W)

    ite = 0
    objectives = []

    t_before_loop = time.time()

    while True:

        t0 = time.time()
        R = ctf.copy(T)
        t1 = time.time()

        R -= ctf.einsum('ijk, ir, jr, kr -> ijk', omega, U, V, W)
        t2 = time.time()
        R += ctf.einsum('ijk, i, j, k -> ijk', omega, U[:, 0], V[:, 0], W[:,
                                                                          0])

        t3 = time.time()

        # print(R)
        # exit(0)

        t4 = time.time()
        objective = get_objective(T, U, V, W, I, J, K, omega, regParam)
        if glob_comm.rank() == 0:
            print('ctf.copy() takes {}'.format(t1 - t0))
            print('ctf.einsum() takes {}'.format(t2 - t1))
            print('ctf.einsum() takes {}'.format(t3 - t2))
            print('get_objective takes {}'.format(time.time() - t4))
            print('Objective: {}'.format(objective))

        objectives.append(objective)

        for f in range(r):

            # update U[:,f]
            if glob_comm.rank() == 0:
                print('updating U[:,{}]'.format(f))

            t0 = time.time()
            alphas = ctf.einsum('ijk, j, k -> i', R, V[:, f], W[:, f])
            t1 = time.time()
            betas = ctf.einsum('ijk, j, j, k, k -> i', omega, V[:, f], V[:, f],
                               W[:, f], W[:, f])
            t2 = time.time()

            U[:, f] = alphas / (regParam + betas)

            objective = get_objective(T, U, V, W, I, J, K, omega, regParam)
            if glob_comm.rank() == 0:
                print('Objective: {}'.format(objective))
                print('ctf.einsum() takes {}'.format(t1 - t0))
                print('ctf.einsum() takes {}'.format(t2 - t1))

            objectives.append(objective)

            # update V[:,f]
            if glob_comm.rank() == 0:
                print('updating V[:,{}]'.format(f))
            alphas = ctf.einsum('ijk, i, k -> j', R, U[:, f], W[:, f])
            betas = ctf.einsum('ijk, i, i, k, k -> j', omega, U[:, f], U[:, f],
                               W[:, f], W[:, f])

            V[:, f] = alphas / (regParam + betas)

            objective = get_objective(T, U, V, W, I, J, K, omega, regParam)
            if glob_comm.rank() == 0:
                print('Objective: {}'.format(objective))
            objectives.append(objective)

            # exit(0)

            # update W[:,f]
            if glob_comm.rank() == 0:
                print('updating W[:,{}]'.format(f))
            alphas = ctf.einsum('ijk, i, j -> k', R, U[:, f], V[:, f])
            betas = ctf.einsum('ijk, i, i, j, j -> k', omega, U[:, f], U[:, f],
                               V[:, f], V[:, f])

            W[:, f] = alphas / (regParam + betas)

            objective = get_objective(T, U, V, W, I, J, K, omega, regParam)
            if glob_comm.rank() == 0:
                print('Objective: {}'.format(objective))
            objectives.append(objective)

            # exit(0)

            # t0 = time.time()
            R -= ctf.einsum('ijk, i, j, k -> ijk', omega, U[:, f], V[:, f],
                            W[:, f])
            R += ctf.einsum('ijk, i, j, k -> ijk', omega, U[:, f + 1],
                            V[:, f + 1], W[:, f + 1])
            # print(time.time() - t0)

            # print(R)
            # exit(0)

        ite += 1

        if ite == 1:
            break

    if glob_comm.rank() == 0:
        print('Time/Iteration: {}'.format((time.time() - t_before_loop) / 1))