Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REVIEW] Add QuasiNewton tests #3135

Merged
merged 4 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- PR #3112: Speed test_array
- PR #3111: Adding Cython to Code Coverage
- PR #3129: Update notebooks README
- PR #3135: Add QuasiNewton tests
- PR #3040: Improved Array Conversion with CumlArrayDescriptor and Decorators
- PR #3134: Improving the Deprecation Message Formatting in Documentation

Expand Down
44 changes: 29 additions & 15 deletions python/cuml/test/test_qn.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import cupy as cp

from cuml.solvers import QN as cuQN
from cuml.preprocessing.model_selection import train_test_split
from cuml.datasets.classification import make_classification
from cuml.metrics import accuracy_score


# todo: add util functions to better compare against precomputed solutions
Expand All @@ -34,22 +37,37 @@ def test_qn(loss, dtype, penalty, l1_strength, l2_strength, fit_intercept):

tol = 1e-6

X = np.array(precomputed_X, dtype=dtype)

if loss == 'sigmoid':
y = np.array(precomputed_y_log, dtype=dtype)
else:
y = np.array(precomputed_y_multi, dtype=dtype)

qn = cuQN(loss=loss, fit_intercept=fit_intercept, l1_strength=l1_strength,
l2_strength=l2_strength, tol=1e-8, output_type="cupy")

qn.fit(X, y)
if loss == 'softmax':
X, y = make_classification(n_samples=5000,
n_informative=10,
n_features=20,
n_classes=4,
dtype=dtype)

X_train, X_test, y_train, y_test = train_test_split(X.astype(dtype),
y.astype(dtype),
stratify=True)
most_class = cp.unique(y)[cp.argmax(cp.bincount(y))]

baseline_preds = cp.array([most_class] * y_test.shape[0], dtype=dtype)
baseline_score = accuracy_score(y_test, baseline_preds)

print(qn.objective)
print(qn.coef_)
y_pred = qn.fit(X_train, y_train).predict(X_test)
cuml_score = accuracy_score(y_test, y_pred)

assert(cuml_score > baseline_score)
assert(cuml_score >= 0.50)

elif loss == 'sigmoid':
X = np.array(precomputed_X, dtype=dtype)
y = np.array(precomputed_y_log, dtype=dtype)
qn.fit(X, y)
print(qn.objective)
print(qn.coef_)

if loss == 'sigmoid':
if penalty == 'none' and l1_strength == 0.0 and l2_strength == 0.0:
if fit_intercept:
assert (qn.objective - 0.40263831615448) < tol
Expand Down Expand Up @@ -198,10 +216,6 @@ def test_qn(loss, dtype, penalty, l1_strength, l2_strength, fit_intercept):

print()

elif loss == 'softmax':
pytest.skip("Better initial conditions for softmax tests are "
"in progress.")

# todo add tests for softmax dtype=np.float64
# elasticnet for this points converged to different solution
# if loss == 'softmax':
Expand Down