Exemplo n.º 1
0
def test_cprand_random():
    """
    For a noised I*J*K rank r random tensor, with random initialized factor matrices
    plot err_fast and exact err for simple / complicated case
    """
    I = 50
    J = 50
    K = 50
    r = 10  # rank
    n_samples = int(10 * r * np.log(r) + 1)  # nb of randomized samples
    fac_true, noise = init_factors(I, J, K, r, True)
    t = tl.cp_to_tensor((None, fac_true)) + noise
    print(tl.norm(t))
    factors = random_init_fac(t, r)
    weights2, factors2, it2, error2, error_es2 = CPRAND(
        t,
        r,
        n_samples,
        factors=copy.deepcopy(factors),
        exact_err=True,
        it_max=500,
        err_it_max=400)
    plt.figure(0)
    plt.plot(range(len(error2)), error2, 'b-', label="exact")
    plt.plot(range(len(error_es2)), error_es2, 'r--', label="err fast")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('cprand for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(1)
    fac_true, noise = init_factors(I, J, K, r, False)
    t = tl.cp_to_tensor((None, fac_true)) + noise
    print(tl.norm(t))
    factors = random_init_fac(t, r)
    weights2, factors2, it2, error2, error_es2 = CPRAND(
        t,
        r,
        n_samples,
        factors=copy.deepcopy(factors),
        exact_err=True,
        it_max=500,
        err_it_max=400)
    plt.plot(range(len(error2)), error2, 'b-', label="exact")
    plt.plot(range(len(error_es2)), error_es2, 'r--', label="err fast")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('cprand for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
Exemplo n.º 2
0
def test_herals_score():
    """
    Test of score for a 3 order simple tensor

    """
    # create a kruskal tensor
    # factor matrices
    I = 3
    J = 3
    K = 3
    r = 3
    factors, noise = init_factors(I, J, K, r)

    t_krus = tl.cp_to_tensor((None, factors))
    factors_init = random_init_fac(t_krus, r)

    weights, factors1, it, error1, l, pct = her_Als(t_krus,
                                                    r,
                                                    factors=factors_init,
                                                    it_max=500,
                                                    list_factors=True)
    print(score(factors, factors1))
    weights1, factors1n = tl.cp_normalize((weights, factors1))
    weight, factorsn = tl.cp_normalize((None, factors))
    for i in factors1n:
        print(i)
    for i in factorsn:
        print(i)
    print(it)
    print(error1[len(error1) - 1])
Exemplo n.º 3
0
def test_nnals():
    """
    Test of nn_als for a kruskal tensor, start with random factors

    """
    # create a kruskal tensor
    # factor matrices
    fac_true, noise = init_factors(200, 200, 200, 10, scale=True, nn=True)

    t_krus = tl.cp_to_tensor((None, fac_true)) + noise
    factors = random_init_fac(t_krus, 10)

    weights, factors1, it, error1, l = nn_als(t_krus,
                                              10,
                                              factors=factors,
                                              tol=0.1,
                                              list_factors=True)
    print(it)
    for i in error1:
        print(i)
Exemplo n.º 4
0
def test_hercprand_13():
    """
    test hercprand 1 3 for complicated case
    """
    I=50
    J=50
    K=50
    r=10 # rank
    n_samples=int(10*r*np.log(r)+1) # nb of randomized samples
    fac_true,noise=init_factors(I,J,K,r,True)
    t=tl.cp_to_tensor((None,fac_true))+noise
    factors=random_init_fac(t,r)
    
    weights1,factors1,it1,error1,cpt1,time1=her_Als(t,r,factors=copy.deepcopy(factors),it_max=200,time_rec=True)
    print("her als Complicated case pct restart",cpt1)
    
    weights3,factors3,it3,error3,error_es3,cpt3,time3=her_CPRAND3(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("3 Complicated case pct restart",cpt3)
    print("3 Complicated case time err_rand",np.cumsum(time3)[len(time3)-1])
    print("3 min error", np.min(error3))
    print("3 min error es", np.min(error_es3))
  
    weights5,factors5,it5,error5,error_es5,cpt5,time5=her_CPRAND1(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("5 Complicated case pct restart",cpt5)
    print("5 Complicated case time err_rand",np.cumsum(time5)[len(time5)-1])
    print("5 min error", np.min(error5))
    print("5 min error es", np.min(error_es5))
    
    
    plt.figure(0)
    plt.plot(range(len(error3)),error3,'b-',label="exact 3")
    plt.plot(range(len(error_es3)),error_es3,'r--',label="err rand 3")
    plt.plot(range(len(error5)),error5,'g-',label="exact 1")
    plt.plot(range(len(error_es5)),error_es5,'k--',label="err rand 1")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand 1 3 for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
Exemplo n.º 5
0
def test_hercprand():
    """
    Run herCPRAND1 2 3 4 5 for the simple and complicated case, plot exact/estimated error.
    Print restart percentage.
    Compare running time with herCPRAND for complicated case.
    """
    I=50
    J=50
    K=50
    r=10 # rank
    n_samples=int(10*r*np.log(r)+1) # nb of randomized samples
    fac_true,noise=init_factors(I,J,K,r,True)
    t=tl.cp_to_tensor((None,fac_true))+noise
    factors=random_init_fac(t,r)
    weights1,factors1,it1,error1,error_es1,cpt1,time1=her_CPRAND1(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("1 Complicated case pct restart",cpt1)
    print("1 Complicated case time err_rand",np.cumsum(time1)[len(time1)-1])
    weights2,factors2,it2,error2,error_es2,cpt2,time2=her_CPRAND2(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("2 Complicated case pct restart",cpt2)
    print("2 Complicated case time err_rand",np.cumsum(time2)[len(time2)-1])
    weights3,factors3,it3,error3,error_es3,cpt3,time3=her_CPRAND3(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("3 Complicated case pct restart",cpt3)
    print("3 Complicated case time err_rand",np.cumsum(time3)[len(time3)-1])
    weights4,factors4,it4,error4,error_es4,cpt4,time4=her_CPRAND4(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("4 Complicated case pct restart",cpt4)
    print("4 Complicated case time err_rand",np.cumsum(time4)[len(time4)-1])
    weights5,factors5,it5,error5,error_es5,cpt5,time5=her_CPRAND5(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("5 Complicated case pct restart",cpt5)
    print("5 Complicated case time err_rand",np.cumsum(time5)[len(time5)-1])
    
    
    
    
    plt.figure(0)
    plt.plot(range(len(error1)),error1,'b-',label="exact")
    plt.plot(range(len(error_es1)),error_es1,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand1 for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(1)
    plt.plot(range(len(error2)),error2,'b-',label="exact")
    plt.plot(range(len(error_es2)),error_es2,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand2 for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(2)
    plt.plot(range(len(error3)),error3,'b-',label="exact")
    plt.plot(range(len(error_es3)),error_es3,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand3 for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(3)
    plt.plot(range(len(error4)),error4,'b-',label="exact")
    plt.plot(range(len(error_es4)),error_es4,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand4 for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(4)
    plt.plot(range(len(error5)),error5,'b-',label="exact")
    plt.plot(range(len(error_es5)),error_es5,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand5 for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    

    fac_true,noise=init_factors(I,J,K,r,False)
    t=tl.cp_to_tensor((None,fac_true))+noise
    factors=random_init_fac(t,r)
    weights1,factors1,it1,error1,error_es1,cpt1,time1=her_CPRAND1(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("Simple case pct restart",cpt1)
    weights2,factors2,it2,error2,error_es2,cpt2,time2=her_CPRAND2(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("Simple case pct restart",cpt2)
    weights3,factors3,it3,error3,error_es3,cpt3,time3=her_CPRAND(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("Simple case pct restart",cpt3)
    weights4,factors4,it4,error4,error_es4,cpt4,time4=her_CPRAND4(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("Simple case pct restart",cpt4)
    weights5,factors5,it5,error5,error_es5,cpt5,time5=her_CPRAND5(t,r,n_samples,factors=copy.deepcopy(factors),exact_err=True,it_max=200,err_it_max=100,time_rec=True)
    print("Simple case pct restart",cpt5)
    plt.figure(5)
    plt.plot(range(len(error1)),error1,'b-',label="exact")
    plt.plot(range(len(error_es1)),error_es1,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand1 for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(6)
    plt.plot(range(len(error2)),error2,'b-',label="exact")
    plt.plot(range(len(error_es2)),error_es2,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand2 for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(7)
    plt.plot(range(len(error3)),error3,'b-',label="exact")
    plt.plot(range(len(error_es3)),error_es3,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand3 for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(8)
    plt.plot(range(len(error4)),error4,'b-',label="exact")
    plt.plot(range(len(error_es4)),error_es4,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand4 for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
    plt.figure(9)
    plt.plot(range(len(error5)),error5,'b-',label="exact")
    plt.plot(range(len(error_es5)),error_es5,'r--',label="err rand")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('hercprand5 for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
Exemplo n.º 6
0
def compar_time(I,
                J,
                K,
                r,
                nb_rand,
                n_samples,
                exact_err=False,
                list_factors=False,
                scale=False):
    """
    plot data fitting/factors error over nb_rand noised I*J*K rank r random tensors,
    with the median value in bold.
    x aixs is time.
    For each tensor, we have 5 factors initializations.
    Need to change plot x,y label and title to run the test.

    Parameters
    ----------
    I : int
        dimension of mode 1.
    J : int
        dimension of mode 2.
    K : int
        dimension of mode 3.
    r : int
        rank.
    nb_rand : int
        nb of tensors.
    n_samples : int
        sample size used for herCPRAND.
    exact_err : boolean, optional
        whether use exact error computation or not for herCPRAND. The default is False.
    list_factors : TYPE, optional
        DESCRIPTION. The default is False.
    scale : TYPE, optional
        DESCRIPTION. The default is False.

    Returns
    -------
    None.

  """
    list_err1 = []
    list_time1 = []
    list_err2 = []
    list_time2 = []
    list_err3 = []
    list_time3 = []
    list_err4 = []
    list_time4 = []
    min_e = None
    for i in range(nb_rand):
        # Random initialization of a noised cp_tensor
        np.random.seed(i)
        A, B, C, noise = init_factors(I, J, K, r, scale)
        fac_true = [A, B, C]
        t = tl.cp_to_tensor((None, fac_true)) + noise
        norm_tensor = tl.norm(t, 2)
        if (min_e == None): min_e = norm_tensor
        for k in range(5):
            factors = random_init_fac(t, r)
            if list_factors == False:
                weights4, factors4, it4, error4, _, cpt, time4 = her_CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=250,
                    time_rec=True)
                weights1, factors1, it1, error1, cpt, time1 = her_Als(
                    t,
                    r,
                    factors=copy.deepcopy(factors),
                    it_max=500,
                    time_rec=True)
                weights3, factors3, it3, error3, _, time3 = CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=250,
                    time_rec=True)
                weights2, factors2, it2, error2, time2 = als(
                    t,
                    r,
                    factors=copy.deepcopy(factors),
                    it_max=500,
                    time_rec=True)
                error1 = [i * norm_tensor for i in error1]
                del error1[0]
                error2 = [i * norm_tensor for i in error2]
                del error2[0]
                error3 = [i * norm_tensor for i in error3]
                del error3[0]
                error4 = [i * norm_tensor for i in error4]
                del error4[0]
            else:
                weights4, factors4, it4, error, _, cpt, l4, time4 = her_CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=250,
                    list_factors=list_factors,
                    time_rec=True)
                weights1, factors1, it1, error, cpt, l1, time1 = her_Als(
                    t,
                    r,
                    factors=copy.deepcopy(factors),
                    it_max=500,
                    list_factors=list_factors,
                    time_rec=True)
                weights3, factors3, it3, error, _, l3, time3 = CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=250,
                    list_factors=list_factors,
                    time_rec=True)
                weights2, factors2, it2, error, l2, time2 = als(
                    t,
                    r,
                    factors=copy.deepcopy(factors),
                    it_max=500,
                    list_factors=list_factors,
                    time_rec=True)
                error1 = [err_fac(fac_true, i) for i in l1]
                del error1[0]
                error2 = [err_fac(fac_true, i) for i in l2]
                del error2[0]
                error3 = [err_fac(fac_true, i) for i in l3]
                del error3[0]
                error4 = [err_fac(fac_true, i) for i in l4]
                del error4[0]
            if (min_e > min(min(error1), min(error2), min(error3),
                            min(error4))):
                min_e = min(min(error1), min(error2), min(error3), min(error4))
            list_err1.append(error1)
            list_err2.append(error2)
            list_err3.append(error3)
            list_err4.append(error4)
            list_time1.append(time1)
            list_time2.append(time2)
            list_time3.append(time3)
            list_time4.append(time4)
    list_err1 = np.array([np.array(i) for i in list_err1])
    list_err2 = np.array([np.array(i) for i in list_err2])
    list_err3 = np.array([np.array(i) for i in list_err3])
    list_err4 = np.array([np.array(i) for i in list_err4])
    list_err1 = list_err1 - min_e
    list_err2 = list_err2 - min_e
    list_err3 = list_err3 - min_e
    list_err4 = list_err4 - min_e
    for i in range(len(list_err1)):
        plt.plot(np.cumsum(list_time1[i]), list_err1[i], 'b-', linewidth=.3)
    for i in range(len(list_err2)):
        plt.plot(np.cumsum(list_time2[i]), list_err2[i], 'r-', linewidth=.3)
    for i in range(len(list_err3)):
        plt.plot(np.cumsum(list_time3[i]), list_err3[i], 'y-', linewidth=.3)
    for i in range(len(list_err4)):
        plt.plot(np.cumsum(list_time4[i]), list_err4[i], 'g-', linewidth=.3)
    n_max1 = len(max(list_err1, key=len))  # length of the longest error
    n_max2 = len(max(list_err2, key=len))
    n_max3 = len(max(list_err3, key=len))
    n_max4 = len(max(list_err4, key=len))
    mat1 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max1 - len(i)) for i in list_err1])
    mat2 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max2 - len(i)) for i in list_err2])
    mat3 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max3 - len(i)) for i in list_err3])
    mat4 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max4 - len(i)) for i in list_err4])

    t_max1 = len(max(list_time1, key=len))
    t_max2 = len(max(list_time2, key=len))
    t_max3 = len(max(list_time3, key=len))
    t_max4 = len(max(list_time4, key=len))
    mat_time1 = np.array([i + [0] * (t_max1 - len(i)) for i in list_time1])
    mat_time2 = np.array([i + [0] * (t_max2 - len(i)) for i in list_time2])
    mat_time3 = np.array([i + [0] * (t_max3 - len(i)) for i in list_time3])
    mat_time4 = np.array([i + [0] * (t_max4 - len(i)) for i in list_time4])
    # plot

    plt.plot(np.cumsum(np.median(mat_time1, axis=0)),
             np.median(mat1, axis=0),
             'b-',
             linewidth=3,
             label="her als")
    plt.plot(np.cumsum(np.median(mat_time2, axis=0)),
             np.median(mat2, axis=0),
             'r-',
             linewidth=3,
             label="als")
    plt.plot(np.cumsum(np.median(mat_time3, axis=0)),
             np.median(mat3, axis=0),
             'y-',
             linewidth=3,
             label="CPRAND")
    plt.plot(np.cumsum(np.median(mat_time4, axis=0)),
             np.median(mat4, axis=0),
             'g-',
             linewidth=3,
             label="herCPRAND")
    plt.yscale("log")
    plt.xlabel('time')
    plt.ylabel('data fitting error')
    plt.legend(loc='best')
    plt.title('Simple case exact')
Exemplo n.º 7
0
def comparaison(I,
                J,
                K,
                r,
                nb_rand,
                n_samples,
                exact_err=False,
                list_factors=False,
                scale=False):
    """
    plot data fitting/factors error over nb_rand noised I*J*K rank r random tensors,
    with the median value in bold.
    x aixs is it.
    For each tensor, we have 5 random factors initializations.
    Need to change plot x,y label and title to run the test.

    Parameters
    ----------
    I : int
        dimension of mode 1.
    J : int
        dimension of mode 2.
    K : int
        dimension of mode 3.
    r : int
        rank.
    nb_rand : int
        nb of tensors.
    n_samples : int
        sample size used for (her)CPRAND.
    exact_err : boolean, optional
        whether use exact error computation or not for (her)CPRAND. The default is False.
    list_factors : boolean, optional
        whether compute factor error or data fitting error. The default is False.
    scale : boolean, optional
        whether to scale the singular values of matrices or not. The default is False.

    Returns
    -------
    None.

  """
    list_err1 = []
    list_err2 = []
    list_err3 = []
    list_err4 = []
    list_pct = []
    min_e = None
    for i in range(nb_rand):
        # Random initialization of a noised cp_tensor
        # np.random.seed(i)
        A, B, C, noise = init_factors(I, J, K, r, scale)
        fac_true = [A, B, C]
        t = tl.cp_to_tensor((None, fac_true)) + noise
        norm_tensor = tl.norm(t, 2)
        if (min_e == None): min_e = norm_tensor
        for k in range(5):  # 5 initializations
            factors = random_init_fac(t, r)
            if list_factors == False:
                weights4, factors4, it4, error4, _, cpt1 = her_CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=100)
                weights1, factors1, it1, error1, cpt = her_Als(
                    t, r, factors=copy.deepcopy(factors), it_max=500)
                weights3, factors3, it3, error3, _ = CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=100)
                weights2, factors2, it2, error2 = als(
                    t, r, factors=copy.deepcopy(factors), it_max=500)
                error1 = [i * norm_tensor for i in error1]
                error2 = [i * norm_tensor for i in error2]
                error3 = [i * norm_tensor for i in error3]
                error4 = [i * norm_tensor for i in error4]
            else:
                weights4, factors4, it4, error, _, cpt1, l4 = her_CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=100,
                    list_factors=list_factors)
                weights1, factors1, it1, error, cpt, l1 = her_Als(
                    t,
                    r,
                    factors=copy.deepcopy(factors),
                    it_max=500,
                    list_factors=list_factors)
                weights3, factors3, it3, error, _, l3 = CPRAND(
                    t,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    exact_err=True,
                    it_max=500,
                    err_it_max=100,
                    list_factors=list_factors)
                weights2, factors2, it2, error, l2 = als(
                    t,
                    r,
                    factors=copy.deepcopy(factors),
                    it_max=500,
                    list_factors=list_factors)
                error1 = [err_fac(fac_true, i) for i in l1]
                error2 = [err_fac(fac_true, i) for i in l2]
                error3 = [err_fac(fac_true, i) for i in l3]
                error4 = [err_fac(fac_true, i) for i in l4]
            list_pct.append(cpt1)
            if (min_e > min(min(error1), min(error2), min(error3),
                            min(error4))):
                min_e = min(min(error1), min(error2), min(error3), min(error4))
            list_err1.append(error1)
            list_err2.append(error2)
            list_err3.append(error3)
            list_err4.append(error4)
    list_err1 = np.array([np.array(i) for i in list_err1])
    list_err2 = np.array([np.array(i) for i in list_err2])
    list_err3 = np.array([np.array(i) for i in list_err3])
    list_err4 = np.array([np.array(i) for i in list_err4])
    list_err1 = list_err1 - min_e
    list_err2 = list_err2 - min_e
    list_err3 = list_err3 - min_e
    list_err4 = list_err4 - min_e
    for i in list_err1:
        plt.plot(range(len(i)), i, 'b-', linewidth=.3)
    for i in list_err2:
        plt.plot(range(len(i)), i, 'r-', linewidth=.3)
    for i in list_err3:
        plt.plot(range(len(i)), i, 'y-', linewidth=.3)
    for i in list_err4:
        plt.plot(range(len(i)), i, 'g-', linewidth=.3)
    n_max1 = len(max(list_err1, key=len))  # length of the longest error
    n_max2 = len(max(list_err2, key=len))
    n_max3 = len(max(list_err3, key=len))
    n_max4 = len(max(list_err4, key=len))
    mat1 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max1 - len(i)) for i in list_err1])
    mat2 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max2 - len(i)) for i in list_err2])
    mat3 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max3 - len(i)) for i in list_err3])
    mat4 = np.array(
        [i.tolist() + [i[len(i) - 1]] * (n_max4 - len(i)) for i in list_err4])

    # plot
    plt.plot(range(n_max1),
             np.median(mat1, axis=0),
             'b-',
             linewidth=3,
             label="her_als")
    plt.plot(range(n_max2),
             np.median(mat2, axis=0),
             'r-',
             linewidth=3,
             label="als")
    plt.plot(range(n_max3),
             np.median(mat3, axis=0),
             'y-',
             linewidth=3,
             label="CPRAND")
    plt.plot(range(n_max4),
             np.median(mat4, axis=0),
             'g-',
             linewidth=3,
             label="herCPRAND")
    plt.yscale("log")
    plt.xlabel('it')
    plt.ylabel('data fitting error')
    plt.legend(loc='best')
    plt.title('Complicated case')
