Exemplo n.º 1
0
def run_one(reg, n_dims, n_atoms, random_state):

    current_time = time.time() - START
    msg = f"{random_state} - {reg}: started at T={current_time:.0f} sec"
    print(colorify(msg, BLUE))

    x, D, _ = make_coding(n_samples=1,
                          n_atoms=n_atoms,
                          n_dim=n_dims,
                          normalize=True,
                          random_state=random_state)

    def stopping_criterion(costs):
        return stop_on_no_decrease(1e-13, costs)

    _, cost, *_ = ista(D,
                       x,
                       reg,
                       max_iter=int(1e6),
                       stopping_criterion=stopping_criterion)
    cost_stop = cost[-1] + 1e-8

    def stopping_criterion(costs):
        return stop_on_value(cost_stop, costs)

    results = {}
    for method, label in methods:
        if 'Oracle' in label:
            x_ = x[0]
        else:
            x_ = x
        _, cost, times, *_ = method(D,
                                    x_,
                                    reg,
                                    max_iter=int(1e6),
                                    stopping_criterion=stopping_criterion)
        results[label] = cost, np.cumsum(times)

    duration = time.time() - START - current_time
    msg = (f"{random_state} - {reg}: done in {duration:.0f} sec "
           f"at T={current_time:.0f} sec")
    print(colorify(msg, GREEN))

    return (reg, n_dims, n_atoms, random_state, label, cost_stop, results)
Exemplo n.º 2
0
def test_ista(reg):

    n_dim = 20
    n_atoms = 10
    n_samples = 1000
    random_state = 42

    n_iters = 100

    # Generate a problem
    x, D, z = make_coding(n_samples=n_samples,
                          n_atoms=n_atoms,
                          n_dim=n_dim,
                          random_state=random_state)

    z_hat, cost_ista, *_ = ista(D, x, reg, max_iter=n_iters)

    assert all(np.diff(cost_ista) <= 0)
    assert all(np.diff(cost_ista) <= 1e-8)
Exemplo n.º 3
0
def test_facnet(reg):

    n_dim = 20
    n_atoms = 10
    n_samples = 1000
    random_state = 42

    # Generate a problem
    x, D, z = make_coding(n_samples=n_samples,
                          n_atoms=n_atoms,
                          n_dim=n_dim,
                          random_state=random_state)

    z_hat_ista, cost_ista, *_ = ista(D, x, reg, max_iter=1)

    L = np.linalg.norm(D.dot(D.T), 2)
    A = np.eye(n_atoms)
    S = L * np.ones((1, n_atoms))
    z_hat = facnet_step(np.zeros_like(z), D, x, reg, A, S)

    assert np.allclose(z_hat_ista, z_hat)
Exemplo n.º 4
0
def test_fista(reg):

    n_dim = 20
    n_atoms = 10
    n_samples = 1000
    random_state = 42

    n_iters = 100

    # Generate a problem
    x, D, z = make_coding(n_samples=n_samples,
                          n_atoms=n_atoms,
                          n_dim=n_dim,
                          random_state=random_state)

    z_hat_ista, cost_ista, *_ = ista(D, x, reg, max_iter=n_iters * 10)
    z_hat_fista, cost_fista, *_ = fista(D, x, reg, max_iter=n_iters)

    assert np.isclose(cost_ista[1], cost_fista[1])
    assert np.isclose(cost_ista[-1], cost_fista[-1])

    diff = z_hat_fista - z_hat_ista
    print(diff[abs(diff) > 1e-2])
Exemplo n.º 5
0
def test_lista_init(reg, n_layers, parametrization, learn_th):

    if parametrization == "alista":
        pytest.skip(msg="ALISTA is not initialized to match ISTA.")
    if "step" in parametrization and not learn_th:
        pytest.skip(msg="For parametrization 'step' and 'coupled_step', "
                    "learn_th need to be set to True.")
    n_dim = 20
    n_atoms = 10
    n_samples = 1000
    random_state = 42

    # Generate a problem
    x, D, z = make_coding(n_samples=n_samples,
                          n_atoms=n_atoms,
                          n_dim=n_dim,
                          random_state=random_state)

    z_hat, cost_ista, _ = ista(D, x, reg, max_iter=n_layers)

    lista = Lista(D, n_layers, parametrization=parametrization)
    cost_lista = lista.score(x, reg)
    assert np.isclose(cost_ista[n_layers], cost_lista)
