Пример #1
0
def test_simple():
    Z = np.random.standard_normal((10,10)) * 4
    p = rr.l1_l2((10,10), lagrange=0.13)
    dual = p.conjugate
    L = 0.23

    loss = rr.quadratic.shift(-Z, coef=L)
    problem = rr.simple_problem(loss, p)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-10, debug=True)

    simple_coef = solver.composite.coefs
    q = rr.identity_quadratic(L, Z, 0, 0)
    prox_coef = p.proximal(q)

    p2 = copy(p)
    p2.quadratic = rr.identity_quadratic(L, Z, 0, 0)
    problem = rr.simple_problem.nonsmooth(p2)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-14, debug=True)
    simple_nonsmooth_coef = solver.composite.coefs

    p = rr.l1_l2((10,10), lagrange=0.13)
    p.quadratic = rr.identity_quadratic(L, Z, 0, 0)
    problem = rr.simple_problem.nonsmooth(p)
    simple_nonsmooth_gengrad = gengrad(problem, L, tol=1.0e-10)

    p = rr.l1_l2((10,10), lagrange=0.13)
    problem = rr.separable_problem.singleton(p, loss)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-10)
    separable_coef = solver.composite.coefs

    ac(prox_coef, Z-simple_coef, 'prox to simple')
    ac(prox_coef, simple_nonsmooth_gengrad, 'prox to nonsmooth gengrad')
    ac(prox_coef, separable_coef, 'prox to separable')
    ac(prox_coef, simple_nonsmooth_coef, 'prox to simple_nonsmooth')
Пример #2
0
def test_simple():
    Z = np.random.standard_normal((10, 10)) * 4
    p = rr.l1_l2((10, 10), lagrange=0.13)
    dual = p.conjugate
    L = 0.23

    loss = rr.quadratic.shift(-Z, coef=L)
    problem = rr.simple_problem(loss, p)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-10, debug=True)

    simple_coef = solver.composite.coefs
    q = rr.identity_quadratic(L, Z, 0, 0)
    prox_coef = p.proximal(q)

    p2 = copy(p)
    p2.quadratic = rr.identity_quadratic(L, Z, 0, 0)
    problem = rr.simple_problem.nonsmooth(p2)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-14, debug=True)
    simple_nonsmooth_coef = solver.composite.coefs

    p = rr.l1_l2((10, 10), lagrange=0.13)
    p.quadratic = rr.identity_quadratic(L, Z, 0, 0)
    problem = rr.simple_problem.nonsmooth(p)
    simple_nonsmooth_gengrad = gengrad(problem, L, tol=1.0e-10)

    p = rr.l1_l2((10, 10), lagrange=0.13)
    problem = rr.separable_problem.singleton(p, loss)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-10)
    separable_coef = solver.composite.coefs

    ac(prox_coef, Z - simple_coef, 'prox to simple')
    ac(prox_coef, simple_nonsmooth_gengrad, 'prox to nonsmooth gengrad')
    ac(prox_coef, separable_coef, 'prox to separable')
    ac(prox_coef, simple_nonsmooth_coef, 'prox to simple_nonsmooth')
