Training a neural network or large deep learning model is a difficult optimization task.

The classical algorithm to train neural networks is called stochastic gradient descent. It has been well established that you can achieve increased performance and faster training on some problems by using a learning rate that changes during training.

In this post you will discover how you can use different learning rate schedules for your neural network models in Python using the Keras deep learning library.

After reading this post you will know:

- How to configure and evaluate a time-based learning rate schedule.
- How to configure and evaluate a drop-based learning rate schedule.

Let’s get started.

**Update Mar/2017**: Updated example for Keras 2.0.2, TensorFlow 1.0.1 and Theano 0.9.0.

## Learning Rate Schedule For Training Models

Adapting the learning rate for your stochastic gradient descent optimization procedure can increase performance and reduce training time.

Sometimes this is called learning rate annealing or adaptive learning rates. Here we will call this approach a learning rate schedule, were the default schedule is to use a constant learning rate to update network weights for each training epoch.

The simplest and perhaps most used adaptation of learning rate during training are techniques that reduce the learning rate over time. These have the benefit of making large changes at the beginning of the training procedure when larger learning rate values are used, and decreasing the learning rate such that a smaller rate and therefore smaller training updates are made to weights later in the training procedure.

This has the effect of quickly learning good weights early and fine tuning them later.

Two popular and easy to use learning rate schedules are as follows:

- Decrease the learning rate gradually based on the epoch.
- Decrease the learning rate using punctuated large drops at specific epochs.

Next, we will look at how you can use each of these learning rate schedules in turn with Keras.

### Need help with Deep Learning in Python?

Take my free 2-week email course and discover MLPs, CNNs and LSTMs (with code).

Click to sign-up now and also get a free PDF Ebook version of the course.

## Time-Based Learning Rate Schedule

Keras has a time-based learning rate schedule built in.

The stochastic gradient descent optimization algorithm implementation in the SGD class has an argument called decay. This argument is used in the time-based learning rate decay schedule equation as follows:

1 |
LearningRate = LearningRate * 1/(1 + decay * epoch) |

When the decay argument is zero (the default), this has no effect on the learning rate.

1 2 |
LearningRate = 0.1 * 1/(1 + 0.0 * 1) LearningRate = 0.1 |

When the decay argument is specified, it will decrease the learning rate from the previous epoch by the given fixed amount.

For example, if we use the initial learning rate value of 0.1 and the decay of 0.001, the first 5 epochs will adapt the learning rate as follows:

1 2 3 4 5 6 |
Epoch Learning Rate 1 0.1 2 0.0999000999 3 0.0997006985 4 0.09940249103 5 0.09900646517 |

Extending this out to 100 epochs will produce the following graph of learning rate (y axis) versus epoch (x axis):

You can create a nice default schedule by setting the decay value as follows:

1 2 3 |
Decay = LearningRate / Epochs Decay = 0.1 / 100 Decay = 0.001 |

The example below demonstrates using the time-based learning rate adaptation schedule in Keras.

It is demonstrated on the Ionosphere binary classification problem. This is a small dataset that you can download from the UCI Machine Learning repository. Place the data file in your working directory with the filename ionosphere.csv.

The ionosphere dataset is good for practicing with neural networks because all of the input values are small numerical values of the same scale.

A small neural network model is constructed with a single hidden layer with 34 neurons and using the rectifier activation function. The output layer has a single neuron and uses the sigmoid activation function in order to output probability-like values.

The learning rate for stochastic gradient descent has been set to a higher value of 0.1. The model is trained for 50 epochs and the decay argument has been set to 0.002, calculated as 0.1/50. Additionally, it can be a good idea to use momentum when using an adaptive learning rate. In this case we use a momentum value of 0.8.

