Пример #1
0
    def test_dense_and_sparse_match(self):
        """...Test in SVRG that dense and sparse code matches in all possible
        settings
        """
        variance_reductions = ['last', 'rand']
        rand_types = ['perm', 'unif']
        seed = 123
        tol = 0.
        max_iter = 50

        n_samples = 500
        n_features = 20

        # Crazy prox examples
        proxs = [
            ProxTV(strength=1e-2, range=(5, 13),
                   positive=True).astype(self.dtype),
            ProxElasticNet(strength=1e-2, ratio=0.9).astype(self.dtype),
            ProxEquality(range=(0, n_features)).astype(self.dtype),
            ProxL1(strength=1e-3, range=(5, 17)).astype(self.dtype),
            ProxL1w(strength=1e-3, weights=np.arange(5, 17, dtype=np.double),
                    range=(5, 17)).astype(self.dtype),
        ]

        for intercept in [-1, None]:
            X, y = self.simu_linreg_data(dtype=self.dtype, interc=intercept,
                                         n_features=n_features,
                                         n_samples=n_samples)

            fit_intercept = intercept is not None
            model_dense, model_spars = self.get_dense_and_sparse_linreg_model(
                X, y, dtype=self.dtype, fit_intercept=fit_intercept)
            step = 1 / model_spars.get_lip_max()

            for variance_reduction, rand_type, prox in product(
                    variance_reductions, rand_types, proxs):
                solver_sparse = SVRG(step=step, tol=tol, max_iter=max_iter,
                                     verbose=False,
                                     variance_reduction=variance_reduction,
                                     rand_type=rand_type, seed=seed)
                solver_sparse.set_model(model_spars).set_prox(prox)

                solver_dense = SVRG(step=step, tol=tol, max_iter=max_iter,
                                    verbose=False,
                                    variance_reduction=variance_reduction,
                                    rand_type=rand_type, seed=seed)
                solver_dense.set_model(model_dense).set_prox(prox)

                solver_sparse.solve()
                solver_dense.solve()
                places = 7
                if self.dtype is "float32":
                    places = 3
                np.testing.assert_array_almost_equal(solver_sparse.solution,
                                                     solver_dense.solution,
                                                     decimal=places)
Пример #2
0
    def _construct_prox_obj(self, coeffs=None, project=False):
        n_penalized_features = len(self.penalized_features) \
            if self.penalized_features is not None else 0

        if project:
            # project future _coeffs on the support of given _coeffs
            if all(self.n_lags == 0):
                proxs = [ProxZero()]
            elif coeffs is not None:
                prox_ranges = self._detect_support(coeffs)
                proxs = [ProxEquality(0, range=r) for r in prox_ranges]
            else:
                raise ValueError("Coeffs are None. " +
                                 "Equality penalty cannot infer the "
                                 "coefficients support.")
        elif n_penalized_features > 0 and self._C_tv is not None or \
                self._C_group_l1 is not None:
            # TV and GroupLasso penalties
            blocks_start = np.zeros(n_penalized_features)
            blocks_end = np.zeros(n_penalized_features)
            proxs = []

            for i in self.penalized_features:
                start = int(self._features_offset[i])
                blocks_start[i] = start
                end = int(blocks_start[i] + self._n_lags[i] + 1)
                blocks_end[i] = end
                if self._C_tv is not None:
                    proxs.append(ProxTV(1 / self._C_tv, range=(start, end)))

            if self._C_group_l1 is not None:
                blocks_size = blocks_end - blocks_start
                proxs.append(
                    ProxGroupL1(1 / self._C_group_l1, blocks_start.tolist(),
                                blocks_size.tolist()))
        else:
            # Default prox: does nothing
            proxs = [ProxZero()]

        prox_obj = ProxMulti(tuple(proxs))

        return prox_obj
Пример #3
0
    def test_ProxEquality(self):
        """...Test of ProxEquality
        """
        coeffs = self.coeffs.copy()
        strength = 0.5

        prox = ProxEquality(strength)
        self.assertIsNone(prox.strength)
        prox.strength = 2.
        self.assertIsNone(prox.strength)

        self.assertEqual(prox.value(coeffs), np.inf)

        out = np.empty(coeffs.shape)
        out.fill(np.mean(coeffs))
        np.testing.assert_array_almost_equal(prox.call(coeffs), out)
        step = 4.
        np.testing.assert_array_almost_equal(prox.call(coeffs, step=step), out)

        coeffs -= 10.
        out.fill(np.mean(coeffs))
        np.testing.assert_array_almost_equal(prox.call(coeffs), out)
        prox.positive = True
        out.fill(0.)
        np.testing.assert_array_almost_equal(prox.call(coeffs), out)

        prox.range = (3, 8)
        prox.positive = False
        coeffs = self.coeffs.copy()
        out = coeffs.copy()
        out[3:8] = np.mean(out[3:8])
        np.testing.assert_array_almost_equal(prox.call(coeffs), out)

        coeffs[3:8] -= 10.
        out[3:8].fill(np.mean(coeffs[3:8]))
        np.testing.assert_array_almost_equal(prox.call(coeffs), out)
        prox.positive = True
        out[3:8].fill(0.)
        np.testing.assert_array_almost_equal(prox.call(coeffs), out)

        self.assertEqual(prox.value(out), 0)
        self.assertEqual(prox.value(coeffs), np.inf)
Пример #4
0
    ProxEquality, ProxL1w

np.random.seed(12)
x = np.random.randn(50)
a, b = x.min() - 1e-1, x.max() + 1e-1
s = 0.4

proxs = [
    ProxZero(),
    ProxPositive(),
    ProxL2Sq(strength=s),
    ProxL1(strength=s),
    ProxElasticNet(strength=s, ratio=0.5),
    ProxSlope(strength=s),
    ProxTV(strength=s),
    ProxEquality(range=(25, 40)),
    ProxL1w(strength=s, weights=0.1 * np.arange(50, dtype=np.double)),
    ProxGroupL1(strength=2 * s,
                blocks_start=np.arange(0, 50, 10),
                blocks_length=10 * np.ones((5, ))),
    ProxBinarsity(strength=s,
                  blocks_start=np.arange(0, 50, 10),
                  blocks_length=10 * np.ones((5, )))
]

fig, _ = plt.subplots(3, 4, figsize=(16, 12), sharey=True, sharex=True)
fig.axes[0].stem(x)
fig.axes[0].set_title("original vector", fontsize=16)
fig.axes[0].set_xlim((-1, 51))
fig.axes[0].set_ylim((a, b))