I'm using Keras (with TensorFlow back-end) to implement a neural network and want to only save the model that minimises loss on the validation set during training. To do this, I instantiated a ModelCheckpoint and pass it when calling the fit method of the model. However, when I do this I get the following error: "AttributeError: 'ModelCheckpoint' object has no attribute '_implements_train_batch_hooks'
". The closest thing I have found online for my problem is this post with a similar error, where the problem came from mixing modules from keras
and tf.keras
, however this is not my case as all my modules are imported from keras
. I've been looking online and through the Keras documentation for a while and can't find anything that could explain this bug. Here are the parts of the code that seem the most relevant to the issue:
Imported modules:
from keras.models import Sequential
from keras.layers import Embedding, Conv1D, Dense, Dropout, GlobalMaxPool1D, Concatenate
from keras.callbacks import ModelCheckpoint
ModelCheckpoint instantiation, model compilation and call to fit method:
checkpoint = ModelCheckpoint('../model_best.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
history = model.fit(x_train, y_train,
epochs = 10, batch_size = 64,
validation_data = (x_val, y_val),
callbacks = [checkpoint])
...and here is the full Traceback:
Traceback (most recent call last):
File "/Users/thisuser/thisrepo/classifier.py", line 39, in <module>
callbacks = [checkpoint])
File "/Users/thisuser/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
return method(self, *args, **kwargs)
File "/Users/thisuser/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 826, in fit
steps=data_handler.inferred_steps)
File "/Users/thisuser/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/callbacks.py", line 231, in __init__
cb._implements_train_batch_hooks() for cb in self.callbacks)
File "/Users/thisuser/anaconda3/envs/tf/lib/python3.7/site-packages/tensorflow/python/keras/callbacks.py", line 231, in <genexpr>
cb._implements_train_batch_hooks() for cb in self.callbacks)
AttributeError: 'ModelCheckpoint' object has no attribute '_implements_train_batch_hooks'
The versions I'm using are:
- Python: 3.7.7
- Keras: 2.3.0-tf
Does anyone know what might be causing the issue? If needed I can modify my code slightly to give it all here, so that it is reproducible. Thanks in advance for your help!