Ejemplo n.º 1
0
def test_tv2_prox():
    """
    similar test, but for 2D total variation penalty.
    """
    np.random.seed(0)
    n_rows, n_cols = 6, 8
    n_features = n_rows * n_cols
    gamma = np.random.rand()
    epsilon = 0.1  # account for some numerical errors

    def tv_norm(x, n_rows, n_cols):
        X = x.reshape((n_rows, n_cols))
        return np.sum(np.abs(np.diff(X, 0))) + np.sum(np.abs(np.diff(X, 1)))

    for nrun in range(20):
        x = np.random.randn(n_features)
        x_next = tv_prox.prox_tv2d(x,
                                   gamma,
                                   n_rows,
                                   n_cols,
                                   tol=1e-10,
                                   max_iter=10000)
        diff_obj = tv_norm(x, n_rows, n_cols) - tv_norm(x_next, n_rows, n_cols)
        testing.assert_array_less(((x - x_next)**2).sum() / gamma,
                                  (1 + epsilon) * diff_obj)
Ejemplo n.º 2
0
    def prox(self, x, step_size):
        # here to avoid circular imports
        from copt import tv_prox

        return tv_prox.prox_tv2d(
            x,
            step_size * self.alpha,
            self.n_rows,
            self.n_cols,
            max_iter=self.max_iter,
            tol=self.tol,
        )