Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing optimizer in keras during training

Tags:

keras

I am developing a model using nadam optimizer. I was wondering if there is a way to switch to sgd during training if validation loss does not reduce for two epochs.

like image 941
Haroon S. Avatar asked Dec 04 '17 03:12

Haroon S.


People also ask

How do I change my learning rate in Adam Optimizer keras?

The constant learning rate is the default schedule in all Keras Optimizers. For example, in the SGD optimizer, the learning rate defaults to 0.01 . To use a custom learning rate, simply instantiate an SGD optimizer and pass the argument learning_rate=0.01 .

What does an optimiser do in keras?

Optimizers are Classes or methods used to change the attributes of your machine/deep learning model such as weights and learning rate in order to reduce the losses. Optimizers help to get results faster.

How can keras reduce learning rate?

A typical way is to to drop the learning rate by half every 10 epochs. To implement this in Keras, we can define a step decay function and use LearningRateScheduler callback to take the step decay function as argument and return the updated learning rates for use in SGD optimizer.

How do I create a custom Optimizer in Tensorflow?

Creating a custom optimizer If you intend to create your own optimization algorithm, simply inherit from this class and override the following methods: _resource_apply_dense (update variable given gradient tensor is a dense tf. Tensor ) _resource_apply_sparse (update variable given gradient tensor is a sparse tf.

How do I modify the learning rate of a keras optimizer?

You can use a learning rate schedule to modulate how the learning rate of your optimizer changes over time: Check out the learning rate schedule API documentation for a list of available schedules. These methods and attributes are common to all Keras optimizers. Apply gradients to variables. This is the second part of minimize ().

What are the Keras objects used for?

This object is used later for training and testing the neural network. Before the model can be trained, Keras requires us to specify some details about the training process like the optimizer, and a loss function. For the example, we also tell Keras to track the network’s accuracy during the training process.

How do I change the learning rate of my optimizer?

You can use a learning rate schedule to modulate how the learning rate of your optimizer changes over time: Check out the learning rate schedule API documentation for a list of available schedules. These methods and attributes are common to all Keras optimizers.

How to change the loss attribute of a keras model?

If you really need to have the loss attribute of your model changed, you can set the compiled_loss attribute using a keras.engine.compile_utils.LossesContainer ( here is the reference) and set model.train_function to model.make_train_function () (so that the new loss gets taken into account).


2 Answers

Would something like this work ?

model.compile( optimizer='Adam', ...) 
model.fit( X, y, epochs=100, callback=[EarlyStoppingCallback] ) 
# now switch to SGD and finish training
model.compile( optimizer='SGD', ...) 
model.fit( X, y, epochs=10 ) 

Or would the second call to compile over-write all the variables (ie. do something like tf.initialize_all_variables()

(It's actually a followup question - but I'm writing this as an answer - because stackoverflow does not allow code in comments)

like image 197
firdaus Avatar answered Oct 15 '22 19:10

firdaus


You can create an EarlyStopping callback that will stop the training, and in this callback, you create a function to change your optimizer and fit again.

The following callback will monitor the validation loss (val_loss) and stop training after two epochs (patience) without an improvement greater than min_delta.

min_delta = 0.000000000001

stopper = EarlyStopping(monitor='val_loss',min_delta=min_delta,patience=2) 

But for adding an extra action after the training is finished, we can extend this callback and change the on_train_end method:

class OptimizerChanger(EarlyStopping):

    def __init__(self, on_train_end, **kwargs):

        self.do_on_train_end = on_train_end
        super(OptimizerChanger,self).__init__(**kwargs)

    def on_train_end(self, logs=None):
        super(OptimizerChanger,self).on_train_end(self,logs)
        self.do_on_train_end()

For the custom function to call when the model ends training:

def do_after_training():

    #warining, this creates a new optimizer and,
    #at the beginning, it might give you a worse training performance than before
    model.compile(optimizer = 'SGD', loss=...., metrics = ...)
    model.fit(.....)

Now let's use the callbacks:

changer = OptimizerChanger(on_train_end= do_after_training, 
                           monitor='val_loss',
                           min_delta=min_delta,
                           patience=2)

model.fit(..., ..., callbacks = [changer])
like image 28
Daniel Möller Avatar answered Oct 15 '22 20:10

Daniel Möller