Exemplo n.º 6
0
###########################################
# Parameters of the simulation
###########################################
n_atoms = 50
n_dims = 10
max_iter = 200
D = rng.randn(n_atoms, n_dims)
x = rng.randn(n_dims)
x /= np.max(np.abs(D.dot(x)))
lmbd = 0.5

############################################
# Benchmark computation function
############################################
_, cost_ista, times_ista = ista(D, x[None, :], lmbd, max_iter=max_iter)
_, cost_fista, times_fista = fista(D, x[None, :], lmbd, max_iter=max_iter)
_, cost_oista, times_oista, steps = oracle_ista(D, x, lmbd, max_iter=max_iter)
_, cost_btista, times_btista, steps_bt = backtracking_ista(D,
                                                           x,
                                                           lmbd,
                                                           eta=.95,
                                                           max_iter=max_iter)
cost_ista = np.array(cost_ista)
cost_oista = np.array(cost_oista)
cost_fista = np.array(cost_fista)
cost_btista = np.array(cost_btista)
z_hat, c_star, _ = ista(D, x[None, :], lmbd, max_iter=3000)
c_star = c_star[-1]

############################################
Exemplo n.º 7
0
"""
import numpy as np
import matplotlib.pyplot as plt

from adopty.ista import ista
from adopty.datasets import make_coding


if __name__ == "__main__":

    reg = .5
    n_iters = 100

    n_dim = 64
    n_atoms = 100
    n_samples = 1000
    random_state = 42

    # Generate a problem
    x, D, z, lmbd_max = make_coding(n_samples=n_samples, n_atoms=n_atoms,
                                    n_dim=n_dim, random_state=random_state)

    # Compute the effective regularization
    lmbd = lmbd_max * reg

    _, cost_ista, _ = ista(D, x, lmbd, max_iter=100)

    cost_ista = np.array(cost_ista)
    plt.loglog(cost_ista - cost_ista.min() + 1e-16)
    plt.show()
Exemplo n.º 8
0
                                  n_dim=n_dim,
                                  normalize=True,
                                  random_state=random_state)
        x_test = x[n_samples:]
        x_train = x[:n_samples]
        for reg in regs:
            print("Computing ISTA for {} with lmbd={:.1}".format(data, reg))
            if data == 'images':
                reg = reg

            def stopping_criterion(costs):
                return stop_on_no_decrease(1e-13, costs)

            _, cost_test, *_ = ista(D,
                                    x_test,
                                    reg,
                                    max_iter=int(1e3),
                                    stopping_criterion=stopping_criterion,
                                    verbose=1)
            _, cost_train, *_ = ista(D,
                                     x_train,
                                     reg,
                                     max_iter=int(1e3),
                                     stopping_criterion=stopping_criterion,
                                     verbose=1)
            c0 = cost_test[0]
            c_star = cost_test[-1]

            results = [(*r, c0, c_star) if r[1] == data and r[2] == reg else r
                       for r in results]
            for n_layer in range(n_layers):
                results.append(
Exemplo n.º 9
0
    quad = .5 * np.sum(r**2) + .5 * L * np.sum((z - zt)**2) + g
    return quad + lbda * np.sum(np.abs(z))


def t_x(x, D, zt, lbda, alpha):
    z = zt - alpha * np.dot(D.T, D.dot(zt) - x)
    return np.sign(z) * np.maximum(np.abs(z) - alpha * lbda, 0)


n, m = 2, 3
n_points = 100
D = rng.randn(n, m)
x = rng.randn(n)
x /= np.max(np.abs(D.T.dot(x)))
lbda = .1
z = ista(D.T, x[None, :], lbda, max_iter=1000)[0]
zt = z.ravel() + .1 * rng.randn(m)
L = np.linalg.norm(D.T.dot(D), ord=2)

alphas = np.linspace(0, 10 / L, n_points)
z_ = [t_x(x, D, zt, lbda, alpha) for alpha in alphas]

F = [lasso_cost(x, D, z, lbda) for z in z_]
L = .4 * L
S1 = [surrogate(x, D, z, zt, lbda, L) for z in z_]
fac = .6
S2 = [surrogate(x, D, z, zt, lbda, fac * L) for z in z_]

f = plt.figure(figsize=(3, 2))
plt.plot(L * alphas, F, label=r'$F_x$', color='k')
plt.plot(L * alphas,