Allow passing a pre-initialized OptimWrapper to Learner#3843
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
When I run the tests locally, CI error does not occur. I also did not change anything related to default callbacks. Investigating. |
|
@jph00 The CI failure appears to be random and due to a change made in the development branch of nbdev, which the fastai CI currently uses. My updated passed the CI, but would have been safe to merge even if it did not, as I tested it with nbdev 2.3.9 and it passed every time. |
jph00
left a comment
There was a problem hiding this comment.
This looks great @warner-benjamin . My only request is to avoid restricting types more than necessary. You've added some restrictions which are too tight -- and it's also made me realise some existing ones are also too tight!
|
Will change any of the type hints to what you want. I tried to follow the convention set by #3639 and attempted to add some clarity. I added a few comments and questions on the code review. |
|
Many thanks @warner-benjamin -- let me know if you have any outstanding questions. |
|
Let me know which would be easier/preferred - I can merge this PR as is, and make the type changes on my end, or I you can change them in this PR. |
|
I have most of them done already, just need some clarification on the |
|
Many thanks! |
|
I'm replacing |
The
OptimWrapperdocumentation suggests that using a pre-defined optimizer withLearner's opt_func argument should work:however, following this example and passing
opt_functoLearner.opt_funcresults in an error, asLearner.create_optonly accepts callable methods and not initialized objects.This PR modifies
Learnerto accept a pre-initializedOptimWrapperto the opt_func argument by modifyingLearner.create_optto set a pre-initializedOptimWrappertoLearner.opt. To mimic the current behavior of always initializing an optimizer from scratch,create_optwill callclear_stateon a pre-initializedOptimWrapper.This allows a straightforward method of using custom parameter groups in a PyTorch style optimizer:
To prevent a pre-initialized optimizer's state from being cleared on the first fit, users can use the current behavior of passing the optimizer directly to
Learner.opt. However, in this case manually callingLearner.fit(..., reset_opt=True)will cause an error. Instead of using reset_opt, users will need to redefine the optimizer.This PR also updates some of the existing
Learnerdocments to better match the docment style and more accurately describe the argument behavior.