Ejemplo n.º 1
0
def test_lasso(n=100):

    l1 = 1.
    sparsity1 = l1norm(n, l=l1*0.75)
    sparsity2 = l1norm(n, l=l1*0.25)
    sparsity = l1norm(n, l=l1)
    
    X = np.random.standard_normal((5000,n))
    Y = np.random.standard_normal((5000,))
    regloss = squaredloss(X,Y)


    #p=regloss.add_seminorm(sparsity)
    #p=regloss.add_seminorm(seminorm(sparsity1,sparsity2),initial=np.zeros(n))
    p=regloss.add_seminorm(seminorm(sparsity),initial=np.zeros(n))
    solver=FISTA(p)
    solver.debug = True
    t1 = time.time()
    vals1 = solver.fit(max_its=800,tol=1e-18,set_prox_tol=True)
    t2 = time.time()
    dt1 = t2 - t1
    soln = solver.problem.coefs

    time.sleep(5)


    p2 = lasso.gengrad((X, Y))#,initial_coefs = np.random.normal(0,1,n))
    p2.assign_penalty(l1=l1)
    opt = FISTA(p2)
    opt.debug = True
    t1 = time.time()
    vals2 = opt.fit(tol=1e-18,max_its=800)
    t2 = time.time()
    dt2 = t2 - t1
    beta = opt.problem.coefs


    print soln[range(10)]
    print beta[range(10)]

    print p.obj(soln), p.obj(beta)
    print p2.obj(soln), p2.obj(beta)
    print "Times", dt1, dt2
    
    return [vals1, vals2]
Ejemplo n.º 2
0
def lasso_example(compare=False):

    l1 = 20.
    sparsity = l1norm(500, l=l1 / 2.)
    X = np.random.standard_normal((1000, 500))
    Y = np.random.standard_normal((1000, ))
    regloss = squaredloss(X, Y)
    sparsity2 = l1norm(500, l=l1 / 2.)
    #p=regloss.add_seminorm(sparsity)
    p = regloss.add_seminorm(seminorm(sparsity, sparsity2))
    solver = FISTA(p)
    solver.debug = True
    vals = solver.fit(max_its=2000, min_its=100)
    soln = solver.problem.coefs

    if not compare:
        # solution
        pylab.figure(num=1)
        pylab.clf()
        pylab.plot(soln, c='g')

        # objective values
        pylab.figure(num=2)
        pylab.clf()
        pylab.plot(vals)
    else:
        p2 = lasso.gengrad((X, Y))
        p2.assign_penalty(l1=l1)
        opt = FISTA(p2)
        opt.debug = True
        opt.fit(tol=1e-10, max_its=5000)
        beta = opt.problem.coefs
        print "Terminal error with seminorm:", np.min(
            vals), "\tTerminal error with lasso", p.obj(
                beta), "\nTerminal relative error:", (
                    np.min(vals) - p.obj(beta)) / p.obj(beta)

        pylab.figure(num=1)
        pylab.clf()
        #pylab.plot(soln, c='g')
        pylab.scatter(soln, beta)

        pylab.figure(num=2)
        pylab.clf()
        pylab.plot(vals)
Ejemplo n.º 3
0
def test_1d_fused_lasso(n=100):

    l1 = 1.


    sparsity1 = l1norm(n, l=l1)
    D = (np.identity(n) - np.diag(np.ones(n-1),-1))[1:]
    extra = np.zeros(n)
    extra[0] = 1.
    D = np.vstack([D,extra])
    D = sparse.csr_matrix(D)

    fused = seminorm(l1norm(D, l=l1))

    X = np.random.standard_normal((2*n,n))
    Y = np.random.standard_normal((2*n,))
    regloss = squaredloss(X,Y)
    p=regloss.add_seminorm(fused)
    solver=FISTA(p)
    solver.debug = True
    vals1 = solver.fit(max_its=25000, tol=1e-12)
    soln1 = solver.problem.coefs

    B = np.array(sparse.tril(np.ones((n,n))).todense())
    X2 = np.dot(X,B)

    time.sleep(3)
    
    D2 = np.diag(np.ones(n))
    p2 = lasso.gengrad((X2, Y))
    p2.assign_penalty(l1=l1)
    opt = FISTA(p2)
    opt.debug = True
    opt.fit(tol=1e-12,max_its=25000)
    beta = opt.problem.coefs
    soln2 = np.dot(B,beta)

    print soln1[range(10)]
    print soln2[range(10)]
    print p.obj(soln1), p.obj(soln2)
    #np.testing.assert_almost_equal(soln1,soln2)

    return vals1
