Reputation: 151
I am fitting a model using Keras and passed the callbacks list to the model fit, but encountered the following error. What am I doing wrong here?
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)
The error is as follows:
---------------------------------------------------------------------------
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
It only says 'AttributeError: set_model' and I don't understand what's causing the error.
Upvotes: 1
Views: 1481
Reputation: 26
sometimes if you write in the list all required callbacks it accepts but sometimes you should assign it to another variable then write it like this. history = model.fit( train_generator, validation_data = valid_generator, epochs=10, verbose=1, callbacks= [my_callbacks]) may your model.fit() will work.
Upvotes: 1