Keras callback AttributeError: 'ModelCheckpoint' object has no attribute '_implements_train_batch_hooks'
Asked Answered
D

3

7

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!

Doublure answered 18/6, 2020 at 15:54 Comment(0)
B
7

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.

Burschenschaft answered 18/6, 2020 at 22:20 Comment(0)
H
0

replace: from keras.callbacks import ModelCheckpoint to: from tensorflow.keras.callbacks import ModelCheckpoint

Hundley answered 18/8, 2021 at 15:59 Comment(0)
D
0

I encountered the same error. I used import tf_keras after running pip install tf_keras. Then from tf_keras import callbacks to successfully train my model. It might be a computer/local environment specific issue.

Dorinedorion answered 5/9 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.