Prevent unexpected unpacking error when calling lr_finder.plot()
with suggest_lr=True
#98
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
@davidtvs, this PR should fix #88. And if it is resolved, maybe we can go on #97 to make a new release to PyPI.
@chAwater, I've rewritten part of the test case you made, please feel free to advise me if there is anything can be improved.
Problem
When calling
lr_finder.plot(..., suggest_lr=True)
, we usually expect the returned value is actually a tuple containingax
andsuggested_lr
, and the API is also described as it.But it would return only
ax
if there is no sufficient data points to calculate gradient of lr-loss curve:pytorch-lr-finder/torch_lr_finder/lr_finder.py
Lines 536 to 542 in fd9e949
pytorch-lr-finder/torch_lr_finder/lr_finder.py
Lines 568 to 571 in fd9e949
Therefore, if users prefer unpacking the returned value directly as below, they would sometimes ran into the error
TypeError: cannot unpack non-iterable AxesSubplot object
.Solution
Always return 2 values if
suggest_lr=True
. This makes sure it work with the 2 kinds of syntax as follows:The responsibility of "check whether
suggested_lr
is available/none" is left back to users now. But it should be fine since the warning message would show up and it's easy to check. Also, the warning message is now more verbose to help user figure out the problem.Note
Though I think this issue could be resolved better by separating the feature of "suggest learning rate" into a new function, it should be safer to keep the API unchanged before we decide to support more different methods to find a suggested learning rate in the future.