【问题标题】:Getting error when using learningratescheduler with keras and SGD optimizer将 learningratescheduler 与 keras 和 SGD 优化器一起使用时出错
【发布时间】:2021-11-06 08:10:34
【问题描述】:

我想降低每个 epoch 的学习率。我正在使用 Keras。运行代码时出现此错误。


{Traceback (most recent call last):

  File "<ipython-input-1-2983b4be581f>", line 1, in <module>
    runfile('C:/Users/Gehan Mohamed/cnn_learningratescheduler.py', wdir='C:/Users/Gehan Mohamed')

  File "C:\Users\Gehan Mohamed\Anaconda3\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)

ValueError: Attempt to convert a value (<keras.callbacks.callbacks.LearningRateScheduler object at 0x000001E7C7B8E780>) with an unsupported type (<class 'keras.callbacks.callbacks.LearningRateScheduler'>) to a Tensor.
Attempt to convert a value (<keras.callbacks.callbacks.LearningRateScheduler object at 0x000001E7C7B8E780>) with an unsupported type (<class 'keras.callbacks.callbacks.LearningRateScheduler'>) to a Tensor}. 

我该如何解决这个错误?

def step_decay(epochs):
    if epochs <50:
        lrate=0.1
        return lrate
    if epochs >50:
        lrate=0.01
        return lrate            
            
lrate = LearningRateScheduler(step_decay)
sgd = SGD(lr=lrate, decay=0, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
callbacks_list = [lrate,callback]
filesPath=getFilesPathWithoutSeizure(i, indexPat)
history=model.fit_generator(generate_arrays_for_training(indexPat, filesPath, end=75), 
                                validation_data=generate_arrays_for_training(indexPat, filesPath, start=75),
                                steps_per_epoch=int((len(filesPath)-int(len(filesPath)/100*25))), 
                                validation_steps=int((len(filesPath)-int(len(filesPath)/100*75))),
                                verbose=2,
                                epochs=300, max_queue_size=2, shuffle=True, callbacks=callbacks_list)

【问题讨论】:

  • 当您在代码中遇到错误时,请发布您获得的完整错误回溯。使用它编辑您的原始帖子,而不是将其作为评论发布,因为 cmets 的格式很少,难以阅读

标签: python tensorflow optimization keras deep-learning


【解决方案1】:

在这部分代码中:

lrate = LearningRateScheduler(step_decay)
sgd = SGD(lr=lrate, decay=0, momentum=0.9, nesterov=True)

您将SGD的学习率设置为回调,这是不正确的,您应该将初始学习率设置为SGD:

sgd = SGD(lr=0.01, decay=0, momentum=0.9, nesterov=True)

并将回调列表传递给model.fit,也许它是您也称为lrate 的先前变量的工件。

【讨论】:

  • 我想降低每个时期的学习率。如何使用 SGD 优化器来做到这一点? ,我无法将初始学习率设置为 SGD,因为学习会在每个时期更新
  • @gigi 不,这种想法是错误的,你设置了一个初始学习率,LearningRateScheduler 回调将设置跨时期的学习率。
  • 它不起作用。学习率不会在每个时期更新。我在SGD中设置了初始学习率=0.1 sgd = SGD(lr=0.1, decay=0, momentum=0.9, nesterov=True)
  • @gigi 您究竟是如何检查它是否不起作用的?我经常使用该回调,我知道它工作正常。
  • 仅使用初始学习率,不会更新每个时期的学习率。
【解决方案2】:

您可以如下所示在每个 epoch 后通过自定义值降低学习率

def scheduler(epoch, lr):
  if epoch < 1:
    return lr
  else:
    return lr * tf.math.exp(-0.1)

上面是降低学习率的函数,现在这个函数应该在每个 epoch 之后调用。下面是使用 LearningRateScheduler 初始化函数(您可以查看 tensorflow 网站上的文档了解更多详细信息)

callback = tf.keras.callbacks.LearningRateScheduler(scheduler)

现在,让我们从 fit 方法中调用它。

history = model.fit(trainGen, validation_data=valGen, validation_steps=val_split//batch_size, epochs=200, steps_per_epoch= train_split//batch_size, callbacks=[callback])

正如您在上面看到的,您只需在 fit 方法中配置初始化的调度程序并运行它。您会注意到,在每个 epoch 之后,学习率会根据您在调度程序函数中设置的值不断降低。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2021-02-16
    • 2017-06-12
    • 1970-01-01
    相关资源
    最近更新 更多