出于某种原因,虽然我设定了衰减因子,但我的学习率似乎并没有改变。我添加了一个回调来查看学习率,并且在每个时代之后它似乎是相同的。为什么不改变
class LearningRatePrinter(Callback):
def init(self):
super(LearningRatePrinter, self).init()
def on_epoch_begin(self, epoch, logs={}):
print('lr:', self.model.optimizer.lr.get_value())
lr_printer = LearningRatePrinter()
model = Sequential()
model.add(Flatten(input_shape = (28, 28)))
model.add(Dense(200, activation = 'tanh'))
model.add(Dropout(0.5))
model.add(Dense(20, activation = 'tanh'))
model.add(Dense(10, activation = 'softmax'))
print('Compiling Model')
sgd = SGD(lr = 0.01, decay = 0.1, momentum = 0.9, nesterov = True)
model.compile(loss = 'categorical_crossentropy', optimizer = sgd)
print('Fitting Data')
model.fit(x_train, y_train, batch_size = 128, nb_epoch = 400, validation_data = (x_test, y_test), callbacks = [lr_printer])
lr: 0.009999999776482582
Epoch 24/400
60000/60000 [==============================] - 0s - loss: 0.7580 - val_loss: 0.6539
lr: 0.009999999776482582
Epoch 25/400
60000/60000 [==============================] - 0s - loss: 0.7573 - val_loss: 0.6521
lr: 0.009999999776482582
Epoch 26/400
60000/60000 [==============================] - 0s - loss: 0.7556 - val_loss: 0.6503
lr: 0.009999999776482582
Epoch 27/400
60000/60000 [==============================] - 0s - loss: 0.7525 - val_loss: 0.6485
lr: 0.009999999776482582
Epoch 28/400
60000/60000 [==============================] - 0s - loss: 0.7502 - val_loss: 0.6469
lr: 0.009999999776482582
Epoch 29/400
60000/60000 [==============================] - 0s - loss: 0.7494 - val_loss: 0.6453
lr: 0.009999999776482582
Epoch 30/400
60000/60000 [==============================] - 0s - loss: 0.7483 - val_loss: 0.6438
lr: 0.009999999776482582
Epoch 31/400
答案 0 :(得分:15)
这种情况正在改变,问题是您尝试访问商店的字段初始学习率,而不是当前商店。在每次迭代期间通过等式
从头开始计算当前值lr = self.lr * (1. / (1. + self.decay * self.iterations))
和永远不会存储,因此您无法以这种方式进行监控,您只需使用此等式自行计算。
见https://github.com/fchollet/keras/blob/master/keras/optimizers.py
的第126行