Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras callback AttributeError: 'ModelCheckpoint' object has no attribute '_implements_train_batch_hooks'

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!

like image 612
Sanda Achard Avatar asked Jun 18 '20 15:06

Sanda Achard


1 Answers

I also encountered this problem recently.

What did I find out: recently the keras or tensorflow version was updated by developers and this cause the problem.

Solution: since the developers of keras require everyone to switch to the version tf.keras, you need to replace of your code import section.

From:

import keras

To:

import tensorflow.keras as keras

After that everything worked for me.

like image 61
maximusend Avatar answered Nov 08 '22 18:11

maximusend