Exemplo n.º 8
0
def test_err_fast():
    """
    Test err_fast for 3 tensors.
    plot the terminaison criterion (obtained by err_fast) and exact error.
    """
    # create a kruskal tensor
    # factor matrices
    A = np.arange(9).reshape(3, 3)
    B = np.arange(6).reshape(2, 3) + 9
    C = np.arange(6).reshape(2, 3) + 15
    factors = []
    factors += [A]
    factors += [B]
    factors += [C]
    t_krus = tl.cp_to_tensor((None, factors))
    weights, factors, it, error, l = als(t_krus, 3, list_factors=True)
    err_ex = []
    for i in l:
        err_ex += [err(t_krus, weights, i)]
    plt.figure(0)
    plt.plot(range(len(err_ex)), err_ex / tl.norm(t_krus), 'b-', label="exact")
    plt.plot(range(len(error)), error, 'r--', label="err fast")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('als for t_krus')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')

    # create an complicated random tensor
    I = 50
    J = 50
    K = 50
    r = 10  # rank
    fac_true, noise = init_factors(I, J, K, r, True)
    t = tl.cp_to_tensor((None, fac_true)) + noise
    weights, factors, it, error, l = als(t, r, list_factors=True)
    err_ex = []
    for i in l:
        err_ex += [err(t, weights, i)]
    plt.figure(1)
    plt.plot(range(len(err_ex)), err_ex / tl.norm(t), 'b-', label="exact")
    plt.plot(range(len(error)), error, 'r--', label="err fast")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('als for complicated case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')

    # create a simple random tensor
    fac_true, noise = init_factors(I, J, K, r, False)
    t = tl.cp_to_tensor((None, fac_true)) + noise
    weights, factors, it, error, l = als(t, r, list_factors=True)
    err_ex = []
    for i in l:
        err_ex += [err(t, weights, i)]
    plt.figure(2)
    plt.plot(range(len(err_ex)), err_ex / tl.norm(t), 'b-', label="exact")
    plt.plot(range(len(error)), error, 'r--', label="err fast")
    plt.xlabel('it')
    plt.yscale('log')
    plt.title('als for simple case')
    plt.ylabel('terminaison criterion')
    plt.legend(loc='best')
Exemplo n.º 9
0
def nn_comparison(I,
                  J,
                  K,
                  r,
                  nb_rand,
                  n_samples,
                  n_samples_err,
                  exact_err=False,
                  scale=False,
                  noise_level=0.1,
                  tol=0.10):
    """
    boxplot for fits, scores, it, time, restarts.
    We generate nb_rand noised I*J*K rank r random tensors, for each tensor, we have 5 factors initializations.
    Then we run the 4 nn algorithms.

    Parameters
    ----------
    I : int
        dimension of mode 1.
    J : int
        dimension of mode 2.
    K : int
        dimension of mode 3.
    r : int
        rank.
    nb_rand : int
        nb of tensors.
    n_samples : int
        sample size used for (her)CPRAND.
    n_samples_err : int
        sample size used for error in (her)cprand
    exact_err : boolean, optional
        whether use exact error computation or not for (her)CPRAND. The default is False.
    scale : boolean, optional
        whether to scale the singular values of matrices or not. The default is False.
    
    Returns
    -------
    None.

  """

    fit = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    score_tot = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    it_tot = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    time_tot = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    restart = {"herals": [], "hercprand": [], "hercprand small": []}
    # local variables
    error = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    l_fac = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    it = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0}
    time = {1: [], 2: [], 3: [], 4: [], 5: [], 6: []}
    for i in range(nb_rand):
        # Random initialization of a noised cp_tensor
        fac_true, noise = init_factors(I, J, K, r, noise_level, scale, nn=True)
        t = tl.cp_to_tensor((None, fac_true)) + noise
        for k in range(5):
            # random initialization of factors
            factors = random_init_fac(t, r)
            # run 4 methods
            weights1, l_fac[1], it[1], error[1], cpt1, l_fac1, time[
                1] = nn_her_Als(t,
                                r,
                                factors=copy.deepcopy(factors),
                                it_max=200,
                                tol=tol,
                                list_factors=True,
                                time_rec=True)
            weights2, l_fac[2], it[2], error[2], l_fac2, time[2] = nn_als(
                t,
                r,
                factors=copy.deepcopy(factors),
                it_max=200,
                tol=tol,
                list_factors=True,
                time_rec=True)
            weights3, l_fac[3], it[3], error[3], l_fac3, time[3] = nn_CPRAND(
                t,
                r,
                n_samples,
                n_samples_err,
                factors=copy.deepcopy(factors),
                exact_err=exact_err,
                it_max=200,
                err_it_max=200,
                tol=tol,
                list_factors=True,
                time_rec=True)
            weights4, l_fac[4], it[4], error[4], cpt4, l_fac4, time[
                4] = nn_her_CPRAND(t,
                                   r,
                                   n_samples,
                                   n_samples_err,
                                   factors=copy.deepcopy(factors),
                                   exact_err=exact_err,
                                   it_max=200,
                                   err_it_max=200,
                                   tol=tol,
                                   list_factors=True,
                                   time_rec=True)
            weights5, l_fac[5], it[5], error[5], l_fac5, time[5] = nn_CPRAND(
                t,
                r,
                100,
                n_samples_err,
                factors=copy.deepcopy(factors),
                exact_err=exact_err,
                it_max=200,
                err_it_max=200,
                tol=tol,
                list_factors=True,
                time_rec=True)
            weights6, l_fac[6], it[6], error[6], cpt6, l_fac6, time[
                6] = nn_her_CPRAND(t,
                                   r,
                                   100,
                                   n_samples_err,
                                   factors=copy.deepcopy(factors),
                                   exact_err=exact_err,
                                   it_max=200,
                                   err_it_max=200,
                                   tol=tol,
                                   list_factors=True,
                                   time_rec=True)
            # information storage
            restart["herals"].append(cpt1)
            restart["hercprand"].append(cpt4)
            restart["hercprand small"].append(cpt6)
            for j in range(1, 7):
                fit[j].append(1 - (error[j][len(error[j]) - 1]))
                score_tot[j].append(score(fac_true, l_fac[j]))
                it_tot[j].append(it[j])
                time_tot[j].append(np.cumsum(time[j])[len(time[j]) - 1])
    # figure
    labels = [
        "herals", "als", "cprand", "hercprand", "cprand small",
        "hercprand small"
    ]
    _, dataf = [*zip(*fit.items())]
    _, datas = [*zip(*score_tot.items())]
    _, datai = [*zip(*it_tot.items())]
    _, datat = [*zip(*time_tot.items())]
    _, datar = [*zip(*restart.items())]
    plt.figure(0)
    plt.boxplot(dataf, vert=False)
    plt.yticks(range(1, len(labels) + 1), labels)
    plt.title('fits')
    plt.figure(1)
    plt.boxplot(datas, vert=False)
    plt.yticks(range(1, len(labels) + 1), labels)
    plt.title('scores')
    plt.figure(2)
    plt.boxplot(datai, vert=False)
    plt.yticks(range(1, len(labels) + 1), labels)
    plt.title('it')
    plt.figure(3)
    plt.boxplot(datat, vert=False)
    plt.yticks(range(1, len(labels) + 1), labels)
    plt.title('time')
    plt.figure(4)
    plt.boxplot(datar, vert=False)
    plt.yticks(range(1, 4), ["herals", "hercprand", "hercprand small"])
    plt.title('restarts')
