gpt4 book ai didi

python - 尽管 SGD 衰减,Keras 学习率没有改变

转载 作者:太空狗 更新时间:2023-10-29 17:30:01 25 4
gpt4 key购买 nike

出于某种原因,即使我设置了衰减因子,我的学习率似乎也没有改变。我添加了一个回调来查看学习率,它似乎在每个纪元之后都是一样的。为什么没有变化

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

最佳答案

这个变化很好,问题是你试图访问存储初始学习率的字段,而不是当前的。当前是在每次迭代中通过方程从头开始计算

lr = self.lr * (1. / (1. + self.decay * self.iterations))

并且它永远不会被存储,因此您无法以这种方式监控它,您只需使用此等式自行计算即可。

参见 https://github.com/fchollet/keras/blob/master/keras/optimizers.py 的第 126 行

关于python - 尽管 SGD 衰减,Keras 学习率没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37091751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com