예제 #1
0
def run_without_soft_lock(n_atoms=25,
                          atom_support=(12, 12),
                          reg=.01,
                          tol=5e-2,
                          n_workers=100,
                          random_state=60):
    rng = np.random.RandomState(random_state)

    X = get_mandril()
    D_init = init_dictionary(X, n_atoms, atom_support, random_state=rng)
    lmbd_max = get_lambda_max(X, D_init).max()
    reg_ = reg * lmbd_max

    z_hat, *_ = dicod(X,
                      D_init,
                      reg_,
                      max_iter=1000000,
                      n_workers=n_workers,
                      tol=tol,
                      strategy='greedy',
                      verbose=1,
                      soft_lock='none',
                      z_positive=False,
                      timing=False)
    pobj = compute_objective(X, z_hat, D_init, reg_)
    z_hat = np.clip(z_hat, -1e3, 1e3)
    print("[DICOD] final cost : {}".format(pobj))

    X_hat = reconstruct(z_hat, D_init)
    X_hat = np.clip(X_hat, 0, 1)
    return X_hat, pobj
예제 #2
0
def run_one(T, L, K, d, noise_level, seed_pb, n_jobs, reg, tol, strategy,
            common_args):

    X, D_hat = simulate_data(T, L, K, d, noise_level, seed=seed_pb)
    lmbd_max = get_lambda_max(X, D_hat)
    reg_ = reg * lmbd_max

    n_seg = 1
    strategy_ = strategy
    if strategy == 'lgcd':
        n_seg = 'auto'
        strategy_ = "greedy"

    *_, pobj, _ = dicod(X,
                        D_hat,
                        reg_,
                        n_jobs=n_jobs,
                        tol=tol,
                        strategy=strategy_,
                        n_seg=n_seg,
                        **common_args)
    print(pobj)

    return ResultItem(reg=reg,
                      n_jobs=n_jobs,
                      strategy=strategy,
                      tol=tol,
                      seed=seed_pb,
                      pobj=pobj)
예제 #3
0
def run_one_grid(n_atoms, atom_support, reg, n_workers, grid, tol, soft_lock,
                 dicod_args, random_state):

    tag = f"[{soft_lock} - {reg:.0e} - {random_state[0]}]"
    random_state = random_state[1]

    # Generate a problem
    print(
        colorify(79 * "=" + f"\n{tag} Start with {n_workers} workers\n" +
                 79 * "="))
    X = get_mandril()
    D = init_dictionary(X, n_atoms, atom_support, random_state=random_state)
    reg_ = reg * get_lambda_max(X, D).max()

    if grid:
        w_world = 'auto'
    else:
        w_world = n_workers

    z_hat, *_, run_statistics = dicod(X,
                                      D,
                                      reg=reg_,
                                      n_seg='auto',
                                      strategy='greedy',
                                      w_world=w_world,
                                      n_workers=n_workers,
                                      timing=False,
                                      tol=tol,
                                      soft_lock=soft_lock,
                                      **dicod_args)

    runtime = run_statistics['runtime']
    sparsity = len(z_hat.nonzero()[0]) / z_hat.size

    print(
        colorify("=" * 79 + f"\n{tag} End for {n_workers} workers "
                 f"in {runtime:.1e}\n" + "=" * 79,
                 color=GREEN))

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_workers=n_workers,
                      grid=grid,
                      tol=tol,
                      soft_lock=soft_lock,
                      random_state=random_state,
                      dicod_args=dicod_args,
                      sparsity=sparsity,
                      **run_statistics)
예제 #4
0
def run_one(n_atoms, atom_support, reg, n_jobs, strategy, tol, seed, timeout,
            max_iter, verbose, dicod_kwargs):
    # Generate a problem
    X, D = get_problem(n_atoms, atom_support, seed)
    lmbd = reg * get_lambda_max(X[None], D).max()

    if strategy == 'lgcd':
        n_seg = 'auto'
        effective_strategy = 'greedy'
    elif strategy in ["greedy", 'random']:
        n_seg = 1
        effective_strategy = strategy
    else:
        raise NotImplementedError(f"Bad strategy name {strategy}")

    z_hat, *_, pobj, cost = dicod(X,
                                  D,
                                  reg=lmbd,
                                  n_seg=n_seg,
                                  strategy=effective_strategy,
                                  n_jobs=n_jobs,
                                  timing=True,
                                  tol=tol,
                                  timeout=timeout,
                                  max_iter=max_iter,
                                  verbose=verbose,
                                  **dicod_kwargs)

    sparsity = len(z_hat.nonzero()[0]) / z_hat.size

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_jobs=n_jobs,
                      n_seg=n_seg,
                      strategy=strategy,
                      tol=tol,
                      dicod_kwargs=dicod_kwargs,
                      seed=seed,
                      sparsity=sparsity,
                      pobj=pobj)
