-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best practices question: decreasing learning rates between epochs #898
Comments
@sergeyf check the solution in this #888 |
Thank you very much! I will leave this open. |
This code may be work, to be tested... class LrReducer(Callback):
def __init__(self, patience=0, reduce_rate=0.5, reduce_nb=10, verbose=1):
super(Callback, self).__init__()
self.patience = patience
self.wait = 0
self.best_score = -1.
self.reduce_rate = reduce_rate
self.current_reduce_nb = 0
self.reduce_nb = reduce_nb
self.verbose = verbose
def on_epoch_end(self, epoch, logs={}):
current_score = logs.get('val_acc')
if current_score > self.best_score:
self.best_score = current_score
self.wait = 0
if self.verbose > 0:
print('---current best val accuracy: %.3f' % current_score)
else:
if self.wait >= self.patience:
self.current_reduce_nb += 1
if self.current_reduce_nb <= 10:
lr = self.model.optimizer.lr.get_value()
self.model.optimizer.lr.set_value(lr*self.reduce_rate)
else:
if self.verbose > 0:
print("Epoch %d: early stopping" % (epoch))
self.model.stop_training = True
self.wait += 1 |
Thanks @jiumem! This might be a good pull request to Keras? |
@sergeyf I just saw this thread, and I'd thought I'd throw in my own function I made to address this. I always use nb_epoch =1 because I'm interested in generating text:
|
Awesome, thanks! On Fri, Nov 6, 2015 at 7:24 AM, LeavesBreathe notifications@github.com
|
I have a problem using the solution like
with tensorflow backend.
Any suggestions please? |
@Rusianka I just found that one can do this to get and set value:
|
@entron when does the .set_value() happen? after every epoch? |
@shuaiw you can put the line |
@entron thanks for your response. my model doesn't have an epoch loop; instead it's like this:
Anyway to fit in? |
Maybe you can set N_EPOCH=1 and loop outside. |
So I had problems with the
Here are my questions:
I am unfamiliar with Theano, and I do not have time at this point to learn about it so any information you can provide on both these questions is much appreciated. Thanks. |
@sergeyf @shuaiw A more simpler solution for decay after specified epochs. class decay_lr(Callback):
'''
n_epoch = no. of epochs after decay should happen.
decay = decay value
'''
def __init__(self, n_epoch, decay):
super(decay_lr, self).__init__()
self.n_epoch=n_epoch
self.decay=decay
def on_epoch_begin(self, epoch, logs={}):
old_lr = self.model.optimizer.lr.get_value()
if epoch > 1 and epoch%self.n_epoch == 0 :
new_lr= self.decay*old_lr
k.set_value(self.model.optimizer.lr, new_lr)
else:
k.set_value(self.model.optimizer.lr, old_lr)
decaySchedule=decay_lr(10, 0.95) You can use this directly without the epoch loop. |
In my case, the learning rate was supposed to be decayed by specific iterations. In a theano backend keras, I can do using the following code:
But when I changed to a tensorflow backend keras, I can not use above code, beacause
But it did not work. I found that the Carol |
Another option could be to use the LearningRateScheduler that You can use the schedule function that best fits your needs |
@sergeyf please update the answer inside your initial question, because model.optimizer.lr.set_value() is no longer valid. |
@FedericoMuciaccia Thanks, I did as you suggested. |
With TF backend, I did this (for inception-V3) from keras.callbacks import LearningRateScheduler
def scheduler(epoch):
if epoch%2==0 and epoch!=0:
lr = K.get_value(model.optimizer.lr)
K.set_value(model.optimizer.lr, lr*.9)
print("lr changed to {}".format(lr*.9))
return K.get_value(model.optimizer.lr)
lr_decay = LearningRateScheduler(scheduler)
model.fit_generator(train_gen, (nb_train_samples//batch_size)*batch_size,
nb_epoch=100, verbose=1,
validation_data=valid_gen, nb_val_samples=val_size,
callbacks=[lr_decay]) EDITI'm happy it helped. from keras.callbacks import LearningRateScheduler
def lr_decay_callback(lr_init, lr_decay):
def step_decay(epoch):
return lr_init * (lr_decay ** (epoch + 1))
return LearningRateScheduler(step_decay)
lr_decay = lr_decay_callback(lr_init, lr_decay)
# callback=[lr_decay, ] |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed. |
@FedericoMuciaccia @sergeyf
Keras doesn't throw an exception, but the lr doesn't change anyways. Fortunately, the backend method still works:
Edit:By the way, is it also possible to change the learning rate during an epoch (e.g. after 1000 batches) while looping over
Would be very useful when the data sample is large and/or the network is deep such that one epoch takes about 24h. |
The "decay" option in the optimizer seems to be designed for learning rate decay. I did not see any suggestion on using this in the discussion. Could someone please comment on the use (or not use) the "decay" option? |
It does learning rate decay: This functionality was introduced roughly one year ago in within this commit: |
when using rate decay in SGD do the optimizer.iterations reset at each epoch? |
@marc-moreaux |
FYI updated issue and much simpler solution at #5724 (comment) |
Howdy,
In published papers I often see that the learning rates are decreased after some hundreds of epochs when learning stalls. What is the best way to do this in Keras? Thus far, I have been recompiling, but (not knowing if there is a better way), that seems foolish.
An example:
First, I build some model and train it.
UPDATE -- the following works without having to recompile
Thank you to @EderSantana for the quick reply.
The text was updated successfully, but these errors were encountered: