Example #1
0
    def fit(self, X, y):
        """ Fit model with mfista

        Parameters
        -----------
        X : ndarray or scipy.sparse matrix, (n_samples, n_features)
            Data
        y : ndarray, shape (n_samples,) or (n_samples, n_targets)
            Target
        """
        n_features = X.shape[1]

        l1_penalty = self.alpha * self.l1_ratio
        l2_penalty = self.alpha * (1 - self.l1_ratio)

        # objective function
        obj_func = lambda w: logistic_loss(X,y,w) \
                             + l1_penalty*norm(w,1) \
                             + l2_penalty*norm(self.C.dot(w),2)**2/2.

        # lipshictz contant of f1 grad
        lipschitz_constant = logistic_loss_lipschitz_constant(X) + \
                             l2_penalty * self.C_lipschitz_squared

        # gradient of the smooth term
        L = self.C.T.dot(self.C)  # laplacian matrix
        f1_grad = lambda w: logistic_loss_grad(X, y, w) + l2_penalty * L.dot(w)

        # proximal operator for the non-smooth part
        def f2_prox(w, l, *args, **kwargs):
            return prox_l1(w, l * l1_penalty), dict(converged=True)

        if self.warm_start and self.solver_info_ is not None:
            #print 'warm start'
            coef, cost, solver_info = mfista(f1_grad,
                                             f2_prox,
                                             obj_func,
                                             lipschitz_constant,
                                             w_size=n_features,
                                             verbose=self.verbose,
                                             tol=self.tol,
                                             init=self.solver_info_)
        else:
            coef, cost, solver_info = mfista(f1_grad,
                                             f2_prox,
                                             obj_func,
                                             lipschitz_constant,
                                             w_size=n_features,
                                             verbose=self.verbose,
                                             tol=self.tol)

        self.coef_ = coef
        self.cost_ = cost
        self.solver_info_ = solver_info
        self.n_iter = cost.__len__()

        return self
Example #2
0
def test_input_args_and_kwargs():
    rng = np.random.RandomState(42)
    p = 125
    noise_std = 1e-1
    sig = np.zeros(p)
    sig[[0, 2, 13, 4, 25, 32, 80, 89, 91, 93, -1]] = 1
    sig[:6] = 2
    sig[-7:] = 2
    sig[60:75] = 1
    y = sig + noise_std * rng.randn(*sig.shape)
    X = np.eye(p)
    mask = np.ones((p,)).astype(np.bool)
    alpha = .01
    alpha_ = alpha * X.shape[0]
    l1_ratio = .2
    l1_weight = alpha_ * l1_ratio
    f1 = lambda w: _squared_loss(X, y, w, compute_grad=False)
    f1_grad = lambda w: _squared_loss(X, y, w, compute_grad=True,
                                      compute_energy=False)
    f2_prox = lambda w, l, *args, **kwargs: (_prox_l1(w, l * l1_weight),
                                             dict(converged=True))
    total_energy = lambda w: f1(w) + l1_weight * np.sum(np.abs(w))
    for cb_retval in [0, 1]:
        for verbose in [0, 1]:
            for dgap_factor in [1., None]:
                best_w, objective, init = mfista(
                    f1_grad, f2_prox, total_energy, 1., p,
                    dgap_factor=dgap_factor,
                    callback=lambda _: cb_retval, verbose=verbose,
                    max_iter=100)
                assert_equal(best_w.shape, mask.shape)
                assert_true(isinstance(objective, list))
                assert_true(isinstance(init, dict))
                for key in ["w", "t", "dgap_tol", "stepsize"]:
                    assert_true(key in init)
Example #3
0
def test_input_args_and_kwargs():
    rng = np.random.RandomState(42)
    p = 125
    noise_std = 1e-1
    sig = np.zeros(p)
    sig[[0, 2, 13, 4, 25, 32, 80, 89, 91, 93, -1]] = 1
    sig[:6] = 2
    sig[-7:] = 2
    sig[60:75] = 1
    y = sig + noise_std * rng.randn(*sig.shape)
    X = np.eye(p)
    mask = np.ones((p, )).astype(np.bool)
    alpha = .01
    alpha_ = alpha * X.shape[0]
    l1_ratio = .2
    l1_weight = alpha_ * l1_ratio
    f1 = lambda w: _squared_loss(X, y, w, compute_grad=False)
    f1_grad = lambda w: _squared_loss(
        X, y, w, compute_grad=True, compute_energy=False)
    f2_prox = lambda w, l, *args, **kwargs: (_prox_l1(w, l * l1_weight),
                                             dict(converged=True))
    total_energy = lambda w: f1(w) + l1_weight * np.sum(np.abs(w))
    for cb_retval in [0, 1]:
        for verbose in [0, 1]:
            for dgap_factor in [1., None]:
                best_w, objective, init = mfista(f1_grad,
                                                 f2_prox,
                                                 total_energy,
                                                 1.,
                                                 p,
                                                 dgap_factor=dgap_factor,
                                                 callback=lambda _: cb_retval,
                                                 verbose=verbose,
                                                 max_iter=100)
                assert best_w.shape == mask.shape
                assert isinstance(objective, list)
                assert isinstance(init, dict)
                for key in ["w", "t", "dgap_tol", "stepsize"]:
                    assert key in init