예제 #5
0
def run_one_grid(n_atoms, atom_support, reg, n_jobs, grid, tol, random_state,
                 verbose):
    # Generate a problem
    X = get_mandril()
    D = init_dictionary(X, n_atoms, atom_support, random_state=random_state)
    reg_ = reg * get_lambda_max(X, D).max()

    if grid:
        w_world = 'auto'
    else:
        w_world = n_jobs

    dicod_kwargs = dict(z_positive=False,
                        soft_lock='corner',
                        timeout=None,
                        max_iter=int(1e8))
    z_hat, *_, pobj, cost = dicod(X,
                                  D,
                                  reg=reg_,
                                  n_seg='auto',
                                  strategy='greedy',
                                  w_world=w_world,
                                  n_jobs=n_jobs,
                                  timing=True,
                                  tol=tol,
                                  verbose=verbose,
                                  **dicod_kwargs)

    sparsity = len(z_hat.nonzero()[0]) / z_hat.size

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_jobs=n_jobs,
                      grid=grid,
                      tol=tol,
                      random_state=random_state,
                      sparsity=sparsity,
                      pobj=pobj)
예제 #6
0
def run_one_scaling_2d(n_atoms, atom_support, reg, n_workers, strategy, tol,
                       dicod_args, random_state):
    tag = f"[{strategy} - {reg:.0e} - {random_state[0]}]"
    random_state = random_state[1]

    # Generate a problem
    print(
        colorify(79 * "=" + f"\n{tag} Start with {n_workers} workers\n" +
                 79 * "="))
    X = get_mandril()
    D = init_dictionary(X, n_atoms, atom_support, random_state=random_state)
    reg_ = reg * get_lambda_max(X, D).max()

    z_hat, *_, run_statistics = dicod(X,
                                      D,
                                      reg=reg_,
                                      strategy=strategy,
                                      n_workers=n_workers,
                                      tol=tol,
                                      **dicod_args)

    runtime = run_statistics['runtime']
    sparsity = len(z_hat.nonzero()[0]) / z_hat.size
    print(
        colorify('=' * 79 + f"\n{tag} End with {n_workers} workers for reg="
                 f"{reg:.0e} in {runtime:.1e}\n" + "=" * 79,
                 color=GREEN))

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_workers=n_workers,
                      strategy=strategy,
                      tol=tol,
                      dicod_args=dicod_args,
                      random_state=random_state,
                      sparsity=sparsity,
                      **run_statistics)
예제 #7
0
def run_one(method, n_atoms, atom_support, reg, z_positive, n_jobs, n_iter,
            tol, eps, random_state):

    X = get_hubble()[:, 512:1024, 512:1024]
    D_init = init_dictionary(X,
                             n_atoms,
                             atom_support,
                             random_state=random_state)

    if method == 'wohlberg':
        ################################################################
        #            Run parallel consensus ADMM
        #
        lmbd_max = get_lambda_max(X, D_init).max()
        print("Lambda max = {}".format(lmbd_max))
        reg_ = reg * lmbd_max

        D_init_ = np.transpose(D_init, axes=(3, 2, 1, 0))
        X_ = np.transpose(X[None], axes=(3, 2, 1, 0))

        options = {
            'Verbose': True,
            'StatusHeader': False,
            'MaxMainIter': n_iter,
            'CCMOD': {
                'rho': 1.0,
                'ZeroMean': False
            },
            'CBPDN': {
                'rho': 50.0 * reg_ + 0.5,
                'NonNegCoef': z_positive
            },
            'DictSize': D_init_.shape,
        }
        opt = ConvBPDNDictLearn_Consensus.Options(options)
        cdl = ConvBPDNDictLearn_Consensus(D_init_,
                                          X_,
                                          lmbda=reg_,
                                          nproc=n_jobs,
                                          opt=opt,
                                          dimK=1,
                                          dimN=2)

        _, pobj = cdl.solve()
        print(pobj)

        itstat = cdl.getitstat()
        times = itstat.Time

    elif method == "dicodile":
        pobj, times, D_hat, z_hat = dicodile(X,
                                             D_init,
                                             reg=reg,
                                             z_positive=z_positive,
                                             n_iter=n_iter,
                                             eps=eps,
                                             n_jobs=n_jobs,
                                             verbose=2,
                                             tol=tol)
        pobj = pobj[::2]
        times = np.cumsum(times)[::2]

    else:
        raise NotImplementedError()

    return ResultItem(n_atoms=n_atoms,
                      atom_support=atom_support,
                      reg=reg,
                      n_jobs=n_jobs,
                      random_state=random_state,
                      method=method,
                      z_positive=z_positive,
                      times=times,
                      pobj=pobj)