The complete example is listed below.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# Time Based Learning Rate Decay from pandas import read_csv import numpy from keras.models import Sequential from keras.layers import Dense from keras.optimizers import SGD from sklearn.preprocessing import LabelEncoder # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) # load dataset dataframe = read_csv("ionosphere.csv", header=None) dataset = dataframe.values # split into input (X) and output (Y) variables X = dataset[:,0:34].astype(float) Y = dataset[:,34] # encode class values as integers encoder = LabelEncoder() encoder.fit(Y) Y = encoder.transform(Y) # create model model = Sequential() model.add(Dense(34, input_dim=34, kernel_initializer='normal', activation='relu')) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) # Compile model epochs = 50 learning_rate = 0.1 decay_rate = learning_rate / epochs momentum = 0.8 sgd = SGD(lr=learning_rate, momentum=momentum, decay=decay_rate, nesterov=False) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) # Fit the model model.fit(X, Y, validation_split=0.33, epochs=epochs, batch_size=28, verbose=2) |

The model is trained on 67% of the dataset and evaluated using a 33% validation dataset.

Running the example shows a classification accuracy of 99.14%. This is higher than the baseline of 95.69% without the learning rate decay or momentum.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
... Epoch 45/50 0s - loss: 0.0622 - acc: 0.9830 - val_loss: 0.0929 - val_acc: 0.9914 Epoch 46/50 0s - loss: 0.0695 - acc: 0.9830 - val_loss: 0.0693 - val_acc: 0.9828 Epoch 47/50 0s - loss: 0.0669 - acc: 0.9872 - val_loss: 0.0616 - val_acc: 0.9828 Epoch 48/50 0s - loss: 0.0632 - acc: 0.9830 - val_loss: 0.0824 - val_acc: 0.9914 Epoch 49/50 0s - loss: 0.0590 - acc: 0.9830 - val_loss: 0.0772 - val_acc: 0.9828 Epoch 50/50 0s - loss: 0.0592 - acc: 0.9872 - val_loss: 0.0639 - val_acc: 0.9828 |

## Drop-Based Learning Rate Schedule

Another popular learning rate schedule used with deep learning models is to systematically drop the learning rate at specific times during training.

Often this method is implemented by dropping the learning rate by half every fixed number of epochs. For example, we may have an initial learning rate of 0.1 and drop it by 0.5 every 10 epochs. The first 10 epochs of training would use a value of 0.1, in the next 10 epochs a learning rate of 0.05 would be used, and so on.

If we plot out the learning rates for this example out to 100 epochs you get the graph below showing learning rate (y axis) versus epoch (x axis).

We can implement this in Keras using a the LearningRateScheduler callback when fitting the model.

The LearningRateScheduler callback allows us to define a function to call that takes the epoch number as an argument and returns the learning rate to use in stochastic gradient descent. When used, the learning rate specified by stochastic gradient descent is ignored.

In the code below, we use the same example before of a single hidden layer network on the Ionosphere dataset. A new step_decay() function is defined that implements the equation:

1 |
LearningRate = InitialLearningRate * DropRate^floor(Epoch / EpochDrop) |

Where InitialLearningRate is the initial learning rate such as 0.1, the DropRate is the amount that the learning rate is modified each time it is changed such as 0.5, Epoch is the current epoch number and EpochDrop is how often to change the learning rate such as 10.

Notice that we set the learning rate in the SGD class to 0 to clearly indicate that it is not used. Nevertheless, you can set a momentum term in SGD if you want to use momentum with this learning rate schedule.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# Drop-Based Learning Rate Decay import pandas from pandas import read_csv import numpy import math from keras.models import Sequential from keras.layers import Dense from keras.optimizers import SGD from sklearn.preprocessing import LabelEncoder from keras.callbacks import LearningRateScheduler # learning rate schedule def step_decay(epoch): initial_lrate = 0.1 drop = 0.5 epochs_drop = 10.0 lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop)) return lrate # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) # load dataset dataframe = read_csv("ionosphere.csv", header=None) dataset = dataframe.values # split into input (X) and output (Y) variables X = dataset[:,0:34].astype(float) Y = dataset[:,34] # encode class values as integers encoder = LabelEncoder() encoder.fit(Y) Y = encoder.transform(Y) # create model model = Sequential() model.add(Dense(34, input_dim=34, kernel_initializer='normal', activation='relu')) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) # Compile model sgd = SGD(lr=0.0, momentum=0.9, decay=0.0, nesterov=False) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) # learning schedule callback lrate = LearningRateScheduler(step_decay) callbacks_list = [lrate] # Fit the model model.fit(X, Y, validation_split=0.33, epochs=50, batch_size=28, callbacks=callbacks_list, verbose=2) |

Running the example results in a classification accuracy of 99.14% on the validation dataset, again an improvement over the baseline for the model on the problem.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
... Epoch 45/50 0s - loss: 0.0546 - acc: 0.9830 - val_loss: 0.0634 - val_acc: 0.9914 Epoch 46/50 0s - loss: 0.0544 - acc: 0.9872 - val_loss: 0.0638 - val_acc: 0.9914 Epoch 47/50 0s - loss: 0.0553 - acc: 0.9872 - val_loss: 0.0696 - val_acc: 0.9914 Epoch 48/50 0s - loss: 0.0537 - acc: 0.9872 - val_loss: 0.0675 - val_acc: 0.9914 Epoch 49/50 0s - loss: 0.0537 - acc: 0.9872 - val_loss: 0.0636 - val_acc: 0.9914 Epoch 50/50 0s - loss: 0.0534 - acc: 0.9872 - val_loss: 0.0679 - val_acc: 0.9914 |

## Tips for Using Learning Rate Schedules

This section lists some tips and tricks to consider when using learning rate schedules with neural networks.

**Increase the initial learning rate**. Because the learning rate will very likely decrease, start with a larger value to decrease from. A larger learning rate will result in a lot larger changes to the weights, at least in the beginning, allowing you to benefit from the fine tuning later.**Use a large momentum**. Using a larger momentum value will help the optimization algorithm to continue to make updates in the right direction when your learning rate shrinks to small values.**Experiment with different schedules**. It will not be clear which learning rate schedule to use so try a few with different configuration options and see what works best on your problem. Also try schedules that change exponentially and even schedules that respond to the accuracy of your model on the training or test datasets.

## Summary

In this post you discovered learning rate schedules for training neural network models.

After reading this post you learned:

- How to configure and use a time-based learning rate schedule in Keras.
- How to develop your own drop-based learning rate schedule in Keras.

Do you have any questions about learning rate schedules for neural networks or about this post? Ask your question in the comments and I will do my best to answer.

Great

Thanks Hunaina.

nice post

Hello Jason,

Interesting post. Can’t we use this learning rate and momentum for optimizers other than SGD? Since ‘adam’ is performing good with most of the datasets, i wanna try learning rate and momentum tuning for ‘adam’ optimizer. Also, I did a quick research on this and found that ‘adam’ already have decaying learning rate. Is it true? and what about other optimizers?

I want to implement a learning rate that is exponentially decayed with respect to the cost? For example, the learning rate is updated as follows:

eta = eta0*exp(CostFunction)

Such that a larger learning rate is employed when the cost is large, vice versa.

Do you know to achieve that in Keras? Thank you!

Hi Yuzhen,

I would advise you to develop your decay function in excel or something to test and plot it. Use the above linear decay function inputs/outputs as a starting point.

You can then plug your function into one of the examples above.

Let me know how you go.

Hi Jason!

Thanks for your great poster! It does great help!

One more question: I use the “Drop-Based Learning Rate Schedule”, how can I print out the current learning rate in terminal?

Best,

Got the solution! Just print out

Good question, you might be able to call print() from the custom function. Let me know if that works.

Does the weight decay change once per mini-batch or epoch? According to https://groups.google.com/forum/#!topic/keras-users/7KM2AvCurW0, it updates per mini-batch. In my problem, one epoch contains 800 mini-batches.

If you implement the decay yourself, you can control the schedule, as in the tutorial above (e.g. a function of the epoch not the batch).

Does it make sense to use these techniques of weight decay with adaptive optimizers like adam or rmsprop rather than sgd? or will it be redundant?

I’d like to think it is redundant, but test and see on your problem to know for sure. Data wins.

Thanks a lot for this guide! Is there a way to see the code you wrote before the update? I have a constraint and need to use Keras 1.2 currently, and when applying the SGD I get:

File “/usr/lib64/python2.7/site-packages/keras/models.py”, line 924, in fit_generator

pickle_safe=pickle_safe)

File “/usr/lib64/python2.7/site-packages/keras/engine/training.py”, line 1401, in fit_generator

self._make_train_function()

File “/usr/lib64/python2.7/site-packages/keras/engine/training.py”, line 713, in _make_train_function

