-
Notifications
You must be signed in to change notification settings - Fork 540
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
Implement hypothesis-based tests for linear models #4952
Merged
rapids-bot
merged 31 commits into
rapidsai:branch-22.12
from
csadorf:fea-hypothesis-based-testing-for-cpu-gpu-comparison
Nov 11, 2022
Merged
Changes from 27 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
fe6d204
Implement first prototype for hypothesis-based linear model testing.
csadorf cb98126
Fix copyright years.
csadorf fed4b6c
Adjust comments spacing to make flake8 happy.
csadorf ff26882
Fix and clarify assumptions.
csadorf 01ee031
Remove max_examples override.
csadorf 0b1a5d8
Document search strategies.
csadorf d7af241
Mention hypothesis strategies in developer guide.
csadorf dedc1ba
Datasets output values shape is either (n_samples,) or (n_samples, n_…
csadorf 80ae1cb
fixup! Datasets output values shape is either (n_samples,) or (n_samp…
csadorf fab7c72
Load hypothesis profiles based on test configuration.
csadorf 23966fa
Increase min number of samples for split_datasets() default datasets.
csadorf 8f18663
Slightly adjust strategy definition and extend documentation.
csadorf fa69180
Extend and further clarify needed assumptions.
csadorf 31346db
Declare expected failure for test_linear_regression_model_default test.
csadorf 53dcf8f
Drop assumption for unused variable.
csadorf f2a0953
Improve customizability of hypothesis regression_datasets() strategy.
csadorf c8c100b
Adjust hypothesis profiles for more reasonable runtimes.
csadorf bf71841
Suppress data_too_large hypothesis healthcheck.
csadorf 258b233
Use a more sensible strategy for n_informative.
csadorf 79ad0cd
Improve implementation of the split_datasets() strategy.
csadorf 3dd7036
Adjust default sizes for standard_regression_datasets strategy.
csadorf e9beee9
fixup! Improve customizability of hypothesis regression_datasets() st…
csadorf c17c991
Add additional test for standard_dataset strategy.
csadorf 35dfbf1
fixup! Improve implementation of the split_datasets() strategy.
csadorf af49c89
Find hypothesis strategy to pass test_linear_regression_model_default.
csadorf 779a23f
Refactor required assumptions into dedicated functions.
csadorf af41aa2
Implement test_linear_regression_model_default_generalized as stop-gap.
csadorf 1f99c1f
Minor fixup of the documentation.
csadorf 22fcb64
Suppress too_slow healtcheck for flaky test.
csadorf 117496a
Merge branch 'branch-22.12' into fea-hypothesis-based-testing-for-cpu…
csadorf 706f1ac
Suppress too_slow healthcheck for test_split_regression_datasets.
csadorf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,250 @@ | ||
# Copyright (c) 2022, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from hypothesis import assume | ||
from hypothesis.extra.numpy import arrays, floating_dtypes | ||
from hypothesis.strategies import composite, integers, just, none, one_of | ||
from sklearn.datasets import make_regression | ||
from sklearn.model_selection import train_test_split | ||
|
||
|
||
@composite | ||
def standard_datasets( | ||
draw, | ||
dtypes=floating_dtypes(), | ||
n_samples=integers(min_value=0, max_value=200), | ||
n_features=integers(min_value=0, max_value=200), | ||
*, | ||
n_targets=just(1), | ||
): | ||
""" | ||
Returns a strategy to generate standard estimator input datasets. | ||
|
||
Parameters | ||
---------- | ||
dtypes: SearchStrategy[np.dtype], default=floating_dtypes() | ||
Returned arrays will have a dtype drawn from these types. | ||
n_samples: SearchStrategy[int],\ | ||
default=integers(min_value=0, max_value=200) | ||
Returned arrays will have number of rows drawn from these values. | ||
n_features: SearchStrategy[int],\ | ||
default=integers(min_value=0, max_values=200) | ||
Returned arrays will have number of columns drawn from these values. | ||
n_targets: SearchStrategy[int], default=just(1) | ||
Determines the number of targets returned datasets may contain. | ||
|
||
Returns | ||
------- | ||
X: SearchStrategy[array] (n_samples, n_features) | ||
The search strategy for input samples. | ||
y: SearchStrategy[array] (n_samples,) or (n_samples, n_targets) | ||
The search strategy for output samples. | ||
|
||
""" | ||
xs = draw(n_samples) | ||
ys = draw(n_features) | ||
X = arrays(dtype=dtypes, shape=(xs, ys)) | ||
y = arrays(dtype=dtypes, shape=(xs, draw(n_targets))) | ||
return draw(X), draw(y) | ||
|
||
|
||
def combined_datasets_strategy(* datasets, name=None, doc=None): | ||
""" | ||
Combine multiple datasets strategies into a single datasets strategy. | ||
|
||
This function will return a new strategy that will build the provided | ||
strategy functions with the common parameters (dtypes, n_samples, | ||
n_features) and then draw from one of them. | ||
|
||
Parameters: | ||
----------- | ||
* datasets: list[Callable[[dtypes, n_samples, n_features], SearchStrategy]] | ||
A list of functions that return a dataset search strategy when called | ||
with the shown arguments. | ||
name: The name of the returned search strategy, default="datasets" | ||
Defaults to a combination of names of the provided dataset stratgegy | ||
functions. | ||
doc: The doc-string of the returned search strategy, default=None | ||
Defaults to a generic doc-string. | ||
|
||
Returns | ||
------- | ||
Datasets search strategy: SearchStrategy[array, array] | ||
""" | ||
|
||
@composite | ||
def strategy( | ||
draw, | ||
dtypes=floating_dtypes(), | ||
n_samples=integers(min_value=0, max_value=200), | ||
n_features=integers(min_value=0, max_value=200) | ||
): | ||
"""Datasets strategy composed of multiple datasets strategies.""" | ||
datasets_strategies = ( | ||
dataset(dtypes, n_samples, n_features) for dataset in datasets) | ||
return draw(one_of(datasets_strategies)) | ||
|
||
strategy.__name__ = "datasets" if name is None else name | ||
if doc is not None: | ||
strategy.__doc__ = doc | ||
|
||
return strategy | ||
|
||
|
||
@composite | ||
def split_datasets( | ||
draw, | ||
datasets, | ||
test_sizes=None, | ||
): | ||
""" | ||
Split a generic search strategy for datasets into test and train subsets. | ||
|
||
The resulting split is guaranteed to have at least one sample in both the | ||
train and test split respectively. | ||
|
||
Note: This function uses the sklearn.model_selection.train_test_split | ||
function. | ||
|
||
See also: | ||
standard_datasets(): A search strategy for datasets that can serve as input | ||
to this strategy. | ||
|
||
Parameters | ||
---------- | ||
datasets: SearchStrategy[dataset] | ||
A search strategy for datasets. | ||
test_sizes: SearchStrategy[float] | SearchStrategy[int], default=None | ||
A search strategy for the test size. Must be provided as a search | ||
strategy for integers or floats. Integers should be bound by one and | ||
the sample size, floats should be between 0 and 1.0. Defaults to | ||
a search strategy that will generate a valid unbiased split. | ||
|
||
Returns | ||
------- | ||
(X_train, X_test, y_train, y_test): SearchStrategy[4 * array] | ||
The train-test split of the input and output samples drawn from | ||
the provided datasets search strategy. | ||
""" | ||
X, y = draw(datasets) | ||
assume(len(X) > 1) | ||
|
||
# Determine default value for test_sizes | ||
if test_sizes is None: | ||
test_sizes = integers(1, max(1, len(X) - 1)) | ||
|
||
test_size = draw(test_sizes) | ||
|
||
# Check assumptions for test_size | ||
if isinstance(test_size, float): | ||
assume(int(len(X) * test_size) > 0) | ||
assume(int(len(X) * (1.0 - test_size)) > 0) | ||
elif isinstance(test_size, int): | ||
assume(1 < test_size < len(X)) | ||
|
||
return train_test_split(X, y, test_size=test_size) | ||
|
||
|
||
@composite | ||
def standard_regression_datasets( | ||
draw, | ||
dtypes=floating_dtypes(), | ||
n_samples=integers(min_value=100, max_value=200), | ||
n_features=integers(min_value=100, max_value=200), | ||
*, | ||
n_informative=None, | ||
n_targets=just(1), | ||
bias=just(0.0), | ||
effective_rank=none(), | ||
tail_strength=just(0.5), | ||
noise=just(0.0), | ||
shuffle=just(True), | ||
random_state=None, | ||
): | ||
""" | ||
Returns a strategy to generate regression problem input datasets. | ||
|
||
Note: | ||
This function uses the sklearn.datasets.make_regression function to | ||
generate the regression problem from the provided search strategies. | ||
|
||
|
||
Parameters | ||
---------- | ||
dtypes: SearchStrategy[np.dtype] | ||
Returned arrays will have a dtype drawn from these types. | ||
n_samples: SearchStrategy[int] | ||
Returned arrays will have number of rows drawn from these values. | ||
n_features: SearchStrategy[int] | ||
Returned arrays will have number of columns drawn from these values. | ||
n_informative: SearchStrategy[int], default=none | ||
A search strategy for the number of informative features. If none, | ||
will use 10% of the actual number of features, but not less than 1 | ||
unless the number of features is zero. | ||
n_targets: SearchStrategy[int], default=just(1) | ||
A search strategy for the number of targets, that means the number of | ||
columns of the returned y output array. | ||
bias: SearchStrategy[float], default=just(0.0) | ||
A search strategy for the bias term. | ||
effective_rank=none() | ||
If not None, a search strategy for the effective rank of the input data | ||
for the regression problem. See sklearn.dataset.make_regression() for a | ||
detailed explanation of this parameter. | ||
tail_strength: SearchStrategy[float], default=just(0.5) | ||
See sklearn.dataset.make_regression() for a detailed explanation of | ||
this parameter. | ||
noise: SearchStrategy[float], default=just(0.0) | ||
A search strategy for the standard deviation of the gaussian noise. | ||
shuffle: SearchStrategy[bool], default=just(True) | ||
A boolean search strategy to determine whether samples and features | ||
are shuffled. | ||
random_state: int, RandomState instance or None, default=None | ||
Pass a random state or integer to determine the random number | ||
generation for data set generation. | ||
|
||
Returns | ||
------- | ||
(X, y): SearchStrategy[array, array] | ||
A search strategy for a tuple of two arrays subject to the | ||
provided parameters. | ||
""" | ||
n_features_ = draw(n_features) | ||
if n_informative is None: | ||
n_informative = just(max(min(n_features_, 1), int(0.1 * n_features_))) | ||
X, y = make_regression( | ||
n_samples=draw(n_samples), | ||
n_features=n_features_, | ||
n_informative=draw(n_informative), | ||
n_targets=draw(n_targets), | ||
bias=draw(bias), | ||
effective_rank=draw(effective_rank), | ||
tail_strength=draw(tail_strength), | ||
noise=draw(noise), | ||
shuffle=draw(shuffle), | ||
random_state=random_state, | ||
) | ||
dtype_ = draw(dtypes) | ||
return X.astype(dtype_), y.astype(dtype_) | ||
|
||
|
||
regression_datasets = combined_datasets_strategy( | ||
standard_datasets, standard_regression_datasets, | ||
name="regression_datasets", | ||
doc=""" | ||
Returns strategy for the generation of regression problem datasets. | ||
|
||
Drawn from the standard_datasets and the standard_regression_datasets | ||
strategies. | ||
""" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A common issue we've had is that sometimes it is very hard to diagnose the error or magnitude of errors when tests fail, adding some printing when failing here might be highly benefitial
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function does not directly fail tests, but only computes the difference between arrays
a
andb
similar toarray_equal()
. I use it as a target function to steer hypothesis towards larger errors.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in #4973 .