Ejemplo n.º 1
0
def Demo_NdLQGAN(root='lqganresults/', Dims=None, Algorithms=None, File=None):
    # Dims = [1]
    Dims = [1,2]
    Nmusigmas = 10
    Lstarts = 10
    Algorithms = dict(zip(['Fsim','EG','Fcon','Freg','Feg','Fcc'],range(6)))
    # Algorithms = dict(zip(['Fcc','Fsim','Feg','Fcon','Freg','EG'],range(6)))
    # Algorithms = dict(zip(['Fcc','Feg','Freg'],range(3)))
    # Algorithms = dict(zip(['Fcc','Feg'],range(2)))
    # Algorithms = dict(zip(['Fcc','Feg','Fccprime','Fegprime'],range(4)))
    # Algorithms = dict(zip(['Fccprime','Fegprime'],range(2)))
    # Algorithms = dict(zip(['Fcc'],range(1)))
    # Algorithms = ['Fcon']
    # Algorithms = dict(zip(['Fcc','Feg','Fcon','Freg','Falt','Funr'],range(6)))

    Iters = 10000

    data = np.empty((len(Dims),Nmusigmas,Lstarts,len(Algorithms),10))

    for i, dim in enumerate(tqdm.tqdm(Dims)):
        Domain = LQGAN(dim=dim, var_only=True)

        # Dimensionality
        s = Domain.s
        pdim = Domain.Dim

        # Set Constraints
        loA = -np.inf*np.ones((dim,dim))
        loA[range(dim),range(dim)] = 1e-2
        lo = np.hstack(([-np.inf]*(dim+s), loA[np.tril_indices(dim)], [-np.inf]*dim))
        P = BoxProjection(lo=lo)

        for musigma in tqdm.tqdm(range(Nmusigmas)):
            # Reset LQGAN to random mu and Sigma
            mu, Sigma = rand_mu_Sigma(dim=dim)
            # print(Sigma.flatten().item())
            mu = np.zeros(dim)
            # Sigma = np.array([[1]])
            # Sigma = np.diag(np.random.rand(dim)*10+1.)
            lams = np.linalg.eigvals(Sigma)
            K = lams.max()/lams.min()
            # print(np.linalg.eigvals(Sigma))
            Domain.set_mu_sigma(mu=mu,sigma=Sigma)
            # print(mu,Sigma)

            for l in tqdm.tqdm(range(Lstarts)):
                # Intialize G and D variables to random starting point
                # should initialize A to square root of random Sigma
                # Start = P.P(10*np.random.rand(pdim)-5.)
                Start = np.zeros(pdim)
                Start[:s] = np.random.rand(s)*10-5
                Start[s:s+dim] = 0.  # set w_1 to zero
                Start[-dim:] = mu  # set b to mu
                Start[-dim-s:-dim] = np.linalg.cholesky(rand_mu_Sigma(dim=dim)[1])[np.tril_indices(dim)]
                Start = P.P(Start)

                # Calculate Initial KL and Euclidean distance
                KL_0 = Domain.dist_KL(Start)
                Euc_0 = Domain.dist_Euclidean(Start)
                norm_F_0 = Domain.norm_F(Start)
                # print(KL_0,Euc_0,norm_F_0)

                for j, alg in enumerate(tqdm.tqdm(Algorithms)):

                    # Step = -1e-3
                    if alg == 'EG':
                        Step = -1e-3
                        Domain.preconditioner = 'Fsim'
                        Method = EG(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Fsim':
                        Step = -1e-4
                        Domain.preconditioner='Fsim'
                        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-3,P=P,MinStep=-1e-2)
                        # Method = Euler(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Fcon':
                        Step = -1e-4
                        Domain.preconditioner = alg
                        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-3,P=P,MinStep=-1e-2)
                        # Method = Euler(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Falt' or alg == 'Funr':
                        Step = -5e-3
                        Domain.preconditioner = alg
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-5,P=P)
                        Method = Euler(Domain=Domain,FixStep=True,P=P)
                    elif alg == 'Fccprime' or alg == 'Fegprime':
                        Step = -1e-1
                        Domain.preconditioner = alg
                        Method = Euler(Domain=Domain,FixStep=True,P=P)
                    else:
                        # Step = -1e-1
                        Step = -1e-4
                        Domain.preconditioner = alg
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-0,P=P,MinStep=-10.)  # for 2d+
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-2,P=P,MinStep=-1e-1) # 13 slow
                        Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-3,P=P,MinStep=-1e-2) # slow
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1,P=P,MinStep=-1e-1) better
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1,P=P,MinStep=-1e-10) slow
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-1,P=P,MinStep=-1e-2) best so far, Feg is not working well?
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-2,P=P,MinStep=-1e-2) ok
                        # Method = HeunEuler_PhaseSpace(Domain=Domain,Delta0=1e-2,P=P,MinStep=-1e-1)
                        # Delta0=1e-2,MinStep=-1e-1 for speed on 1d, scaled versions are surprisingly worse
                    
                    # Set Options
                    Init = Initialization(Step=Step)
                    Tols = [#(Domain.norm_F,1e-3*norm_F_0),
                            # (Domain.dist_KL,1e-3*KL_0),
                            (Domain.dist_Euclidean,1e-3*Euc_0),
                            (Domain.isNotNaNInf,False)]
                    Term = Termination(MaxIter=Iters,Tols=Tols,verbose=False)
                    Repo = Reporting(Requests=[Domain.norm_F, Domain.dist, 
                                               Domain.dist_KL, Domain.dist_Euclidean,
                                               Domain.isNotNaNInf])
                    # 'Step','Data',
                    Misc = Miscellaneous()
                    Options = DescentOptions(Init,Term,Repo,Misc)

                    # Start Solver
                    tic = time.time()
                    LQ_Results = Solve(Start,Method,Domain,Options)
                    toc = time.time() - tic

                    KL = LQ_Results.PermStorage[Domain.dist_KL][-1]
                    Euc = LQ_Results.PermStorage[Domain.dist_Euclidean][-1]
                    norm_F = LQ_Results.PermStorage[Domain.norm_F][-1]
                    # x = np.array(LQ_Results.PermStorage['Data'])
                    # Steps = np.array(LQ_Results.PermStorage['Step'])
                    runtime = toc
                    steps = LQ_Results.thisPermIndex
                    # embed()

                    data[i,musigma,l,j,:] = np.array([dim,musigma,Algorithms[alg],l,K,KL/KL_0,Euc/Euc_0,norm_F/norm_F_0,runtime,steps])
                    # embed()
            np.save(root+'results15.npy', data)
