-
-
Notifications
You must be signed in to change notification settings - Fork 25.8k
[WIP] New assert helpers for model comparison and fit reset checks #4841
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
Conversation
3b8e8f7
to
e70af31
Compare
sklearn/utils/testing.py
Outdated
"'regressor', 'transformer', 'cluster' or None, got" | ||
" %s." % repr(type_filter)) | ||
"'regressor', 'transformer', 'cluster' or None," | ||
"got %s." % repr(type_filter)) |
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.
you forgot a space between ,
and got
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.
Thanks :)
e70af31
to
53a7637
Compare
@TomDLT done :) |
I had meant that you might propose this in #4162 where at least it has one motivating example. We'll see how this is received. |
sklearn/utils/testing.py
Outdated
# Check if the method(X) returns the same for both models. | ||
res1 = getattr(estimator1, method)(X) | ||
res2 = getattr(estimator2, method)(X) | ||
same_model = (res1.shape == res2.shape) and np.allclose(res1, res2) |
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 will not handle transforms with sparse output.
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.
fixed
Thanks for the quick review!
Oh :P Anyway I'll rebase #4162 upon this! So this can be reviewed separately... :)
|
Within limits, check_fit_reset will check this for all estimators. The On 11 June 2015 at 00:08, Raghav R V notifications@github.com wrote:
|
Might I hedge my bets and say "for now"? On 11 June 2015 at 00:15, Raghav R V notifications@github.com wrote:
|
haha okay :) |
53a7637
to
3341afb
Compare
sklearn/utils/testing.py
Outdated
verbose=True): | ||
"""Check if two sparse arrays are equal up to the desired tolerance. | ||
|
||
It compares the difference between `actual` and `desired` to |
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.
The absolute difference, right?
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.
Do you mean absolute difference as in the absolute difference between -1 and 1 is 0?
(I am pretty sure I misunderstood what u said... could you please expand a bit?)
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.
The test compares the absolute value of the difference to the thing you mention. Not the difference. For more information take a look at the documentation of np.allclose
.
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.
That helps! Thanks
I agree with @jnothman that it's hard to review this PR as it is, because it's not clear how reusable these helpers are across the codebase. Are there any places in the existing tests where these helpers could be used? Out of context as they are now, it's hard to say whether the API and implementation choices in this PR are the right ones. |
|
||
|
||
def test_qda_same_model(): | ||
# NRT to make sure the rotations_ attribute is correctly compared |
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.
Does this mean non-regression test? In general, could you document tests with docstrings with clear first lines?
For this test in particular, I don't see what it would test that the one above wouldn't, as LinearSVC
and KMeans
also have fitted attributes of their own. Am I missing anything?
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.
Sorry for the lack of clarity in the comment... @amueller had pointed out in one comment that QDA was not correctly compared by the previous implementation of the assert_same_model
. That was because rotations was a list of numpy arrays and so I thought it would be worthwhile to add a NRT for QDA alone!
Will update the comment to make it more clear!
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.
Good, I think the docstring should explain this.
I would also suggest using stub estimators to more explicitly test these helpers. You could test this with something like
class Dummy(object):
pass
def test_compare_attributes():
a = Dummy()
also_a = Dummy()
not_a = Dummy()
a.foo_ = [3, 10]
also_a.foo_ = [3, 10]
not_a.foo_ = [42]
# assert a and also_a are the same model and not_a is not
This would make both the test more self-descriptive and any failures easier to pinpoint. You should probably use the classes from tests/test_base.py
Sorry, I was pretty chaotic in reviewing this, since I jumped from minor nitpicks to high-level concerns and back. I think it'd be best to discuss the high-level things first, just in case we decide to implement things completely differently. For example as Joel suggested we might want to just densify sparse attributes and leverage |
assert False, msg | ||
assert_allclose(val1, val2, rtol=rtol, atol=atol, err_msg=msg) | ||
else: | ||
assert False, msg |
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.
7f8b5a5
to
35fdeaa
Compare
@jnothman Any suggestions on how to salvage this? Should we proceed on this? |
I'd be interested in the |
Please go ahead and pick it up if you wish to :) Thanks for working on that! |
I think you'll find getting this to merge is a bit of an uphill battle. In your case, Tim (and perhaps a variant applies in general), I also wonder whether pickle equality is what you want... |
I'd like some discussion of this, perhaps at the sprint. Being able to easily asset that models are equal could simplify many tests or make them more rigorous |
(I've not done any work on this, if someone else wants to go ahead please do!) |
Sure, thanks for finding a way to ease me in to the sprint :)
…-------- Original Message --------
From: "(Venkat) Raghav (Rajagopalan)" <notifications@github.com>
Sent: June 5, 2017 9:03:04 AM GMT+02:00
To: scikit-learn/scikit-learn <scikit-learn@noreply.github.com>
Cc: Vlad Niculae <vlad@vene.ro>, Mention <mention@noreply.github.com>
Subject: Re: [scikit-learn/scikit-learn] [WIP] New assert helpers for model comparison and fit reset checks (#4841)
I can revive this if someone is interested in reviewing it... @jnothman is interested. @vene would you be available for the 2nd review?
|
I think that this has real value for estimator developers, has the
potential to strengthen lots of tests, and will allow us to write tests
more clearly without caring what type of estimator we are applying the
assertion to. Its main downside is that it may be overkill and slow, that
it requires us to define what we mean by equivalent after the fact in a way
that may not be universal with respect to existing and future estimators,
and that it requires a lot of messy data structure traversal. Overall I
think it's the right way to go.
…On 5 Jun 2017 5:10 pm, "Vlad Niculae" ***@***.***> wrote:
Sure, thanks for finding a way to ease me in to the sprint :)
-------- Original Message --------
From: "(Venkat) Raghav (Rajagopalan)" ***@***.***>
Sent: June 5, 2017 9:03:04 AM GMT+02:00
To: scikit-learn/scikit-learn ***@***.***>
Cc: Vlad Niculae ***@***.***>, Mention ***@***.***>
Subject: Re: [scikit-learn/scikit-learn] [WIP] New assert helpers for
model comparison and fit reset checks (#4841)
I can revive this if someone is interested in reviewing it... @jnothman is
interested. @vene would you be available for the 2nd review?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4841 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEz6xa18sZdMMRRJLyka74p_qMefLYlks5sA6nugaJpZM4E-XR4>
.
|
Last time I checked, I felt it was not worth the effort, and I haven't thought about it since. Can someone maybe give use-cases? |
I like the thinking that went behind this but this seems complicated, I don't know we're at a point where this is needed yet |
At this point if we're going to define estimator equality we probably needs a SLEP, and I haven't seen much of a usecase for it. Happy to have a fresh discussion on a SLEP of folks are interested though. |
Split from #3907 and #4162
assert_same_model
,assert_not_same_model
,assert_fitted_attributes_equal
andassert_safe_sparse_allclose
.TODO
assert_safe_sparse_allcose
- To support sparse/dense matrices. (naming inspired fromsafe_sparse_dot
)assert_same_model
/assert_not_same_model
/assert_fitted_attributes_equal
fit
.Partially fixes : #406