You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stronger common tests for setting init params? / check_estimator #7738
The common tests let this pass, though that should wreck havoc on get_params and set_params.
I haven't looked into it but I think the tests should fail on this.
Thanks for wanting to contribute.
I think we want to check that calling set_params is equivalent to passing parameters in __init__.
Actually, I'm a bit appalled to see that we never test set_params at all, it seems.
Can you please double check that with the current common tests the example I gave above passes?
The simplest test I can think of is to add a line to check_parameters_default_constructible
that does estimator.set_params(**estimator.get_params()). That should fail with the example I gave.
Does "with the current common tests" mean use check_estimator?
Yes, your example can pass check_estimator but fail on set_params. However it is quite easy to bypass. The example below will pass check_estimator and set_params.
Written like this the param is actually completely transparent to get_param and set_param. I think it can only be detected by just checking the Estimator class param attribute.
from sklearn.utils.testing import assert_false
for param_name in Estimator._get_param_names():
assert_false(hasattr(Estimator, param_name))
@absolutelyNoWarranty can you give details of the assertion error? No, that's not normal.
And why would the hasattr fail? There's a property, right?
And it's not so much about preventing people from using properties. If they are transparent in a way that works with set_params, get_params and __init__ then this could work.
The test is mostly to avoid people implementing estimators that clearly don't work with set_params and get_params.
I think I had some virtualenv issues on my end. Only DummyClassifier throws an assertion error now.
# sklearn=0.19.dev0
from sklearn.dummy import DummyClassifier
from sklearn.utils.estimator_checks import check_estimator
check_estimator(DummyClassifier)
#Output:
#AssertionError: TypeError not raised by fit
Yes the hasattr would be True. I thought that could be used in a blanket check to prevent all properties with the same name as a parameter in __init__.
And it's not so much about preventing people from using properties. If they are transparent in a way that works with set_params, get_params and init then this could work.
If I'm understanding this correctly, idempotent hyper-parameters is not a goal? It would be acceptable to have my_param_dict != estimator.set_params(**my_param_dict).get_params()?
I'm not sure. Maybe that should be the bar?
I though estimator.set_params(estimator.get_params()) might be enough. But yours is stronger and why not. Least surprise I guess.
Yes, we need setters in such cases, and the transformation needs to be invertible.
I've not read all the commentary above yet, but hasattr(Estimator, param_name) is not an appropriate test. Consider step names in Pipelines, for instance. But testing the relationship between get_params (maybe only for deep=False?) and set_params is appropriate.
After doing a search it turns out that TfidfVectorizer uses a lot of param-properties so I see why hasattr is not appropriate (though I don't understand yet why Pipeline would be incompatible.)
p = Pipeline([('clf', LogisticRegression())])
assert(p.get_params()['clf'])
p.set_params(clf=SVC())
assert(not hasattr(p, 'clf'))
On 26 October 2016 at 14:58, absolutelyNoWarranty [email protected]
wrote:
After doing a search it turns out that TfidfVectorizer uses a lot of
param-properties so I see why hasattr is not appropriate (though I don't
understand yet why Pipeline would be incompatible.)
In PR 7477, the bug is that set_params just flat out doesn't work. In the review the comment is that the PR is against the API.
What I am wondering is if something like the below is acceptable.
It solves set_param but and still uses the property to do error-checking. Putting the error-checking in alpha.setter ensures that regardless of whether you use __init__ or set_params, any modification to alpha will go through the error checking.
class MultinomialNB(BaseDiscreteNB):
def __init__(self, alpha=1.0, fit_prior=True, class_prior=None):
self.alpha = alpha # assign to alpha not _alpha
self.fit_prior = fit_prior
self.class_prior = class_prior
@property
def alpha(self):
return self._alpha
@alpha.setter
def alpha(self, value):
# possible error checking but no modifying of _alpha with logic
if value < _ALPHA_MIN:
raise ValueError('alpha too small')
self._alpha = value
Problem 2:
I don't really understand what is meant by invertibility. If it is just like TfidfVectorizer where values simply pass through to another object then I understand but is logic like +1, -1 actually something to be allowed? Where would this be useful?
Activity
absolutelyNoWarranty commentedon Oct 24, 2016
I am interested in contributing. It sounds to me like you want
check_estimatorto verify that there are no properties which are parameter names?amueller commentedon Oct 24, 2016
Thanks for wanting to contribute.
I think we want to check that calling
set_paramsis equivalent to passing parameters in__init__.Actually, I'm a bit appalled to see that we never test
set_paramsat all, it seems.Can you please double check that with the current common tests the example I gave above passes?
The simplest test I can think of is to add a line to
check_parameters_default_constructiblethat does
estimator.set_params(**estimator.get_params()). That should fail with the example I gave.absolutelyNoWarranty commentedon Oct 25, 2016
Does "with the current common tests" mean use
check_estimator?Yes, your example can pass
check_estimatorbut fail onset_params. However it is quite easy to bypass. The example below will passcheck_estimatorandset_params.Written like this the
paramis actually completely transparent toget_paramandset_param. I think it can only be detected by just checking theEstimatorclassparamattribute.absolutelyNoWarranty commentedon Oct 25, 2016
Is it normal for
check_estimatorto throwAssertionErrorsusing build-in sklearn estimators when running from the command line? I am getting a lot ofamueller commentedon Oct 25, 2016
@absolutelyNoWarranty can you give details of the assertion error? No, that's not normal.
And why would the
hasattrfail? There's a property, right?And it's not so much about preventing people from using properties. If they are transparent in a way that works with
set_params,get_paramsand__init__then this could work.The test is mostly to avoid people implementing estimators that clearly don't work with
set_paramsandget_params.absolutelyNoWarranty commentedon Oct 25, 2016
I think I had some virtualenv issues on my end. Only DummyClassifier throws an assertion error now.
Yes the
hasattrwould be True. I thought that could be used in a blanket check to prevent all properties with the same name as a parameter in__init__.If I'm understanding this correctly, idempotent hyper-parameters is not a goal? It would be acceptable to have
my_param_dict != estimator.set_params(**my_param_dict).get_params()?amueller commentedon Oct 25, 2016
I'm not sure. Maybe that should be the bar?
I though
estimator.set_params(estimator.get_params())might be enough. But yours is stronger and why not. Least surprise I guess.jnothman commentedon Oct 25, 2016
Yes, we need setters in such cases, and the transformation needs to be invertible.
I've not read all the commentary above yet, but
hasattr(Estimator, param_name)is not an appropriate test. Consider step names in Pipelines, for instance. But testing the relationship betweenget_params(maybe only fordeep=False?) andset_paramsis appropriate.(*"wreck havoc" -> "wreak havoc")
absolutelyNoWarranty commentedon Oct 26, 2016
After doing a search it turns out that
TfidfVectorizeruses a lot of param-properties so I see whyhasattris not appropriate (though I don't understand yet whyPipelinewould be incompatible.)jnothman commentedon Oct 26, 2016
p = Pipeline([('clf', LogisticRegression())])
assert(p.get_params()['clf'])
p.set_params(clf=SVC())
assert(not hasattr(p, 'clf'))
On 26 October 2016 at 14:58, absolutelyNoWarranty [email protected]
wrote:
absolutelyNoWarranty commentedon Oct 26, 2016
I meant for
hasattrto check against._get_param_names()so for Pipeline the only disallowed parameter would besteps.I opened a PR with the modification suggested by @amueller .
absolutelyNoWarranty commentedon Nov 13, 2016
In PR 7477, the bug is that
set_paramsjust flat out doesn't work. In the review the comment is that the PR is against the API.What I am wondering is if something like the below is acceptable.
It solves
set_parambut and still uses the property to do error-checking. Putting the error-checking inalpha.setterensures that regardless of whether you use__init__orset_params, any modification to alpha will go through the error checking.Problem 2:
I don't really understand what is meant by invertibility. If it is just like
TfidfVectorizerwhere values simply pass through to another object then I understand but is logic like+1,-1actually something to be allowed? Where would this be useful?jnothman commentedon Nov 16, 2016
Something like that can be acceptable, used sparingly: we rather use the convention of validating in
fit.