Ejemplo n.º 2
0
if __name__ == '__main__':
    # Creating a random LQGAN
    dim = 2
    s = (dim**2 + dim) // 2
    mu = np.zeros(dim)
    L = 10 * np.random.rand(dim, dim) - 5 + np.diag(5 * np.ones(dim))
    L[range(dim), range(dim)] = np.clip(L[range(dim), range(dim)], 1e-8,
                                        np.inf)
    L = np.tril(L)
    sig = np.dot(L, L.T)
    # sig = np.diag(np.random.rand(dim)/np.sqrt(2.))
    Domain = LQGAN(mu=mu, sig=sig)

    from VISolver.Projection import BoxProjection
    # Set Constraints
    loA = -np.inf * np.ones((dim, dim))
    loA[range(dim), range(dim)] = 1e-2
    lo = np.hstack(
        ([-np.inf] * (dim + s), loA[np.tril_indices(dim)], [-np.inf] * dim))
    P = BoxProjection(lo=lo)

    mx = -1
    for i in range(10000):
        Start = P.P(100 * np.random.rand(Domain.Dim) - 50.)
        jexact = Domain.J(Start)
        japprox = approx_jacobian(Domain._F, Start)
        newmx = np.max(np.abs(jexact - japprox))
        mx = max(mx, newmx)

    print(mx)