def test_proximal_nonsmooth():
    # Minimize ||x||_1.
    oracle = oracles.create_lasso_prox_oracle(np.zeros([2, 2]), 
                                              np.zeros(2), 
                                              regcoef=1.0)
    x_0 = np.array([2.0, -1.0])
    [x_star, status, hist] = optimization.proximal_gradient_descent(
                                oracle, x_0, trace=True)
    eq_(status, 'success')
    ok_(np.allclose(x_star, np.array([0.0, 0.0])))
    ok_(np.allclose(np.array(hist['func']), np.array([3.0, 1.0, 0.0])))
def test_proximal_gd_one_step():
    # Simple smooth quadratic task.
    A = np.eye(2)
    b = np.array([1.0, 0.0])
    oracle = oracles.create_lasso_prox_oracle(A, b, regcoef=0.0)
    x_0 = np.zeros(2)

    [x_star, status, hist] = optimization.proximal_gradient_descent(
                                oracle, x_0, trace=True)
    eq_(status, 'success')
    ok_(np.allclose(x_star, np.array([1.0, 0.0])))
    ok_(np.allclose(np.array(hist['func']), np.array([0.5, 0.0])))
Пример #3
0
def test_proximal_nonsmooth2():
    oracle = oracles.create_lasso_prox_oracle(np.array([[1, 2, 3], [4, 5, 6]]),
                                              np.array([1, 4]),
                                              regcoef=1.0)
    x_0 = np.array([1, 1, -1])
    [x_star, status, hist] = optimization.proximal_gradient_descent(oracle,
                                                                    x_0,
                                                                    trace=True,
                                                                    max_iter=3)
    eq_(status, 'iterations_exceeded')
    ok_(np.allclose(x_star, np.array([1.02216721, 1.05131721, -0.85703278])))
    ok_(
        np.allclose(
            np.array(hist['func']),
            np.array(
                [[4.0, 3.219970703125, 3.1220934763550758,
                  3.0507238902373501]])))
Пример #4
0
        x_star, msg, hist = optimization.subgradient_method(oracle, x_0, alpha_0=a_0, trace=True, max_iter=10**4)
        res.append(len(hist['func']))
    plt.plot([a / 10 for a in range(1, 50)], res)
    plt.xlabel("Alpha")
    plt.ylabel("Iterations")
    plt.savefig("exp1/{}.png".format(label))

# ------------------ Experiment 2

for n in [5, 50]:
    A = np.random.rand(n, n)
    b = np.random.rand(n)
    r = 1/n

    oracle = oracles.create_lasso_prox_oracle(A, b, r)
    x_star, msg, hist = optimization.proximal_gradient_descent(oracle, np.zeros(n), trace=True)
    plt.clf()
    plt.plot(range(len(hist['line_counter'])), hist['line_counter'])
    plt.xlabel("Iteration")
    plt.ylabel("Line search counter")
    plt.savefig("exp2/LCounter{}.png".format(n))

# ------------------- Experiment 3

for n in [50, 1000]:
    for eps, eps_name in zip([10**-2, 10**-7], ["low", "high"]):
        A = np.random.rand(n, n)
        b = np.random.rand(n)
        r = 1
        x_0 = np.zeros(n)
        u_0 = np.ones(n) * 50