Exemplo n.º 1
0
def one_run(X, n_channels, method, n_atoms, n_times_atom, random_state, reg):
    func, label, n_iter = method
    current_time = time.time() - START
    print('{}-{}-{}: started at {:.0f} sec'.format(label, n_channels,
                                                   random_state, current_time))

    # use the same init for all methods
    D_init = generate_D_init(n_atoms, n_channels, n_times_atom, random_state)
    X = X[:, :n_channels]

    lmbd_max = get_lambda_max(X, D_init).mean()
    reg_ = reg * lmbd_max

    # run the selected algorithm with one iter to remove compilation overhead
    _, _, _, _ = func(X, D_init, reg_, 1, random_state, label, n_channels)

    # run the selected algorithm
    pobj, times, d_hat, z_hat = func(X, D_init, reg_, n_iter, random_state,
                                     label, n_channels)

    # store z_hat in a sparse matrix to reduce size
    for z in z_hat:
        z[z < 1e-3] = 0
    z_hat = [sp.csr_matrix(z) for z in z_hat]

    current_time = time.time() - START
    print('{}-{}-{}: done at {:.0f} sec'.format(label, n_channels,
                                                random_state, current_time))
    assert len(times) > 5
    return (n_channels, random_state, label,
            np.asarray(pobj), np.asarray(times), np.asarray(d_hat),
            np.asarray(z_hat), n_atoms, n_times_atom, reg)
Exemplo n.º 2
0
def run_one(reg, sigma, n_atoms, n_times_atom, max_n_channels, n_times_valid,
            n_iter, run_n_channels, random_state):
    """Run the benchmark for a given set of parameter."""

    X, uv, uv_init = get_signals(max_n_channels, n_times_atom, n_times_valid,
                                 sigma, random_state)

    reg_ = reg * get_lambda_max(X, uv_init).max()
    # reg_ *= run_n_channels

    uv_init_ = prox_uv(np.c_[uv_init[:, :run_n_channels],
                             uv_init[:, max_n_channels:]])
    uv_ = prox_uv(np.c_[uv[:, :run_n_channels], uv[:, max_n_channels:]],
                  uv_constraint='separate',
                  n_channels=max_n_channels)

    def cb(X, uv_hat, z_hat, pobj):
        it = len(pobj) // 2
        if it % 10 == 0:
            print("[channels{}] iter{} score sig={:.2e}: {:.3e}".format(
                run_n_channels, it, sigma, score_uv(uv_, uv_hat,
                                                    run_n_channels)))

    pobj, times, uv_hat, z_hat, reg = learn_d_z_multi(
        X[:, :run_n_channels, :],
        n_atoms,
        n_times_atom,
        random_state=random_state,
        # callback=cb,
        n_iter=n_iter,
        n_jobs=1,
        reg=reg_,
        uv_constraint='separate',
        solver_d='alternate_adaptive',
        solver_d_kwargs={'max_iter': 50},
        solver_z="lgcd",
        solver_z_kwargs=dict(tol=1e-3, maxiter=500),
        use_sparse_z=True,
        D_init=uv_init_,
        verbose=VERBOSE,
    )

    score = score_uv(uv_, uv_hat, run_n_channels)
    print("=" * 79 + "\n" +
          "[channels{}-{:.2e}-{}] iter {} score sig={:.2e}: {:.3e}\n".format(
              run_n_channels, reg, random_state,
              len(pobj) // 2, sigma, score) + "=" * 79)

    return random_state, sigma, run_n_channels, score, uv, uv_hat, reg
Exemplo n.º 3
0
def plot_loss(reg_ratio):
    n_trials = 1
    n_channels = 1
    n_times, n_atoms, n_times_atom = 100000, 10, 100

    rng = np.random.RandomState(0)
    X = rng.randn(n_trials, n_channels, n_times)
    D = rng.randn(n_atoms, n_channels, n_times_atom)

    reg = reg_ratio * get_lambda_max(X, D).max()

    results = []
    for func, max_iter in all_func:
        print(func.__name__)
        res = run_one(func, n_times, n_atoms, n_times_atom, reg, max_iter, X,
                      D)
        results.append(res)

    best = np.inf
    for res in results:
        func_name, n_times, n_atoms, n_times_atom, reg, times, pobj = res
        if pobj[-1] < best:
            best = pobj[-1]

    fig = plt.figure()
    for (func, max_iter), res in zip(all_func, results):
        style = '-' if 'cd' in func.__name__ else '--'
        func_name, n_times, n_atoms, n_times_atom, reg, times, pobj = res
        plt.loglog(times, np.array(pobj) - best, style, label=func.__name__)
    plt.legend()
    plt.xlim(1e-2, None)
    name = ('T=%s_K=%s_L=%s_reg=%.3f' % (n_times, n_atoms, n_times_atom,
                                         reg_ratio))
    plt.title(name)
    plt.xlabel('Time (s)')
    plt.ylabel('loss function')
    save_name = 'figures/bench_gcd/' + name + '.png'
    print('Saving %s' % (save_name, ))
    fig.savefig(save_name)
    # plt.show()
    plt.close(fig)