Exemplo n.º 10
0
def param_research(I,
                   J,
                   K,
                   r,
                   nb_rand,
                   n_samples,
                   exact_err=True,
                   beta=True,
                   eta=False,
                   gamma=False):
    """
    plot data fitting error for different values of beta, eta and gamma.
    For each parameter (beta for example), we initialize nb_rand noised I*J*K rank r 
    random tensors.
    For each tensor, we have 5 random factors initializations.
    Need to change plot label to run the test.

    Parameters
    ----------
    I : int
        dimension of mode 1.
    J : int
        dimension of mode 2.
    K : int
        dimension of mode 3.
    r : int
        rank.
    nb_rand : int
        nb of tensors.
    n_samples : int
        sample size used for herCPRAND.
    exact_err : boolean, optional
        whether use exact error computation or not for herCPRAND. The default is True.
    beta : boolean, optional
        plot figure for different values of beta. The default is True.
    eta : boolean, optional
        plot figure for different values of eta. The default is False.
    gamma : boolean, optional
        plot figure for different values of gamma. The default is False.

    Returns
    -------
    None.

  """
    list_err1 = []
    list_err2 = []
    list_err3 = []
    min_e = None
    for i in range(nb_rand):
        # Random initialization of a noised cp_tensor
        factors, noise = init_factors(I, J, K, r, scale=True, nn=False)
        tensor = tl.cp_to_tensor((None, factors)) + noise
        norm_tensor = tl.norm(tensor, 2)
        if (min_e == None): min_e = norm_tensor
        for j in range(5):
            factors = random_init_fac(tensor, r)
            # parameter choice
            if (beta == True):
                weights1, factors1, it1, error1, cpt1 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=400,
                    exact_err=exact_err,
                    err_it_max=100,
                    beta=0.01)  # beta0=0.1 bien
                weights2, factors2, it2, error2, cpt2 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=400,
                    exact_err=exact_err,
                    err_it_max=100,
                    beta=0.5)  # beta0=0.3
                weights3, factors3, it3, error3, cpt3 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=400,
                    exact_err=exact_err,
                    err_it_max=100,
                    beta=0.99)  # beta0=0.5
            if (eta == True):
                weights1, factors1, it1, error1, cpt1 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=400,
                    exact_err=exact_err,
                    err_it_max=100,
                    eta=1.1)  # eta=1.1
                weights2, factors2, it2, error2, cpt2 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=400,
                    exact_err=exact_err,
                    err_it_max=100,
                    eta=3)  # eta=2
                weights3, factors3, it3, error3, cpt3 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=400,
                    exact_err=exact_err,
                    err_it_max=100,
                    eta=5)  # eta=3
            if (gamma == True):
                weights1, factors1, it1, error1, cpt1 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=100,
                    exact_err=exact_err,
                    err_it_max=100,
                    gamma=1.01,
                    gamma_bar=1.005)
                weights2, factors2, it2, error2, cpt2 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=100,
                    exact_err=exact_err,
                    err_it_max=100,
                    gamma=1.05,
                    gamma_bar=1.01)
                weights3, factors3, it3, error3, cpt3 = her_CPRAND(
                    tensor,
                    r,
                    n_samples,
                    factors=copy.deepcopy(factors),
                    it_max=100,
                    exact_err=exact_err,
                    err_it_max=100,
                    gamma=1.9,
                    gamma_bar=1.5)
            error1 = [i * norm_tensor for i in error1]
            list_err1.append(error1)
            error2 = [i * norm_tensor for i in error2]
            list_err2.append(error2)
            error3 = [i * norm_tensor for i in error3]
            list_err3.append(error3)
            if (min_e > min(min(error1), min(error2), min(error3))):
                min_e = min(min(error1), min(error2), min(error3))
    list_err1 = [x - min_e for x in list_err1]
    list_err2 = [x - min_e for x in list_err2]
    list_err3 = [x - min_e for x in list_err3]
    # plot
    for i in range(len(list_err1)):
        if i == 0:
            plt.plot(range(len(list_err1[i])),
                     list_err1[i],
                     'b-',
                     label='beta=0.01')
        else:
            plt.plot(range(len(list_err1[i])), list_err1[i], 'b-')

    for i in range(len(list_err2)):
        if i == 0:
            plt.plot(range(len(list_err2[i])),
                     list_err2[i],
                     'r-',
                     label='beta=0.5')
        else:
            plt.plot(range(len(list_err2[i])), list_err2[i], 'r-')

    for i in range(len(list_err3)):
        if i == 0:
            plt.plot(range(len(list_err3[i])),
                     list_err3[i],
                     'g-',
                     label='beta=0.99')
        else:
            plt.plot(range(len(list_err3[i])), list_err3[i], 'g-')
    plt.yscale("log")
    plt.legend(loc='best')
    plt.xlabel('it')
    plt.ylabel('f')
    plt.title('f(iteration)')