Fastai 2 equivalent for recorder.plot_lr_find

Hello,

I am currently migrating a script from fastai 1 to fastai 2 and I do not find the equivalent for the following piece of code:

fig = learner.recorder.plot_lr_find(return_fig=True)
fig.savefig('lrfind.png')

When I run it, I get the following error:

AttributeError: 'NoneType' object has no attribute 'savefig'

Could you please give me some hints? Thanks!

Hello,

This is a bug on fastai’s side. Specifically, the plot_lr_find function ignores the return_fig argument and never returns a value.

Currently, this function is implemented as follows,

def plot_lr_find(self:Recorder, skip_end=5, return_fig=True, suggestions=None, nms=None, **kwargs):
    "Plot the result of an LR Finder test (won't work if you didn't do `learn.lr_find()` before)"
    lrs    = self.lrs    if skip_end==0 else self.lrs   [:-skip_end]
    losses = self.losses if skip_end==0 else self.losses[:-skip_end]
    fig, ax = plt.subplots(1,1)
    ax.plot(lrs, losses)
    ax.set_ylabel("Loss")
    ax.set_xlabel("Learning Rate")
    ax.set_xscale('log')
    if suggestions:
        colors = plt.rcParams['axes.prop_cycle'].by_key()['color'][1:]
        for (val, idx), nm, color in zip(suggestions, nms, colors):
            ax.plot(val, idx, 'o', label=nm, c=color)
        ax.legend(loc='best')

However, it should be,

def plot_lr_find(self:Recorder, skip_end=5, return_fig=True, suggestions=None, nms=None, **kwargs):
    "Plot the result of an LR Finder test (won't work if you didn't do `learn.lr_find()` before)"
    lrs    = self.lrs    if skip_end==0 else self.lrs   [:-skip_end]
    losses = self.losses if skip_end==0 else self.losses[:-skip_end]
    fig, ax = plt.subplots(1,1)
    ax.plot(lrs, losses)
    ax.set_ylabel("Loss")
    ax.set_xlabel("Learning Rate")
    ax.set_xscale('log')
    if suggestions:
        colors = plt.rcParams['axes.prop_cycle'].by_key()['color'][1:]
        for (val, idx), nm, color in zip(suggestions, nms, colors):
            ax.plot(val, idx, 'o', label=nm, c=color)
        ax.legend(loc='best')
    return fig if return_fig else None

The lines below would now yield the desired results. Alternatively, you could monkey-patch plot_lr_find or modify fastai’s source code to save you from future hassle.

fig = plot_lr_find(learner.recorder, return_fig=True)
fig.savefig('lrfind.png')

Please let me know if this is helpful.

Dear Borna,

Thanks for your reply. Your solution works perfectly!

1 Like

Thanks @BobMcDear . I’ll fix that now. For future reference, if you find a bug in one of our repos, it’s really helpful to create a GitHub issue to let us know. Better still, when you have a solution, is to submit a PR that fixes it!

2 Likes