Пример #3
0
def test_quadratic_for_smooth():
    """
    this test is a check to ensure that the
    quadratic part of the smooth functions are being used in the proximal step

    """

    L = 0.45

    W = np.random.standard_normal(40)
    Z = np.random.standard_normal(40)
    U = np.random.standard_normal(40)

    atomq = rr.identity_quadratic(0.4, U, W, 0)
    atom = rr.l1norm(40, quadratic=atomq, lagrange=0.12)

    # specifying in this way should be the same as if we put 0.5*L below
    loss = rr.quadratic.shift(-Z, coef=0.6 * L)
    lq = rr.identity_quadratic(0.4 * L, Z, 0, 0)
    loss.quadratic = lq

    ww = np.random.standard_normal(40)

    # specifying in this way should be the same as if we put 0.5*L below
    loss2 = rr.quadratic.shift(-Z, coef=L)
    yield ac, loss2.objective(ww), loss.objective(ww), "checking objective"

    yield ac, lq.objective(ww, "func"), loss.nonsmooth_objective(ww), "checking nonsmooth objective"
    yield ac, loss2.smooth_objective(ww, "func"), 0.5 / 0.3 * loss.smooth_objective(
        ww, "func"
    ), "checking smooth objective func"
    yield ac, loss2.smooth_objective(ww, "grad"), 0.5 / 0.3 * loss.smooth_objective(
        ww, "grad"
    ), "checking smooth objective grad"

    problem = rr.container(loss, atom)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-12)

    problem3 = rr.simple_problem(loss, atom)
    solver3 = rr.FISTA(problem3)
    solver3.fit(tol=1.0e-12, coef_stop=True)

    loss4 = rr.quadratic.shift(-Z, coef=0.6 * L)
    problem4 = rr.simple_problem(loss4, atom)
    problem4.quadratic = lq
    solver4 = rr.FISTA(problem4)
    solver4.fit(tol=1.0e-12)

    gg_soln = rr.gengrad(problem4, L)

    loss6 = rr.quadratic.shift(-Z, coef=0.6 * L)
    loss6.quadratic = lq + atom.quadratic
    atomcp = copy(atom)
    atomcp.quadratic = rr.identity_quadratic(0, 0, 0, 0)
    problem6 = rr.dual_problem(loss6.conjugate, rr.identity(loss6.primal_shape), atomcp.conjugate)
    problem6.lipschitz = L + atom.quadratic.coef
    dsoln2 = problem6.solve(coef_stop=True, tol=1.0e-10, max_its=100)

    problem2 = rr.container(loss2, atom)
    solver2 = rr.FISTA(problem2)
    solver2.fit(tol=1.0e-12, coef_stop=True)

    q = rr.identity_quadratic(L, Z, 0, 0)

    ac(problem.objective(ww), atom.nonsmooth_objective(ww) + q.objective(ww, "func"))

    aq = atom.solve(q)
    for p, msg in zip(
        [
            solver3.composite.coefs,
            gg_soln,
            solver2.composite.coefs,
            solver4.composite.coefs,
            dsoln2,
            solver.composite.coefs,
        ],
        [
            "simple_problem with loss having no quadratic",
            "gen grad",
            "container with loss having no quadratic",
            "simple_problem container with quadratic",
            "dual problem with loss having a quadratic",
            "container with loss having a quadratic",
        ],
    ):
        yield ac, aq, p, msg
Пример #4
0
def test_quadratic_for_smooth():
    '''
    this test is a check to ensure that the
    quadratic part of the smooth functions are being used in the proximal step

    '''

    L = 0.45

    W = np.random.standard_normal(40)
    Z = np.random.standard_normal(40)
    U = np.random.standard_normal(40)

    atomq = rr.identity_quadratic(0.4, U, W, 0)
    atom = rr.l1norm(40, quadratic=atomq, lagrange=0.12)

    # specifying in this way should be the same as if we put 0.5*L below
    loss = rr.quadratic.shift(-Z, coef=0.6*L)
    lq = rr.identity_quadratic(0.4*L, Z, 0, 0)
    loss.quadratic = lq 

    ww = np.random.standard_normal(40)

    # specifying in this way should be the same as if we put 0.5*L below
    loss2 = rr.quadratic.shift(-Z, coef=L)
    yield ac, loss2.objective(ww), loss.objective(ww), 'checking objective'

    yield ac, lq.objective(ww, 'func'), loss.nonsmooth_objective(ww), 'checking nonsmooth objective'
    yield ac, loss2.smooth_objective(ww, 'func'), 0.5 / 0.3 * loss.smooth_objective(ww, 'func'), 'checking smooth objective func'
    yield ac, loss2.smooth_objective(ww, 'grad'), 0.5 / 0.3 * loss.smooth_objective(ww, 'grad'), 'checking smooth objective grad'

    problem = rr.container(loss, atom)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-12)

    problem3 = rr.simple_problem(loss, atom)
    solver3 = rr.FISTA(problem3)
    solver3.fit(tol=1.0e-12, coef_stop=True)

    loss4 = rr.quadratic.shift(-Z, coef=0.6*L)
    problem4 = rr.simple_problem(loss4, atom)
    problem4.quadratic = lq
    solver4 = rr.FISTA(problem4)
    solver4.fit(tol=1.0e-12)

    gg_soln = rr.gengrad(problem4, L)

    loss6 = rr.quadratic.shift(-Z, coef=0.6*L)
    loss6.quadratic = lq + atom.quadratic
    atomcp = copy(atom)
    atomcp.quadratic = rr.identity_quadratic(0,0,0,0)
    problem6 = rr.dual_problem(loss6.conjugate, rr.identity(loss6.shape), atomcp.conjugate)
    problem6.lipschitz = L + atom.quadratic.coef
    dsoln2 = problem6.solve(coef_stop=True, tol=1.e-10, 
                            max_its=100)

    problem2 = rr.container(loss2, atom)
    solver2 = rr.FISTA(problem2)
    solver2.fit(tol=1.0e-12, coef_stop=True)

    q = rr.identity_quadratic(L, Z, 0, 0)

    ac(problem.objective(ww), atom.nonsmooth_objective(ww) + q.objective(ww,'func'))

    aq = atom.solve(q)
    for p, msg in zip([solver3.composite.coefs,
                       gg_soln,
                       solver2.composite.coefs,
                       solver4.composite.coefs,
                       dsoln2,
                       solver.composite.coefs],
                      ['simple_problem with loss having no quadratic',
                       'gen grad',
                       'container with loss having no quadratic',
                       'simple_problem container with quadratic',
                       'dual problem with loss having a quadratic',
                       'container with loss having a quadratic']):
        yield ac, aq, p, msg
