-
Notifications
You must be signed in to change notification settings - Fork 395
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
[RFC] Feature facilitate customization of net #593
Conversation
A scorer that just passes through a score that is already calculated and logged during the batches (as, e.g., train_loss). Replaces use of BatchScoring for most use cases. This makes it a lot easier to add a custom loss during a batch. Just log it in the history at each batch and add PassthroughScoring with the name of the key of that loss. No need to add a fake scoring function that just returns the value.
This is achieved by: * automatically registering modules/optimizers in the prefixes and cuda_dependent_attributes when they are set * promoting get_params_for{,_optimizer} to public methods, since they need to be used for set_params to work
* fix wrong formatting * add missing information
In this notebook, a DCGAN is trained. It makes use of 2 separate optimizers and adds custom batch-level losses. This is now easier with the changes introduced in this PR.
Check out this pull request on You'll be able to see Jupyter notebook diff and discuss changes. Powered by ReviewNB. |
After discussion with @ottonemo, it might be better to split the introduction of |
I think using
I'm for making this step explicit and to just expose the methods publicly. They are side-effect free and therefore there is no harm done in doing so. |
Good point, I'll look into it. As you said, for now we'd probably be fine. |
The motivation for this PR is to make skorch easier to customize so that things like DCGAN can be implemented with less effort. See the attached notebook for an illustration of this.
To achieve this, I introduced 2 major changes:
PassthroughScoring
Currently, when adding new losses on a batch level, it's very fiddly to log them on an epoch level. You have to define a custom function that picks the right value from
history
and passes it through without modification, then add aBatchScoring
callback that uses this mock scoring function, defines the name of the score again, and passnoop
.Here is the code before and after:
I replaced
BatchScoring
withPassthroughScoring
in the existing code for train and valid loss.Facilitate adding custom torch modules and optimizers
Right now, if a user wants to add their own modules or optimizers to
NeuralNet
, there are a few non-obvious steps they need to take for everything to work well:prefixes_
attribute to makeset_params
work (but don't mutate or else woe!)cuda_dependent_attributes_
to avoid subtle bugs (but again, don't mutate!)self._get_params_for
orself._get_params_for_optimizer
.The changes introduces in this PR automatically take care of 1. and 2. by using some
__setattr__
magic. This is of course a dangerous thing to do, so please think about cases where this could go wrong.For 3., I have not found a better solution right now then to make the mentioned methods public. I was experimenting with automating this step completely with some magic things happening behind the door but this was a) a bit hacky and b) we cannot know which module parameters should be passed to an optimizer, i.e. we cannot fully automate this.
If anyone has a suggestion to make this easier for the user, please come ahead.
Conclusion
To illustrate how all these changes help, please have a look at the attached notebook. This replaces the notebook shown in #587 and sticks much closer to the original pytorch implementation of DCGAN.
If we find this solution okay, I would add more documentation, docstrings, and tests.
ping @zachbellay @cgarciae @YannDubs