self.total_loss)

TypeError: unbound method get_updates() must be called with SGD instance as first argument (got list instance instead)

I assume this is because of some API change that I fail to see

Sorry, I no longer have the older code.

Hi Jason, is there an option to specify momentum when using Adam optimizer in Keras? I can only see the momentum option in SGD.

Not as far as I remember. Double check the API to be sure.

Hi, I have a question here

I wish to print out the learning rate information during the training but it won’t work with this code below:

def step_decay(epoch):

initial_lrate = 0.001

drop = 0.5

epochs_drop = 100.0

lrate = initial_lrate * math.pow(drop, math.floor((1+epoch)/epochs_drop))

print (‘epoch: ‘ + str(epoch) + ‘, learning rate: ‘ + str(lrate)) # added

return lrate

NUM_EPOCHS = 700

BATCH_SIZE = 128

for each_epoch in range(0, NUM_EPOCHS):

lrate = callbacks.LearningRateScheduler(step_decay)

callbacks_list = [lrate]

history = model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=1, callbacks=callbacks_list, verbose=2, validation_data=[x_test, y_test])

prediction_test = model.predict(x_test).flatten()

Do I have to set ‘epochs’ to NUM_EPOCHS and remove for-loop to display lrate?

Does the example in the post work for you?

Perhaps start with something that works and modify it one piece at a time to achieve your desired outcome?

The example works but I want to display the learning rate during training.

I would expect the print in the callback function to work.

Ensure you’re running from the command line and not in a notebook or ide.

Oh, never mind, it is displaying changed lrate per epoch without a for-loop.

There was a lot of displayed information during the training (with verbose=1) and I missed the lrate reports.

Thanks,

Glad to hear it.

Hi Jason,

I know the learning rate can be adjusted in Keras, but all the options seem to only include some decay or decreasing learning rate. I am wondering if it’s possible to create a custom schedule that works like ReduceLROnPlateau, where it is looking to see if the loss stops decreasing for some number of epochs, and if so then it decreases the LR. But after some number of “decreases” it then increases the learning rate the next time loss stagnates, and then continues decreasing again on loss stagnation after that.

My thought here is that you may descend into a local minimum that you may not be able to escape from unless you increase the learning rate, before continuing to descend to the global minimum.

Does it make sense to create a function for learning rate? Would Keras accept that?

Can anyone give me an idea on how I would write that function?

Thanks!

Sure, I don’t think it would be a problem. You may need an object rather than just a function to store state. I have not done this, so some experimentation may be required.

Hi Jason,

This post helped me a lot. How can i tweak the learning rate value after every epoch?

You can implement this directly as your own method, follow the above tutorial.

I want to know how to use keras to change the learning rate in every layers?

Good question, I’m not sure off hand. Perhaps ask on the Keras user group:

https://machinelearningmastery.com/get-help-with-keras/

Hi, I want to plot the learning rate curve of the adam optimizer, should I code callbacks=[lr] in the fit function

Each model parameter (weight) has a learning rate, plotting each would be challenging.

I used the tutorial from this link: https://enlight.nyc/neural-network/ and applied it to a 6 input data set. My values always converge to ~0, .5, or 1 depending on the number of hidden layers. This occurs after the first call to train each time. I suspect this is because it does not include learning rate or bias. Where would you apply the learning rate in this code?

If you have a question about someone else’s tutorial, perhaps ask them directly?

Hello Dr. Jason,

Thanks for your informative posts on working with Keras.

I am using Drop based Learning Schedule as per your tutorial which works fine and can print the changing learning rate with every epoch in the step_decay() function.

Due to shortage of memory, I am saving this model and reloading it in another script.

The model reloads and optimizes from the previous epoch only so learning keeps happening. But I am not able to see the learning rate from the callback. Perhaps the learning rate scheduler callback state is not saved. Do you have any idea on how I can use the previous callback state also in the reloaded model?

You might have to re-seed it manually.

How should we record the learning rate during training in keras?

tensorboard can plot learning rate in every epochs

Sorry, I don’t have an example of monitoring learning rate for tensorboard.

If we want to know the learning rate during training in every epoch,what we can do use the keras