def test_ProxL1w(self): """...Test of test_ProxL1w """ coeffs = self.coeffs.copy().astype(self.dtype) l_l1 = 3e-2 t = 1.7 weights = np.arange(coeffs.shape[0], dtype=np.double) prox = ProxL1w(l_l1, weights).astype(self.dtype) thresh = t * l_l1 * weights out = np.sign(coeffs) * (np.abs(coeffs) - thresh) \ * (np.abs(coeffs) > thresh) self.assertAlmostEqual( prox.value(coeffs), l_l1 * (weights * np.abs(coeffs)).sum(), delta=self.delta) assert_almost_equal( prox.call(coeffs, step=t), out, decimal=self.decimal_places) weights = np.arange(coeffs.shape[0], dtype=np.double)[3:8] prox = ProxL1w(l_l1, weights, (3, 8)).astype(self.dtype) thresh = t * l_l1 * weights sub_coeffs = coeffs[3:8] out = coeffs.copy() out[3:8] = np.sign(sub_coeffs) \ * (np.abs(sub_coeffs) - thresh) \ * (np.abs(sub_coeffs) > thresh) self.assertAlmostEqual( prox.value(coeffs), l_l1 * (weights * np.abs(coeffs[3:8])).sum(), delta=self.delta) assert_almost_equal( prox.call(coeffs, step=t), out, decimal=self.decimal_places) weights = np.arange(coeffs.shape[0], dtype=np.double)[3:8] prox = ProxL1w(l_l1, weights, range=(3, 8), positive=True).astype(self.dtype) thresh = t * l_l1 * weights out = coeffs.copy() out[3:8] = np.sign(sub_coeffs) \ * (np.abs(sub_coeffs) - thresh) \ * (np.abs(sub_coeffs) > thresh) idx = out[3:8] < 0 out[3:8][idx] = 0 self.assertAlmostEqual( prox.value(coeffs), l_l1 * (weights * np.abs(coeffs[3:8])).sum(), delta=self.delta) assert_almost_equal( prox.call(coeffs, step=t), out, decimal=self.decimal_places)
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)
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)) for i, prox in enumerate(proxs):