我正在使用 keras 拟合模型并将回调列表传递给模型 fit_generator 但遇到以下错误。请帮忙。
AttributeError: 'function' object has no attribute 'set_model'
代码片段:
from keras.callbacks import LearningRateScheduler
import numpy as np
from keras import optimizers
from keras.callbacks import *
def lr_schedule(epoch):
lrate = 0.1
if epoch > 50:
lrate = 0.01
elif epoch > 75:
lrate = 0.001
return lrate
filepath="latest_weight_ckpt_{epoch:02d}_{val_acc:.2f}.hdf5"
model_ckpt = ModelCheckpoint(filepath, monitor='val_acc', verbose=1,
save_best_only=True, mode='max')
#lists = [model_ckpt]
#model_checkpoint= ModelCheckpoint("weights/DenseNet-BC-100-12-
CIFAR100.h5", monitor="val_acc",
save_best_only=True,save_weights_only=True)
callbacks_list=[lr_schedule, model_ckpt]
model.fit_generator(train_generator, epochs=25,
steps_per_epoch=200,verbose=1, validation_steps=200,
validation_data=validation_generator,callbacks=callbacks_list)
最佳答案
问题是您传递的是 python 函数 (lr_schedule
) 作为回调而不是 Keras 回调对象。您应该改为使用 Keras LearningRateScheduler
回调,如下所示:
lr = LearningRateScheduler(lr_schedule)
callbacks_list=[lr, model_ckpt]
关于keras - 属性错误 : 'function' object has no attribute 'set_model' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55438400/