示例#1
0
def test_vrtos_ogl():
    """Test on overlapping group lasso"""
    alpha = 1.0 / n_samples
    groups_1 = [np.arange(8)]
    groups_2 = [np.arange(5, 10)]
    f = cp.utils.LogLoss(A, b, alpha)
    for beta in np.logspace(-3, 3, 3):
        p_1 = cp.utils.GroupL1(beta, groups_1)
        p_2 = cp.utils.GroupL1(beta, groups_2)
        L = cp.utils.get_max_lipschitz(A, "logloss") + alpha / density

        opt_vrtos = cp.minimize_vrtos(
            f.partial_deriv,
            A,
            b,
            np.zeros(n_features),
            1 / (3 * L),
            alpha=alpha,
            max_iter=200,
            prox_1=p_1.prox_factory(n_features),
            prox_2=p_2.prox_factory(n_features),
        )

        opt_tos = cp.minimize_three_split(
            f.f_grad, np.zeros(n_features), prox_1=p_1.prox, prox_2=p_2.prox
        )

        norm = np.linalg.norm(opt_tos.x)
        if norm < 1e-10:
            norm = 1
        assert np.linalg.norm(opt_vrtos.x - opt_tos.x) / norm < 1e-4
示例#2
0
    print("Iteration %s, beta %s" % (i, beta))

    def g_prox(x, gamma, pen=beta):
        return cp.tv_prox.prox_tv1d_cols(gamma * pen, x, n_rows, n_cols)

    def h_prox(x, gamma, pen=beta):
        return cp.tv_prox.prox_tv1d_rows(gamma * pen, x, n_rows, n_cols)

    cb_adatos = cp.utils.Trace()
    x0 = np.zeros(n_features)
    adatos = cp.minimize_three_split(
        f.f_grad,
        x0,
        g_prox,
        h_prox,
        step_size=10 * step_size,
        max_iter=max_iter,
        tol=1e-14,
        verbose=1,
        callback=cb_adatos,
        h_Lipschitz=beta,
    )
    trace_ls = [loss(x, beta) for x in cb_adatos.trace_x]
    all_trace_ls.append(trace_ls)
    all_trace_ls_time.append(cb_adatos.trace_time)
    out_img.append(adatos.x.reshape(img.shape))

    cb_tos = cp.utils.Trace()
    x0 = np.zeros(n_features)
    cp.minimize_three_split(
        f.f_grad,
        x0,
示例#3
0
    img_np = data.cpu().numpy().squeeze().flatten()

    def image_constraint_prox(delta, step_size=None):
        """Projects perturbation delta so that x + delta is in the set of images,
        i.e. the (0, 1) range."""
        adv_img_np = img_np + delta
        delta = adv_img_np.clip(0, 1) - img_np
        return delta

    callback = copt.utils.Trace(lambda delta: f_grad(delta)[0])

    delta0 = np.zeros(data.shape, dtype=float).flatten()

    sol = copt.minimize_three_split(f_grad,
                                    delta0,
                                    constraint.prox,
                                    image_constraint_prox,
                                    callback=callback,
                                    max_iter=50)

    fig, ax = plt.subplots()
    ax.plot([-loss_val for loss_val in callback.trace_fx], lw=3)
    ax.set_yscale("log")
    ax.set_xlabel("# Iterations")
    ax.set_ylabel("Objective value")
    ax.grid()

    plt.show()

    classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',
               'ship', 'truck')
示例#4
0
for i, beta in enumerate(all_betas):
    print("Iteration %s, beta %s" % (i, beta))

    def g_prox(x, gamma, pen=beta):
        return cp.tv_prox.prox_tv1d_cols(gamma * pen, x, n_rows, n_cols)

    def h_prox(x, gamma, pen=beta):
        return cp.tv_prox.prox_tv1d_rows(gamma * pen, x, n_rows, n_cols)

    cb_adatos = cp.utils.Trace()
    adatos = cp.minimize_three_split(
        f.f_grad,
        np.zeros(n_features),
        g_prox,
        h_prox,
        step_size=step_size,
        max_iter=max_iter,
        tol=0,
        callback=cb_adatos,
        h_Lipschitz=beta,
    )
    trace_ls = [loss(x, beta) for x in cb_adatos.trace_x]
    all_trace_ls.append(trace_ls)
    all_trace_ls_time.append(cb_adatos.trace_time)
    out_img.append(adatos.x.reshape(img.shape))

    cb_tos = cp.utils.Trace()
    cp.minimize_three_split(
        f.f_grad,
        np.zeros(n_features),
        g_prox,
示例#5
0
for i, beta in enumerate(all_betas):
    print("beta = %s" % beta)
    G1 = copt.penalty.TraceNorm(beta, Sigma.shape)
    G2 = copt.penalty.L1Norm(beta)

    def loss(x):
        return f(x) + G1(x) + G2(x)

    cb_tosls = cp.utils.Trace()
    x0 = np.zeros(n_features)
    tos_ls = cp.minimize_three_split(
        f.f_grad,
        x0,
        G2.prox,
        G1.prox,
        step_size=5 * step_size,
        max_iter=max_iter,
        tol=1e-14,
        verbose=1,
        callback=cb_tosls,
        h_Lipschitz=beta,
    )
    trace_ls = np.array([loss(x) for x in cb_tosls.trace_x])
    all_trace_ls.append(trace_ls)
    all_trace_ls_time.append(cb_tosls.trace_time)

    cb_tos = cp.utils.Trace()
    x0 = np.zeros(n_features)
    tos = cp.minimize_three_split(
        f.f_grad,
        x0,
        G1.prox,