|
1 | 1 | """ |
2 | | -=============================================================== |
3 | | -Use LogisticRegression class with Celer and Prox-Newton solvers |
4 | | -=============================================================== |
| 2 | +================================================================== |
| 3 | +Compare LogisticRegression solver with sklearn's liblinear backend |
| 4 | +================================================================== |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import time |
| 8 | +import warnings |
7 | 9 | import numpy as np |
8 | 10 | from numpy.linalg import norm |
| 11 | +import matplotlib.pyplot as plt |
9 | 12 | from sklearn import linear_model |
| 13 | +from libsvmdata import fetch_libsvm |
10 | 14 |
|
11 | 15 | from celer import LogisticRegression |
12 | | -from celer.datasets import fetch_ml_uci |
13 | 16 |
|
14 | | -dataset = "gisette_train" |
15 | | -X, y = fetch_ml_uci(dataset) |
| 17 | +warnings.filterwarnings("ignore", message="Objective did not converge") |
| 18 | +warnings.filterwarnings("ignore", message="Liblinear failed to converge") |
| 19 | + |
| 20 | +X, y = fetch_libsvm("news20.binary") |
16 | 21 |
|
17 | 22 | C_min = 2 / norm(X.T @ y, ord=np.inf) |
18 | | -C = 5 * C_min |
19 | | -clf = LogisticRegression(C=C, verbose=1, solver="celer-pn", tol=1e0).fit(X, y) |
20 | | -w_celer = clf.coef_.ravel() |
| 23 | +C = 20 * C_min |
| 24 | + |
| 25 | + |
| 26 | +def pobj_logreg(w): |
| 27 | + return np.sum(np.log(1 + np.exp(-y * (X @ w)))) + 1. / C * norm(w, ord=1) |
| 28 | + |
| 29 | + |
| 30 | +pobj_celer = [] |
| 31 | +t_celer = [] |
| 32 | + |
| 33 | +for n_iter in range(10): |
| 34 | + t0 = time.time() |
| 35 | + clf = LogisticRegression( |
| 36 | + C=C, solver="celer-pn", max_iter=n_iter, tol=0).fit(X, y) |
| 37 | + t_celer.append(time.time() - t0) |
| 38 | + w_celer = clf.coef_.ravel() |
| 39 | + pobj_celer.append(pobj_logreg(w_celer)) |
| 40 | + |
| 41 | +pobj_celer = np.array(pobj_celer) |
| 42 | + |
| 43 | + |
| 44 | +pobj_libl = [] |
| 45 | +t_libl = [] |
| 46 | + |
| 47 | +for n_iter in np.arange(0, 50, 10): |
| 48 | + t0 = time.time() |
| 49 | + clf = linear_model.LogisticRegression( |
| 50 | + C=C, solver="liblinear", penalty='l1', fit_intercept=False, |
| 51 | + max_iter=n_iter, random_state=0, tol=1e-10).fit(X, y) |
| 52 | + t_libl.append(time.time() - t0) |
| 53 | + w_libl = clf.coef_.ravel() |
| 54 | + pobj_libl.append(pobj_logreg(w_libl)) |
| 55 | + |
| 56 | +pobj_libl = np.array(pobj_libl) |
| 57 | + |
| 58 | +p_star = min(pobj_celer.min(), pobj_libl.min()) |
21 | 59 |
|
22 | | -clf = linear_model.LogisticRegression( |
23 | | - C=C, solver="liblinear", penalty='l1', fit_intercept=False).fit(X, y) |
24 | | -w_lib = clf.coef_.ravel() |
| 60 | +plt.close("all") |
| 61 | +fig = plt.figure(figsize=(4, 2), constrained_layout=True) |
| 62 | +plt.semilogy(t_celer, pobj_celer - p_star, label="Celer-PN") |
| 63 | +plt.semilogy(t_libl, pobj_libl - p_star, label="liblinear") |
| 64 | +plt.legend() |
| 65 | +plt.xlabel("Time (s)") |
| 66 | +plt.ylabel("objective suboptimality") |
| 67 | +plt.show(block=False) |
0 commit comments