Ejemplo n.º 4
0
def lasso_example(compare=False):

    l1 = 20.
    sparsity = l1norm(500, l=l1/2.)
    X = np.random.standard_normal((1000,500))
    Y = np.random.standard_normal((1000,))
    regloss = squaredloss(X,Y)
    sparsity2 = l1norm(500, l=l1/2.)
    #p=regloss.add_seminorm(sparsity)
    p=regloss.add_seminorm(seminorm(sparsity,sparsity2))
    solver=FISTA(p)
    solver.debug = True
    vals = solver.fit(max_its=2000, min_its = 100)
    soln = solver.problem.coefs

    if not compare:
        # solution
        pylab.figure(num=1)
        pylab.clf()
        pylab.plot(soln, c='g')

        # objective values
        pylab.figure(num=2)
        pylab.clf()
        pylab.plot(vals)
    else:
        p2 = lasso.gengrad((X, Y))
        p2.assign_penalty(l1=l1)
        opt = FISTA(p2)
        opt.debug = True
        opt.fit(tol=1e-10,max_its=5000)
        beta = opt.problem.coefs
        print "Terminal error with seminorm:", np.min(vals), "\tTerminal error with lasso", p.obj(beta) ,"\nTerminal relative error:", (np.min(vals) - p.obj(beta))/p.obj(beta)

        pylab.figure(num=1)
        pylab.clf()
        #pylab.plot(soln, c='g')
        pylab.scatter(soln,beta)
        
        pylab.figure(num=2)
        pylab.clf()
        pylab.plot(vals)
Ejemplo n.º 5
0
def test_lasso(n=100):

    l1 = 1.
    sparsity1 = l1norm(n, l=l1 * 0.75)
    sparsity2 = l1norm(n, l=l1 * 0.25)
    sparsity = l1norm(n, l=l1)

    X = np.random.standard_normal((5000, n))
    Y = np.random.standard_normal((5000, ))
    regloss = squaredloss(X, Y)

    #p=regloss.add_seminorm(sparsity)
    #p=regloss.add_seminorm(seminorm(sparsity1,sparsity2),initial=np.zeros(n))
    p = regloss.add_seminorm(seminorm(sparsity), initial=np.zeros(n))
    solver = FISTA(p)
    solver.debug = True
    t1 = time.time()
    vals1 = solver.fit(max_its=800, tol=1e-18, set_prox_tol=True)
    t2 = time.time()
    dt1 = t2 - t1
    soln = solver.problem.coefs

    time.sleep(5)

    p2 = lasso.gengrad((X, Y))  #,initial_coefs = np.random.normal(0,1,n))
    p2.assign_penalty(l1=l1)
    opt = FISTA(p2)
    opt.debug = True
    t1 = time.time()
    vals2 = opt.fit(tol=1e-18, max_its=800)
    t2 = time.time()
    dt2 = t2 - t1
    beta = opt.problem.coefs

    print soln[range(10)]
    print beta[range(10)]

    print p.obj(soln), p.obj(beta)
    print p2.obj(soln), p2.obj(beta)
    print "Times", dt1, dt2

    return [vals1, vals2]
Ejemplo n.º 6
0
def test_1d_fused_lasso(n=100):

    l1 = 1.

    sparsity1 = l1norm(n, l=l1)
    D = (np.identity(n) - np.diag(np.ones(n - 1), -1))[1:]
    extra = np.zeros(n)
    extra[0] = 1.
    D = np.vstack([D, extra])
    D = sparse.csr_matrix(D)

    fused = seminorm(l1norm(D, l=l1))

    X = np.random.standard_normal((2 * n, n))
    Y = np.random.standard_normal((2 * n, ))
    regloss = squaredloss(X, Y)
    p = regloss.add_seminorm(fused)
    solver = FISTA(p)
    solver.debug = True
    vals1 = solver.fit(max_its=25000, tol=1e-12)
    soln1 = solver.problem.coefs

    B = np.array(sparse.tril(np.ones((n, n))).todense())
    X2 = np.dot(X, B)

    time.sleep(3)

    D2 = np.diag(np.ones(n))
    p2 = lasso.gengrad((X2, Y))
    p2.assign_penalty(l1=l1)
    opt = FISTA(p2)
    opt.debug = True
    opt.fit(tol=1e-12, max_its=25000)
    beta = opt.problem.coefs
    soln2 = np.dot(B, beta)

    print soln1[range(10)]
    print soln2[range(10)]
    print p.obj(soln1), p.obj(soln2)
    #np.testing.assert_almost_equal(soln1,soln2)

    return vals1