-
Notifications
You must be signed in to change notification settings - Fork 398
Fix a bug that led to double-registration #781
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
Fix a bug that led to double-registration #781
Conversation
After cloning a net, _module, _criteria, and _optimizers are already populated. Then, when loading params, there is yet another registration, i.e. a double registration. As a consequence, there would be two 'modules' etc. This is a fix for that.
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.
Thank you for the fix!
if self.init_context_ == 'module': | ||
# make sure to not double register -- this should never happen, but | ||
# still better to check | ||
if (self.init_context_ == 'module') and (name not in self._modules): | ||
self._modules = self._modules[:] + [name] |
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.
I think this is what fixed the bug. Would a set
be a better data structure for _modules
?
to_exclude = {'_modules', '_criteria', '_optimizers'} | ||
return {key: val for key, val in params.items() if key not in to_exclude} |
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.
On master
, these attributes are not included in get_params
.
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.
What do you mean? On current master, after merging #751, get_params
includes those.
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.
Ah I was mistaken. (I was on a older version of master locally)
Can we add a test here:
skorch/skorch/tests/test_net.py
Line 1409 in 812f54d
def test_get_params_works(self, net_cls, module_cls): |
Something like:
assert `_modules` not in params
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 idea. I added a separate test below because that one was already big enough.
Test that get_params does not return any of the unwanted keys.
This is a bug introduced by a last minute change in #751
After
clone
ing a net,_module
,_criteria
, and_optimizers
are alreadypopulated. Then, when loading params, there is yet another registration,
i.e. a double registration. As a consequence, there would be two
'modules'
etc. This is a fix for that.The solution is a two-pronged: Don't return
_modules
etc. when cloning,and make a safety check during registration. Either of those should be
sufficient, but it's probably better to be extra safe.