Пример #5
0
def test_quadratic_for_smooth2():
    '''
    this test is a check to ensure that the
    quadratic part of the smooth functions are being used in the proximal step

    '''

    L = 2

    W = np.arange(5)
    Z = 0.5 * np.arange(5)[::-1]
    U = 1.5 * np.arange(5)

    atomq = rr.identity_quadratic(0.4, U, W, 0)
    atom = rr.l1norm(5, quadratic=atomq, lagrange=0.1)

    # specifying in this way should be the same as if we put 0.5*L below
    loss = rr.quadratic.shift(-Z, coef=0.6*L)
    lq = rr.identity_quadratic(0.4*L, Z, 0, 0)
    loss.quadratic = lq 

    ww = np.ones(5)

    # specifying in this way should be the same as if we put 0.5*L below
    loss2 = rr.quadratic.shift(-Z, coef=L)
    np.testing.assert_allclose(loss2.objective(ww), loss.objective(ww))
    np.testing.assert_allclose(lq.objective(ww, 'func'), loss.nonsmooth_objective(ww))
    np.testing.assert_allclose(loss2.smooth_objective(ww, 'func'), 0.5 / 0.3 * loss.smooth_objective(ww, 'func'))
    np.testing.assert_allclose(loss2.smooth_objective(ww, 'grad'), 0.5 / 0.3 * loss.smooth_objective(ww, 'grad'))

    problem = rr.container(loss, atom)
    solver = rr.FISTA(problem)
    solver.fit(tol=1.0e-12)

    problem3 = rr.simple_problem(loss, atom)
    solver3 = rr.FISTA(problem3)
    solver3.fit(tol=1.0e-12, coef_stop=True)

    loss4 = rr.quadratic.shift(-Z, coef=0.6*L)
    problem4 = rr.simple_problem(loss4, atom)
    problem4.quadratic = lq
    solver4 = rr.FISTA(problem4)
    solver4.fit(tol=1.0e-12)

    gg_soln = rr.gengrad(problem4, L)

    loss6 = rr.quadratic.shift(-Z, coef=0.6*L)
    loss6.quadratic = lq + atom.quadratic
    atomcp = copy(atom)
    atomcp.quadratic = rr.identity_quadratic(0,0,0,0)
    problem6 = rr.dual_problem(loss6.conjugate, rr.identity(loss6.shape), atomcp.conjugate)
    problem6.lipschitz = L + atom.quadratic.coef
    dsoln2 = problem6.solve(coef_stop=True, tol=1.e-10, 
                            max_its=100)

    problem2 = rr.container(loss2, atom)
    solver2 = rr.FISTA(problem2)
    solver2.fit(tol=1.0e-12, coef_stop=True)

    q = rr.identity_quadratic(L, Z, 0, 0)

    ac(problem.objective(ww), atom.nonsmooth_objective(ww) + q.objective(ww,'func'))

    aq = atom.solve(q)
    for p, msg in zip([solver3.composite.coefs,
                       gg_soln,
                       solver2.composite.coefs,
                       solver4.composite.coefs,
                       dsoln2,
                       solver.composite.coefs],
                      ['simple_problem with loss having no quadratic',
                       'gen grad',
                       'container with loss having no quadratic',
                       'simple_problem container with quadratic',
                       'dual problem with loss having a quadratic',
                       'container with loss having a quadratic']):
        yield ac, aq, p, msg