【发布时间】: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