def test_svrg_callback(): class Callback(object): def __init__(self, X, y): self.X = X self.y = y self.obj = [] def __call__(self, clf): clf._finalize_coef() y_pred = clf.decision_function(self.X).ravel() loss = (np.maximum(1 - self.y * y_pred, 0) ** 2).mean() coef = clf.coef_.ravel() regul = 0.5 * clf.alpha * np.dot(coef, coef) self.obj.append(loss + regul) cb = Callback(X_bin, y_bin) clf = SVRGClassifier(loss="squared_hinge", eta=1e-3, max_iter=20, random_state=0, callback=cb) clf.fit(X_bin, y_bin) assert_true(np.all(np.diff(cb.obj) <= 0))
n_classes=2, random_state=0) etas = (1e-3, 1e-4, 1e-5) n_inners = (0.25, 0.5, 1.0, 1.5) y = y * 2 - 1 plt.figure() for eta in etas: print "eta =", eta cb = Callback(X, y) clf = SVRGClassifier(loss="squared_hinge", alpha=1e-5, eta=eta, n_inner=1.0, max_iter=20, random_state=0, callback=cb) clf.fit(X, y) plt.plot(cb.times, cb.obj, label="eta=" + str(eta)) plt.xlabel("CPU time") plt.ylabel("Objective value") plt.legend() plt.figure() for n_inner in n_inners: print "n_inner =", n_inner cb = Callback(X, y) clf = SVRGClassifier(loss="squared_hinge", alpha=1e-5, eta=1e-4, n_inner=n_inner, max_iter=1000, random_state=0, callback=cb, verbose=1) clf.fit(X, y)
y = y * 2 - 1 plt.figure() for eta in etas: print "eta =", eta cb = Callback(X, y) clf = SVRGClassifier(loss="squared_hinge", alpha=1e-5, eta=eta, n_inner=1.0, max_iter=20, random_state=0, callback=cb) clf.fit(X, y) plt.plot(cb.times, cb.obj, label="eta=" + str(eta)) plt.xlabel("CPU time") plt.ylabel("Objective value") plt.legend() plt.figure() for n_inner in n_inners: print "n_inner =", n_inner cb = Callback(X, y) clf = SVRGClassifier(loss="squared_hinge", alpha=1e-5, eta=1e-4, n_inner=n_inner,
def test_multiclass_classes(): clf = SVRGClassifier() clf.fit(X, y) assert_equal(list(clf.classes_), [0, 1, 2])
def test_bin_classes(): clf = SVRGClassifier() clf.fit(X_bin, y_bin) assert_equal(list(clf.classes_), [-1, 1])
def test_svrg(): clf = SVRGClassifier(eta=1e-3, max_iter=20, random_state=0, verbose=0) clf.fit(X_bin, y_bin) assert not hasattr(clf, 'predict_proba') assert_equal(clf.score(X_bin, y_bin), 1.0)
def test_multiclass_classes(): clf = SVRGClassifier() clf.fit(X, y) assert list(clf.classes_) == [0, 1, 2]
def test_bin_classes(): clf = SVRGClassifier() clf.fit(X_bin, y_bin) assert list(clf.classes_) == [-1, 1]