optiml 1.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- optiml/__init__.py +0 -0
- optiml/ml/__init__.py +0 -0
- optiml/ml/neural_network/__init__.py +3 -0
- optiml/ml/neural_network/_base.py +475 -0
- optiml/ml/neural_network/activations.py +79 -0
- optiml/ml/neural_network/initializers.py +66 -0
- optiml/ml/neural_network/layers.py +183 -0
- optiml/ml/neural_network/losses.py +178 -0
- optiml/ml/neural_network/regularizers.py +87 -0
- optiml/ml/svm/__init__.py +3 -0
- optiml/ml/svm/_base.py +1442 -0
- optiml/ml/svm/kernels.py +208 -0
- optiml/ml/svm/losses.py +284 -0
- optiml/ml/svm/smo.py +797 -0
- optiml/ml/tests/__init__.py +0 -0
- optiml/ml/tests/_datasets.py +49 -0
- optiml/ml/tests/_utils.py +28 -0
- optiml/ml/tests/test_initializers.py +33 -0
- optiml/ml/tests/test_neural_network.py +86 -0
- optiml/ml/tests/test_svc.py +245 -0
- optiml/ml/tests/test_svr.py +256 -0
- optiml/ml/utils.py +252 -0
- optiml/opti/__init__.py +4 -0
- optiml/opti/_base.py +309 -0
- optiml/opti/constrained/__init__.py +9 -0
- optiml/opti/constrained/_base.py +404 -0
- optiml/opti/constrained/active_set.py +228 -0
- optiml/opti/constrained/frank_wolfe.py +158 -0
- optiml/opti/constrained/interior_point.py +282 -0
- optiml/opti/constrained/projected_gradient.py +138 -0
- optiml/opti/constrained/tests/__init__.py +0 -0
- optiml/opti/constrained/tests/test_active_set.py +16 -0
- optiml/opti/constrained/tests/test_frank_wolfe.py +16 -0
- optiml/opti/constrained/tests/test_interior_point.py +16 -0
- optiml/opti/constrained/tests/test_lagrangian_quadratic.py +26 -0
- optiml/opti/constrained/tests/test_lower_bound.py +29 -0
- optiml/opti/constrained/tests/test_projected_gradient.py +16 -0
- optiml/opti/unconstrained/__init__.py +6 -0
- optiml/opti/unconstrained/_base.py +63 -0
- optiml/opti/unconstrained/line_search/__init__.py +10 -0
- optiml/opti/unconstrained/line_search/_base.py +106 -0
- optiml/opti/unconstrained/line_search/conjugate_gradient.py +255 -0
- optiml/opti/unconstrained/line_search/gradient_descent.py +212 -0
- optiml/opti/unconstrained/line_search/line_search.py +248 -0
- optiml/opti/unconstrained/line_search/newton.py +198 -0
- optiml/opti/unconstrained/line_search/quasi_newton.py +496 -0
- optiml/opti/unconstrained/proximal_bundle.py +219 -0
- optiml/opti/unconstrained/stochastic/__init__.py +12 -0
- optiml/opti/unconstrained/stochastic/_base.py +246 -0
- optiml/opti/unconstrained/stochastic/adadelta.py +133 -0
- optiml/opti/unconstrained/stochastic/adagrad.py +123 -0
- optiml/opti/unconstrained/stochastic/adam.py +179 -0
- optiml/opti/unconstrained/stochastic/adamax.py +178 -0
- optiml/opti/unconstrained/stochastic/amsgrad.py +177 -0
- optiml/opti/unconstrained/stochastic/gradient_descent.py +135 -0
- optiml/opti/unconstrained/stochastic/rmsprop.py +156 -0
- optiml/opti/unconstrained/stochastic/schedules.py +89 -0
- optiml/opti/unconstrained/tests/__init__.py +0 -0
- optiml/opti/unconstrained/tests/test_adadelta.py +20 -0
- optiml/opti/unconstrained/tests/test_adagrad.py +20 -0
- optiml/opti/unconstrained/tests/test_adam.py +42 -0
- optiml/opti/unconstrained/tests/test_adamax.py +41 -0
- optiml/opti/unconstrained/tests/test_amsgrad.py +40 -0
- optiml/opti/unconstrained/tests/test_conjugate_gradient.py +35 -0
- optiml/opti/unconstrained/tests/test_functions.py +34 -0
- optiml/opti/unconstrained/tests/test_gradient_descent.py +51 -0
- optiml/opti/unconstrained/tests/test_newton.py +20 -0
- optiml/opti/unconstrained/tests/test_quasi_newton.py +30 -0
- optiml/opti/unconstrained/tests/test_rmsprop.py +40 -0
- optiml/opti/unconstrained/tests/test_verbose.py +25 -0
- optiml/opti/utils.py +353 -0
- optiml-1.7.dist-info/METADATA +203 -0
- optiml-1.7.dist-info/RECORD +76 -0
- optiml-1.7.dist-info/WHEEL +5 -0
- optiml-1.7.dist-info/licenses/LICENSE +21 -0
- optiml-1.7.dist-info/top_level.txt +1 -0
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import urllib.request
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pytest
|
|
6
|
+
|
|
7
|
+
# The Boston house-prices dataset has been removed from scikit-learn since version
|
|
8
|
+
# 1.2 due to ethical concerns. To keep the existing regression tests (and their
|
|
9
|
+
# accuracy thresholds) reproducible, it is loaded here directly from its original
|
|
10
|
+
# source, exactly as suggested in the scikit-learn deprecation notice, and cached
|
|
11
|
+
# locally to avoid downloading it more than once.
|
|
12
|
+
|
|
13
|
+
_BOSTON_URL = 'http://lib.stat.cmu.edu/datasets/boston'
|
|
14
|
+
_BOSTON_CACHE = os.path.join(os.path.dirname(__file__), 'data', 'boston.npz')
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def load_boston(return_X_y=True):
|
|
18
|
+
"""
|
|
19
|
+
Load and return the Boston house-prices dataset (regression).
|
|
20
|
+
|
|
21
|
+
The data (506 samples, 13 features) is fetched from its original StatLib
|
|
22
|
+
source and cached locally; if it is not available (e.g., no network) the
|
|
23
|
+
calling test is skipped rather than failed.
|
|
24
|
+
|
|
25
|
+
:param return_X_y: (bool, default True): if True return ``(data, target)``,
|
|
26
|
+
otherwise the same tuple (kept for API compatibility with
|
|
27
|
+
the former ``sklearn.datasets.load_boston``).
|
|
28
|
+
:return: ``(X, y)`` with X of shape (506, 13) and y of shape (506,).
|
|
29
|
+
"""
|
|
30
|
+
if os.path.exists(_BOSTON_CACHE):
|
|
31
|
+
with np.load(_BOSTON_CACHE) as cache:
|
|
32
|
+
return cache['data'], cache['target']
|
|
33
|
+
|
|
34
|
+
try:
|
|
35
|
+
with urllib.request.urlopen(_BOSTON_URL, timeout=30) as response:
|
|
36
|
+
raw = response.read().decode()
|
|
37
|
+
except Exception as e: # no network or source unavailable
|
|
38
|
+
pytest.skip(f'Boston dataset not available: {e}')
|
|
39
|
+
|
|
40
|
+
# the 22 header lines are textual; the rest is a flat stream of 506 * 14
|
|
41
|
+
# floating point numbers (13 features + the target) laid out over two lines
|
|
42
|
+
# per record, so it is enough to parse all the numeric tokens and reshape
|
|
43
|
+
values = np.array(' '.join(raw.splitlines()[22:]).split(), dtype=float).reshape(-1, 14)
|
|
44
|
+
data, target = values[:, :13], values[:, 13]
|
|
45
|
+
|
|
46
|
+
os.makedirs(os.path.dirname(_BOSTON_CACHE), exist_ok=True)
|
|
47
|
+
np.savez(_BOSTON_CACHE, data=data, target=target)
|
|
48
|
+
|
|
49
|
+
return data, target
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
# Tolerance on the relative optimality gap (f(x) - f*) / f*, used to certify that
|
|
4
|
+
# an optimizer actually reached the (solver-certified) primal optimum f* computed
|
|
5
|
+
# by `SVMLoss.f_star`. Smooth losses (squared hinge / squared epsilon-insensitive)
|
|
6
|
+
# are minimized essentially to machine precision by every method, while the
|
|
7
|
+
# nonsmooth ones (hinge / epsilon-insensitive) are only reached within a looser
|
|
8
|
+
# tolerance by first-order and subgradient-type methods.
|
|
9
|
+
SMOOTH_TOL = 1e-4
|
|
10
|
+
NONSMOOTH_TOL = 5e-2
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def optimality_gap(model):
|
|
14
|
+
"""Relative optimality gap (f(x) - f*) / f* of a fitted primal SVM model."""
|
|
15
|
+
x = np.hstack((model.coef_, model.intercept_))
|
|
16
|
+
return (model.loss(x) - model.loss.f_star()) / model.loss.f_star()
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def assert_optimal(model, tol):
|
|
20
|
+
"""Assert that a fitted primal SVM model reached its optimum within ``tol``."""
|
|
21
|
+
gap = optimality_gap(model)
|
|
22
|
+
assert gap <= tol, f'relative optimality gap {gap:.2e} exceeds tolerance {tol:.0e}'
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def assert_all_optimal(ovr, tol):
|
|
26
|
+
"""Assert optimality for each binary estimator of a fitted OvR SVM classifier."""
|
|
27
|
+
for estimator in ovr.estimators_:
|
|
28
|
+
assert_optimal(estimator, tol)
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
|
|
4
|
+
from optiml.ml.neural_network.initializers import (truncated_normal, glorot_normal,
|
|
5
|
+
glorot_uniform, he_normal, he_uniform)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_initializers_shape_finite_and_reproducible():
|
|
9
|
+
shape = (10, 5)
|
|
10
|
+
for init in (glorot_normal, glorot_uniform, he_normal, he_uniform):
|
|
11
|
+
w = init(shape, random_state=42)
|
|
12
|
+
assert w.shape == shape
|
|
13
|
+
assert np.all(np.isfinite(w))
|
|
14
|
+
# same seed must give the same weights
|
|
15
|
+
assert np.allclose(w, init(shape, random_state=42))
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_truncated_normal_is_bounded():
|
|
19
|
+
std, mean = 1., 0.
|
|
20
|
+
w = truncated_normal((10000,), mean=mean, std=std, random_state=0)
|
|
21
|
+
assert np.all(np.abs(w - mean) <= 2 * std + 1e-9)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_uniform_initializers_within_limits():
|
|
25
|
+
shape = (8, 4)
|
|
26
|
+
glorot_limit = np.sqrt(6. / (shape[0] + shape[1]))
|
|
27
|
+
assert np.all(np.abs(glorot_uniform(shape, random_state=0)) <= glorot_limit)
|
|
28
|
+
he_limit = np.sqrt(6. / shape[0])
|
|
29
|
+
assert np.all(np.abs(he_uniform(shape, random_state=0)) <= he_limit)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
if __name__ == "__main__":
|
|
33
|
+
pytest.main()
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
from sklearn.datasets import load_iris
|
|
4
|
+
from sklearn.model_selection import train_test_split
|
|
5
|
+
from sklearn.preprocessing import StandardScaler, MinMaxScaler, OneHotEncoder
|
|
6
|
+
|
|
7
|
+
from optiml.ml.tests._datasets import load_boston
|
|
8
|
+
from optiml.ml.neural_network import NeuralNetworkRegressor, NeuralNetworkClassifier
|
|
9
|
+
from optiml.ml.neural_network.activations import sigmoid, softmax, linear, relu
|
|
10
|
+
from optiml.ml.neural_network.layers import FullyConnected
|
|
11
|
+
from optiml.ml.neural_network.losses import mean_squared_error, mean_absolute_error, categorical_cross_entropy
|
|
12
|
+
from optiml.ml.neural_network.regularizers import L2
|
|
13
|
+
from optiml.opti.unconstrained import ProximalBundle
|
|
14
|
+
from optiml.opti.unconstrained.line_search import Newton
|
|
15
|
+
from optiml.opti.unconstrained.stochastic import Adam, StochasticGradientDescent
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_perceptron_regressor_with_line_search_optimizer():
|
|
19
|
+
# aka linear regression
|
|
20
|
+
X, y = load_boston(return_X_y=True)
|
|
21
|
+
net = NeuralNetworkRegressor((FullyConnected(13, 1, linear, fit_intercept=False),),
|
|
22
|
+
loss=mean_squared_error, optimizer=Newton).fit(X, y)
|
|
23
|
+
assert np.allclose(net.coefs_[0], net.loss.x_star())
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_perceptron_ridge_regressor_with_line_search_optimizer():
|
|
27
|
+
# aka ridge regression
|
|
28
|
+
X, y = load_boston(return_X_y=True)
|
|
29
|
+
lmbda = 0.1
|
|
30
|
+
net = NeuralNetworkRegressor((FullyConnected(13, 1, linear, coef_reg=L2(lmbda), fit_intercept=False),),
|
|
31
|
+
loss=mean_squared_error, optimizer=Newton).fit(X, y)
|
|
32
|
+
assert np.allclose(net.coefs_[0], net.loss.x_star())
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_l2_neural_network_regressor_with_stochastic_optimizer():
|
|
36
|
+
X, y = load_boston(return_X_y=True)
|
|
37
|
+
X_scaled = StandardScaler().fit_transform(X)
|
|
38
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
39
|
+
net = NeuralNetworkRegressor((FullyConnected(13, 13, sigmoid),
|
|
40
|
+
FullyConnected(13, 1, linear)),
|
|
41
|
+
loss=mean_squared_error, optimizer=StochasticGradientDescent,
|
|
42
|
+
learning_rate=0.01, momentum_type='nesterov', momentum=0.9)
|
|
43
|
+
net.fit(X_train, y_train)
|
|
44
|
+
assert net.score(X_test, y_test) >= 0.83
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_neural_network_regressor_with_mini_batches():
|
|
48
|
+
# exercises the mini-batch path of the stochastic optimizers
|
|
49
|
+
X, y = load_boston(return_X_y=True)
|
|
50
|
+
X_scaled = StandardScaler().fit_transform(X)
|
|
51
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
52
|
+
net = NeuralNetworkRegressor((FullyConnected(13, 13, relu),
|
|
53
|
+
FullyConnected(13, 1, linear)),
|
|
54
|
+
loss=mean_squared_error, optimizer=StochasticGradientDescent,
|
|
55
|
+
learning_rate=0.01, max_iter=100, batch_size=32)
|
|
56
|
+
net.fit(X_train, y_train)
|
|
57
|
+
# mini-batch training must run end-to-end and produce finite predictions
|
|
58
|
+
assert np.all(np.isfinite(net.predict(X_test)))
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_l1_neural_network_regressor_with_proximal_bundle():
|
|
62
|
+
X, y = load_boston(return_X_y=True)
|
|
63
|
+
X_scaled = StandardScaler().fit_transform(X)
|
|
64
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
65
|
+
net = NeuralNetworkRegressor((FullyConnected(13, 13, relu),
|
|
66
|
+
FullyConnected(13, 1, linear)),
|
|
67
|
+
loss=mean_absolute_error, optimizer=ProximalBundle, max_iter=150)
|
|
68
|
+
net.fit(X_train, y_train)
|
|
69
|
+
assert net.score(X_test, y_test) >= 0.83
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def test_neural_network_classifier_with_stochastic_optimizer():
|
|
73
|
+
X, y = load_iris(return_X_y=True)
|
|
74
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
75
|
+
ohe = OneHotEncoder(sparse_output=False).fit(y.reshape(-1, 1))
|
|
76
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
77
|
+
net = NeuralNetworkClassifier((FullyConnected(4, 4, sigmoid),
|
|
78
|
+
FullyConnected(4, 4, sigmoid),
|
|
79
|
+
FullyConnected(4, 3, softmax)),
|
|
80
|
+
loss=categorical_cross_entropy, optimizer=Adam, learning_rate=0.01)
|
|
81
|
+
net.fit(X_train, ohe.transform(y_train.reshape(-1, 1)))
|
|
82
|
+
assert net.score(X_test, ohe.transform(y_test.reshape(-1, 1))) >= 0.95
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
pytest.main()
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
from sklearn.datasets import load_iris
|
|
4
|
+
from sklearn.model_selection import train_test_split
|
|
5
|
+
from sklearn.multiclass import OneVsRestClassifier as OVR
|
|
6
|
+
from sklearn.preprocessing import MinMaxScaler
|
|
7
|
+
|
|
8
|
+
from optiml.ml.svm import SVC
|
|
9
|
+
from optiml.ml.svm.kernels import gaussian
|
|
10
|
+
from optiml.ml.svm.losses import hinge, squared_hinge
|
|
11
|
+
from optiml.opti.constrained import ProjectedGradient, ActiveSet, InteriorPoint, FrankWolfe
|
|
12
|
+
from optiml.opti.unconstrained import ProximalBundle
|
|
13
|
+
from optiml.opti.unconstrained.line_search import SteepestGradientDescent, ConjugateGradient, Newton, BFGS, LBFGS
|
|
14
|
+
from optiml.opti.unconstrained.stochastic import (StochasticGradientDescent, Adam, AMSGrad,
|
|
15
|
+
AdaMax, AdaGrad, AdaDelta, RMSProp)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_solve_primal_l1_svc_with_line_search_optimizers():
|
|
19
|
+
X, y = load_iris(return_X_y=True)
|
|
20
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
21
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
22
|
+
|
|
23
|
+
svc = OVR(SVC(loss=hinge, optimizer=SteepestGradientDescent))
|
|
24
|
+
svc = svc.fit(X_train, y_train)
|
|
25
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
26
|
+
|
|
27
|
+
svc = OVR(SVC(loss=hinge, optimizer=ConjugateGradient))
|
|
28
|
+
svc = svc.fit(X_train, y_train)
|
|
29
|
+
# CG only crawls on the nonsmooth multiclass hinge primal, so just check the score here;
|
|
30
|
+
# its convergence to f* is exercised on the better-conditioned SVR problem
|
|
31
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
32
|
+
|
|
33
|
+
svc = OVR(SVC(loss=hinge, optimizer=Newton))
|
|
34
|
+
svc = svc.fit(X_train, y_train)
|
|
35
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
36
|
+
|
|
37
|
+
svc = OVR(SVC(loss=hinge, optimizer=BFGS))
|
|
38
|
+
svc = svc.fit(X_train, y_train)
|
|
39
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
40
|
+
|
|
41
|
+
svc = OVR(SVC(loss=hinge, optimizer=LBFGS))
|
|
42
|
+
svc = svc.fit(X_train, y_train)
|
|
43
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_solve_primal_l1_svc_with_stochastic_optimizers():
|
|
47
|
+
# On the nonsmooth multiclass hinge primal the stochastic optimizers converge
|
|
48
|
+
# too slowly / too erratically to reliably meet a fixed optimality-gap tolerance
|
|
49
|
+
# across platforms and seeds, so here we only check the score; their convergence
|
|
50
|
+
# to f* is verified rigorously on the (single, well-conditioned) SVR problem.
|
|
51
|
+
X, y = load_iris(return_X_y=True)
|
|
52
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
53
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
54
|
+
|
|
55
|
+
for optimizer, kwargs in ((StochasticGradientDescent, {}), (Adam, {}), (AMSGrad, {}), (AdaMax, {}),
|
|
56
|
+
(AdaGrad, {}), (AdaDelta, {'learning_rate': 1.}), (RMSProp, {})):
|
|
57
|
+
svc = OVR(SVC(loss=hinge, optimizer=optimizer, **kwargs)).fit(X_train, y_train)
|
|
58
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_solve_primal_l1_svc_with_proximal_bundle():
|
|
62
|
+
X, y = load_iris(return_X_y=True)
|
|
63
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
64
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
65
|
+
svc = OVR(SVC(loss=hinge, optimizer=ProximalBundle))
|
|
66
|
+
svc = svc.fit(X_train, y_train)
|
|
67
|
+
# the proximal bundle method only crawls on the nonsmooth multiclass hinge primal, so check only the score
|
|
68
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def test_solve_dual_l1_svc_with_smo():
|
|
72
|
+
X, y = load_iris(return_X_y=True)
|
|
73
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
74
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
75
|
+
smo = OVR(SVC(loss=hinge, kernel=gaussian, dual=True, optimizer='smo')).fit(X_train, y_train)
|
|
76
|
+
# SMO must reach essentially the same solution as the reference QP solver (cvxopt)
|
|
77
|
+
ref = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=False, dual=True, optimizer='cvxopt')).fit(X_train, y_train)
|
|
78
|
+
assert (smo.predict(X_test) == ref.predict(X_test)).mean() >= 0.97
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def test_solve_dual_l1_svc_with_cvxopt():
|
|
82
|
+
X, y = load_iris(return_X_y=True)
|
|
83
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
84
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
85
|
+
|
|
86
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True, dual=True, optimizer='cvxopt'))
|
|
87
|
+
svc = svc.fit(X_train, y_train)
|
|
88
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
89
|
+
|
|
90
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=False, dual=True, optimizer='cvxopt'))
|
|
91
|
+
svc = svc.fit(X_train, y_train)
|
|
92
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_solve_dual_l1_svc_with_reg_intercept_with_bcqp_optimizers():
|
|
96
|
+
X, y = load_iris(return_X_y=True)
|
|
97
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
98
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
99
|
+
|
|
100
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True, dual=True, optimizer=ProjectedGradient))
|
|
101
|
+
svc = svc.fit(X_train, y_train)
|
|
102
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
103
|
+
|
|
104
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True, dual=True, optimizer=ActiveSet))
|
|
105
|
+
svc = svc.fit(X_train, y_train)
|
|
106
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
107
|
+
|
|
108
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True, dual=True, optimizer=InteriorPoint))
|
|
109
|
+
svc = svc.fit(X_train, y_train)
|
|
110
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
111
|
+
|
|
112
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True, dual=True, optimizer=FrankWolfe))
|
|
113
|
+
svc = svc.fit(X_train, y_train)
|
|
114
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_solve_dual_l1_svc_with_proximal_bundle():
|
|
118
|
+
X, y = load_iris(return_X_y=True)
|
|
119
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
120
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
121
|
+
|
|
122
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True,
|
|
123
|
+
dual=True, optimizer=ProximalBundle, max_iter=150))
|
|
124
|
+
svc = svc.fit(X_train, y_train)
|
|
125
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
126
|
+
|
|
127
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=False,
|
|
128
|
+
dual=True, optimizer=ProximalBundle, max_iter=150))
|
|
129
|
+
svc = svc.fit(X_train, y_train)
|
|
130
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def test_solve_dual_l1_svc_with_AdaGrad():
|
|
134
|
+
X, y = load_iris(return_X_y=True)
|
|
135
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
136
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
137
|
+
|
|
138
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=True,
|
|
139
|
+
dual=True, optimizer=AdaGrad, learning_rate=1.))
|
|
140
|
+
svc = svc.fit(X_train, y_train)
|
|
141
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
142
|
+
|
|
143
|
+
svc = OVR(SVC(loss=hinge, kernel=gaussian, reg_intercept=False,
|
|
144
|
+
dual=True, optimizer=AdaGrad, learning_rate=1.))
|
|
145
|
+
svc = svc.fit(X_train, y_train)
|
|
146
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def test_solve_primal_l2_svc_with_line_search_optimizers():
|
|
150
|
+
X, y = load_iris(return_X_y=True)
|
|
151
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
152
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
153
|
+
|
|
154
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=SteepestGradientDescent))
|
|
155
|
+
svc = svc.fit(X_train, y_train)
|
|
156
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
157
|
+
|
|
158
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=ConjugateGradient))
|
|
159
|
+
svc = svc.fit(X_train, y_train)
|
|
160
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
161
|
+
|
|
162
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=Newton))
|
|
163
|
+
svc = svc.fit(X_train, y_train)
|
|
164
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
165
|
+
|
|
166
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=BFGS))
|
|
167
|
+
svc = svc.fit(X_train, y_train)
|
|
168
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
169
|
+
|
|
170
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=LBFGS))
|
|
171
|
+
svc = svc.fit(X_train, y_train)
|
|
172
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def test_solve_primal_l2_svc_with_stochastic_optimizers():
|
|
176
|
+
X, y = load_iris(return_X_y=True)
|
|
177
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
178
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
179
|
+
|
|
180
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=StochasticGradientDescent))
|
|
181
|
+
svc = svc.fit(X_train, y_train)
|
|
182
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
183
|
+
|
|
184
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=Adam))
|
|
185
|
+
svc = svc.fit(X_train, y_train)
|
|
186
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
187
|
+
|
|
188
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=AMSGrad))
|
|
189
|
+
svc = svc.fit(X_train, y_train)
|
|
190
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
191
|
+
|
|
192
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=AdaMax))
|
|
193
|
+
svc = svc.fit(X_train, y_train)
|
|
194
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
195
|
+
|
|
196
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=AdaGrad))
|
|
197
|
+
svc = svc.fit(X_train, y_train)
|
|
198
|
+
# AdaGrad converges too slowly on this multiclass problem to reliably hit a fixed
|
|
199
|
+
# optimality-gap tolerance across platforms, so check only the score here
|
|
200
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
201
|
+
|
|
202
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=AdaDelta, learning_rate=1.))
|
|
203
|
+
svc = svc.fit(X_train, y_train)
|
|
204
|
+
# AdaDelta converges too slowly on this multiclass problem to reliably hit a fixed
|
|
205
|
+
# optimality-gap tolerance across platforms, so check only the score here
|
|
206
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
207
|
+
|
|
208
|
+
svc = OVR(SVC(loss=squared_hinge, optimizer=RMSProp))
|
|
209
|
+
svc = svc.fit(X_train, y_train)
|
|
210
|
+
# RMSProp does not reliably converge here, so check only the score
|
|
211
|
+
assert svc.score(X_test, y_test) >= 0.57
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def test_solve_dual_l2_svc_with_cvxopt():
|
|
215
|
+
X, y = load_iris(return_X_y=True)
|
|
216
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
217
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
218
|
+
|
|
219
|
+
svc = OVR(SVC(loss=squared_hinge, kernel=gaussian, reg_intercept=True, dual=True, optimizer='cvxopt'))
|
|
220
|
+
svc = svc.fit(X_train, y_train)
|
|
221
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
222
|
+
|
|
223
|
+
svc = OVR(SVC(loss=squared_hinge, kernel=gaussian, reg_intercept=False, dual=True, optimizer='cvxopt'))
|
|
224
|
+
svc = svc.fit(X_train, y_train)
|
|
225
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def test_solve_dual_l2_svc_with_AdaGrad():
|
|
229
|
+
X, y = load_iris(return_X_y=True)
|
|
230
|
+
X_scaled = MinMaxScaler().fit_transform(X)
|
|
231
|
+
X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, train_size=0.75, random_state=123456)
|
|
232
|
+
|
|
233
|
+
svc = OVR(SVC(loss=squared_hinge, kernel=gaussian, reg_intercept=True,
|
|
234
|
+
dual=True, optimizer=AdaGrad, learning_rate=1.))
|
|
235
|
+
svc = svc.fit(X_train, y_train)
|
|
236
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
237
|
+
|
|
238
|
+
svc = OVR(SVC(loss=squared_hinge, kernel=gaussian, reg_intercept=False,
|
|
239
|
+
dual=True, optimizer=AdaGrad, learning_rate=1.))
|
|
240
|
+
svc = svc.fit(X_train, y_train)
|
|
241
|
+
assert svc.score(X_test, y_test) >= 0.97
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
if __name__ == "__main__":
|
|
245
|
+
pytest.main()
|