【发布时间】:2021-06-15 15:49:26
【问题描述】:
我正在使用 Keras 拟合模型并将回调列表传递给模型拟合,但遇到以下错误。我在这里做错了什么?
from tensorflow.keras.callbacks import ReduceLROnPlateau,
ModelCheckpoint, EarlyStopping, Callback
checkpoint = ModelCheckpoint(f'model{i}.h5', save_best_only=True,
save_weights_only = True,
monitor='val_loss',
verbose = 1)
lr_reducer = ReduceLROnPlateau(monitor="val_loss",
patience=3,
min_lr=1e-6)
my_callbacks = [checkpoint, lr_reducer]
history = model.fit(
train_generator,
validation_data = valid_generator,
epochs=10,
verbose=1,
callbacks= my_callbacks)
错误如下:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-135-0538428c86d8> in <module>
75 epochs=10,
76 verbose=1,
---> 77 callbacks= my_callbacks)
78
79
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1073 verbose=verbose,
1074 epochs=epochs,
-> 1075 steps=data_handler.inferred_steps)
1076
1077 self.stop_training = False
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/callbacks.py in __init__(self, callbacks, add_history, add_progbar, model, **params)
229
230 if model:
--> 231 self.set_model(model)
232 if params:
233 self.set_params(params)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/callbacks.py in set_model(self, model)
284 model.history = self._history
285 for callback in self.callbacks:
--> 286 callback.set_model(model)
287
288 def _call_batch_hook(self, mode, hook, batch, logs=None):
/opt/conda/lib/python3.7/site-packages/fastcore/basics.py in __getattr__(self, k)
387 attr = getattr(self,self._default,None)
388 if attr is not None: return getattr(attr,k)
--> 389 raise AttributeError(k)
390 def __dir__(self): return custom_dir(self,self._dir())
391 # def __getstate__(self): return self.__dict__
AttributeError: set_model
它只显示“AttributeError: set_model”,我不明白是什么导致了错误。
【问题讨论】:
-
能否将您的导入添加到问题中?这可能是您的进口冲突。另外请说出
model{i}.h5中的{i}是什么意思? -
我用导入编辑了这个问题。还有
{i},因为我在做交叉验证。当我注释掉lr_reducer = ReduceLROnPlateau(monitor="val_loss", patience=3, min_lr=1e-6)并以callbacks=[checkpoint]进行回调时,它起作用了,但我不知道为什么包含lr_reducer会引发错误。 -
您是否检查过所有其他对象(例如图层、模型和...)来自
tensorflow.keras而不是来自keras? -
是的,我做到了,但错误仍然存在。
-
您能分享完整的代码来复制您的问题吗?以便我们尝试帮助您。谢谢!
标签: python tensorflow keras callback