简体   繁体   中英

print learning rate evary epoch in sgd

I tried to print the learning rate in the mini-batch gradient descent. But the Ir remain unchanged(always 0.10000000149) for many epochs. But it was suppossed to change evrery mini-batch. The code is as follows:

# set the decay as 1e-1 to see the Ir change between epochs.
sgd = SGD(lr=0.1, decay=1e-1, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
class LossHistory(Callback):
    def on_epoch_begin(self, batch, logs={}):
        lr=self.model.optimizer.lr.get_value()
        print('Ir:', lr)
history=LossHistory()
model.fit(X_train, Y_train,
          batch_size= batch_size,
          nb_epoch= nb_epoch,
          callbacks= [history])

您要打印的是初始学习率,而不是实际计算得出的实际学习率:

lr = self.lr * (1. / (1. + self.decay * self.iterations))
from keras import backend as K
from keras.callbacks import Callback


class SGDLearningRateTracker(Callback):
    def on_epoch_end(self, epoch, logs={}):
        optimizer = self.model.optimizer
        lr = K.eval(optimizer.lr * (1. / (1. + optimizer.decay * optimizer.iterations)))
        print('\nLR: {:.6f}\n'.format(lr))

Then in your model add the callback:

model.fit(X_train, Y_train_cat, nb_epoch=params['n_epochs'], batch_size=params['batch_size'], validation_split=0.1,callbacks=[SGDLearningRateTracker()])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM