TrainingPhase and General scheduler¶
Creates a scheduler that lets you train a model with following different TrainingPhase
.
You can then schedule any hyper-parameter you want by using the following method.
The phase will make the hyper-parameter vary from the first value in vals
to the second, following anneal
. If an annealing function is specified but vals
is a float, it will decay to 0. If no annealing function is specified, the default is a linear annealing for a tuple, a constant parameter if it's a float.
The basic hyper-parameters are named:
- 'lr' for learning rate
- 'mom' for momentum (or beta1 in Adam)
- 'beta' for the beta2 in Adam or the alpha in RMSprop
- 'wd' for weight decay
You can also add any hyper-parameter that is in your optimizer (even if it's custom or a GeneralOptimizer
), like 'eps' if you're using Adam.
Let's make an example by using this to code SGD with warm restarts.
def fit_sgd_warm(learn, n_cycles, lr, mom, cycle_len, cycle_mult):
n = len(learn.data.train_dl)
phases = [(TrainingPhase(n * (cycle_len * cycle_mult**i))
.schedule_hp('lr', lr, anneal=annealing_cos)
.schedule_hp('mom', mom)) for i in range(n_cycles)]
sched = GeneralScheduler(learn, phases)
learn.callbacks.append(sched)
if cycle_mult != 1:
total_epochs = int(cycle_len * (1 - (cycle_mult)**n_cycles)/(1-cycle_mult))
else: total_epochs = n_cycles * cycle_len
learn.fit(total_epochs)
path = untar_data(URLs.MNIST_SAMPLE)
data = ImageDataBunch.from_folder(path)
learn = Learner(data, simple_cnn((3,16,16,2)), metrics=accuracy)
fit_sgd_warm(learn, 3, 1e-3, 0.9, 1, 2)
learn.recorder.plot_lr()
Callback methods¶
You don't call these yourself - they're called by fastai's Callback
system automatically to enable the class's functionality.
Takes a step in the current phase and prepare the hyperparameters for the next batch.
Initiates the hyperparameters to the start values of the first phase.